|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see AppUtils\Traits_Optionable} trait, |
|
4
|
|
|
* and the matching interface. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Application Utils |
|
7
|
|
|
* @subpackage Traits |
|
8
|
|
|
* @see Traits_Optionable |
|
9
|
|
|
* @see Interface_Optionable |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace AppUtils; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait for adding options to a class: allows setting |
|
16
|
|
|
* and getting options of all types. |
|
17
|
|
|
* |
|
18
|
|
|
* NOTE: To add this to a class, it must use the trait, |
|
19
|
|
|
* but also implement the interface. |
|
20
|
|
|
* |
|
21
|
|
|
* @package Application Utils |
|
22
|
|
|
* @subpackage Traits |
|
23
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
24
|
|
|
* |
|
25
|
|
|
* @see Interface_Optionable |
|
26
|
|
|
*/ |
|
27
|
|
|
trait Traits_Optionable |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $options; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Sets an option to the specified value. This can be any |
|
36
|
|
|
* kind of variable type, including objects, as needed. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @param mixed $default |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setOption(string $name, $value) : Interface_Optionable |
|
43
|
|
|
{ |
|
44
|
|
|
if(!isset($this->options)) { |
|
45
|
|
|
$this->options = $this->getDefaultOptions(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->options[$name] = $value; |
|
49
|
|
|
return $this; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets a collection of options at once, from an |
|
54
|
|
|
* associative array. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $options |
|
57
|
|
|
* @return Interface_Optionable |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setOptions(array $options) : Interface_Optionable |
|
60
|
|
|
{ |
|
61
|
|
|
foreach($options as $name => $value) { |
|
62
|
|
|
$this->setOption($name, $value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Retrieves an option's value. |
|
70
|
|
|
* |
|
71
|
|
|
* NOTE: Use the specialized type getters to ensure an option |
|
72
|
|
|
* contains the expected type (for ex. getArrayOption()). |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @param mixed $default The default value to return if the option does not exist. |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getOption(string $name, $default=null) |
|
79
|
|
|
{ |
|
80
|
|
|
if(!isset($this->options)) { |
|
81
|
|
|
$this->options = $this->getDefaultOptions(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if(isset($this->options[$name])) { |
|
85
|
|
|
return $this->options[$name]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $default; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Enforces that the option value is a string. Scalar |
|
93
|
|
|
* values are converted to string, and non-scalar values |
|
94
|
|
|
* are converted to an empty string. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $name |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getStringOption(string $name) : string |
|
100
|
|
|
{ |
|
101
|
|
|
$value = $this->getOption($name, false); |
|
102
|
|
|
|
|
103
|
|
|
if(is_scalar($value)) { |
|
104
|
|
|
return (string)$value; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return ''; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Treats the option value as a boolean value: will return |
|
112
|
|
|
* true if the value actually is a boolean true. |
|
113
|
|
|
* |
|
114
|
|
|
* NOTE: boolean string representations are not accepted. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $name |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getBoolOption(string $name) : bool |
|
120
|
|
|
{ |
|
121
|
|
|
if($this->getOption($name) === true) { |
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Treats an option as an array, and returns its value |
|
130
|
|
|
* only if it contains an array - otherwise, an empty |
|
131
|
|
|
* array is returned. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $name |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getArrayOption(string $name) : array |
|
137
|
|
|
{ |
|
138
|
|
|
$val = $this->getOption($name); |
|
139
|
|
|
if(is_array($val)) { |
|
140
|
|
|
return $val; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return array(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Checks whether the specified option exists - even |
|
148
|
|
|
* if it has a NULL value. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $name |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
|
|
public function hasOption(string $name) : bool |
|
154
|
|
|
{ |
|
155
|
|
|
if(!isset($this->options)) { |
|
156
|
|
|
$this->options = $this->getDefaultOptions(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return array_key_exists($name, $this->options); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Returns all options in one associative array. |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getOptions() : array |
|
167
|
|
|
{ |
|
168
|
|
|
if(!isset($this->options)) { |
|
169
|
|
|
$this->options = $this->getDefaultOptions(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this->options; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Checks whether the option's value is the one specified. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $name |
|
179
|
|
|
* @param mixed $value |
|
180
|
|
|
* @return bool |
|
181
|
|
|
*/ |
|
182
|
|
|
public function isOption(string $name, $value) : bool |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->getOption($name) === $value; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Interface for classes that use the optionable trait. |
|
190
|
|
|
* The trait itself fulfills most of the interface, but |
|
191
|
|
|
* it is used to guarantee internal type checks will work, |
|
192
|
|
|
* as well as ensure the abstract methods are implemented. |
|
193
|
|
|
* |
|
194
|
|
|
* @package Application Utils |
|
195
|
|
|
* @subpackage Traits |
|
196
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
197
|
|
|
* |
|
198
|
|
|
* @see Traits_Optionable |
|
199
|
|
|
*/ |
|
200
|
|
|
interface Interface_Optionable |
|
201
|
|
|
{ |
|
202
|
|
|
function setOption(string $name, $value) : Interface_Optionable; |
|
203
|
|
|
function getOption(string $name, $default=null); |
|
204
|
|
|
function setOptions(array $options) : Interface_Optionable; |
|
205
|
|
|
function getOptions() : array; |
|
206
|
|
|
function isOption(string $name, $value) : bool; |
|
207
|
|
|
function hasOption(string $name) : bool; |
|
208
|
|
|
function getDefaultOptions() : array; |
|
209
|
|
|
} |
|
210
|
|
|
|