summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/Modules/NavigationItemContainer.php
blob: c906ccb459c29dc818807680b80e294007e9d5a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Application\Modules;

use Icinga\Exception\ProgrammingError;

/**
 * Container for module navigation items
 */
abstract class NavigationItemContainer
{
    /**
     * This navigation item's name
     *
     * @var string
     */
    protected $name;

    /**
     * This navigation item's properties
     *
     * @var array
     */
    protected $properties;

    /**
     * Create a new NavigationItemContainer
     *
     * @param   string  $name
     * @param   array   $properties
     */
    public function __construct($name, array $properties = array())
    {
        $this->name = $name;
        $this->properties = $properties;
    }

    /**
     * Set this menu item's name
     *
     * @param   string  $name
     *
     * @return  $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Return this menu item's name
     *
     * @return  string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set this menu item's properties
     *
     * @param   array   $properties
     *
     * @return  $this
     */
    public function setProperties(array $properties)
    {
        $this->properties = $properties;
        return $this;
    }

    /**
     * Return this menu item's properties
     *
     * @return  array
     */
    public function getProperties()
    {
        return $this->properties ?: array();
    }

    /**
     * Allow dynamic setters and getters for properties
     *
     * @param   string  $name
     * @param   array   $arguments
     *
     * @return  mixed
     *
     * @throws  ProgrammingError    In case the called method is not supported
     */
    public function __call($name, $arguments)
    {
        if (method_exists($this, $name)) {
            return call_user_func(array($this, $name), $this, $arguments);
        }

        $type = substr($name, 0, 3);
        if ($type !== 'set' && $type !== 'get') {
            throw new ProgrammingError(
                'Dynamic method %s is not supported. Only getters (get*) and setters (set*) are.',
                $name
            );
        }

        $propertyName = strtolower(join('_', preg_split('~(?=[A-Z])~', lcfirst(substr($name, 3)))));
        if ($type === 'set') {
            $this->properties[$propertyName] = $arguments[0];
            return $this;
        } else { // $type === 'get'
            return array_key_exists($propertyName, $this->properties) ? $this->properties[$propertyName] : null;
        }
    }
}