blob: b09462bc5ad42ec3131a4b88aa7e36dbaffc5ea8 (
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\User;
use Countable;
/**
* User preferences container
*
* Usage example:
* <code>
* <?php
*
* use Icinga\User\Preferences;
*
* $preferences = new Preferences(); // Start with empty preferences
*
* $preferences = new Preferences(array('aPreference' => 'value')); // Start with initial preferences
*
* $preferences->aNewPreference = 'value'; // Set a preference
*
* unset($preferences->aPreference); // Unset a preference
*
* // Retrieve a preference and return a default value if the preference does not exist
* $anotherPreference = $preferences->get('anotherPreference', 'defaultValue');
*/
class Preferences implements Countable
{
/**
* Preferences key-value array
*
* @var array
*/
protected $preferences = array();
/**
* Constructor
*
* @param array $preferences Preferences key-value array
*/
public function __construct(array $preferences = array())
{
$this->preferences = $preferences;
}
/**
* Count all preferences
*
* @return int The number of preferences
*/
public function count(): int
{
return count($this->preferences);
}
/**
* Determine whether a preference exists
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return array_key_exists($name, $this->preferences);
}
/**
* Write data to a preference
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->preferences[$name] = $value;
}
/**
* Retrieve a preference section
*
* @param string $name
*
* @return array|null
*/
public function get($name)
{
if (array_key_exists($name, $this->preferences)) {
return $this->preferences[$name];
}
return null;
}
/**
* Retrieve a value from a specific section
*
* @param string $section
* @param string $name
* @param null $default
*
* @return array|null
*/
public function getValue($section, $name, $default = null)
{
if (array_key_exists($section, $this->preferences)
&& array_key_exists($name, $this->preferences[$section])
) {
return $this->preferences[$section][$name];
}
return $default;
}
/**
* Magic method so that $obj->value will work.
*
* @param string $name
*
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}
/**
* Remove a given preference
*
* @param string $name Preference name
*/
public function remove($name)
{
unset($this->preferences[$name]);
}
/**
* Determine if a preference is set and is not NULL
*
* @param string $name Preference name
*
* @return bool
*/
public function __isset($name)
{
return isset($this->preferences[$name]);
}
/**
* Unset a given preference
*
* @param string $name Preference name
*/
public function __unset($name)
{
$this->remove($name);
}
/**
* Get preferences as array
*
* @return array
*/
public function toArray()
{
return $this->preferences;
}
}
|