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<string,mixed>|NULL |
31
|
|
|
*/ |
32
|
|
|
protected ?array $options = null; |
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 $value |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function setOption(string $name, $value) : self |
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<string,mixed> $options |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function setOptions(array $options) : self |
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
|
|
|
{ |
82
|
|
|
$this->options = $this->getDefaultOptions(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->options[$name] ?? $default; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Enforces that the option value is a string. Numbers are converted |
90
|
|
|
* to string, strings are passed through, and all other types will |
91
|
|
|
* return the default value. The default value is also returned if |
92
|
|
|
* the string is empty. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @param string $default Used if the option does not exist, is invalid, or empty. |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getStringOption(string $name, string $default='') : string |
99
|
|
|
{ |
100
|
|
|
$value = $this->getOption($name, false); |
101
|
|
|
|
102
|
|
|
if((is_string($value) || is_numeric($value)) && !empty($value)) { |
103
|
|
|
return (string)$value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $default; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Treats the option value as a boolean value: will return |
111
|
|
|
* true if the value actually is a boolean true. |
112
|
|
|
* |
113
|
|
|
* NOTE: boolean string representations are not accepted. |
114
|
|
|
* |
115
|
|
|
* @param string $name |
116
|
|
|
* @param bool $default |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function getBoolOption(string $name, bool $default=false) : bool |
120
|
|
|
{ |
121
|
|
|
if($this->getOption($name) === true) |
122
|
|
|
{ |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $default; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Treats the option value as an integer value: will return |
131
|
|
|
* valid integer values (also from integer strings), or the |
132
|
|
|
* default value otherwise. |
133
|
|
|
* |
134
|
|
|
* @param string $name |
135
|
|
|
* @param int $default |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
|
|
public function getIntOption(string $name, int $default=0) : int |
139
|
|
|
{ |
140
|
|
|
$value = $this->getOption($name); |
141
|
|
|
if(ConvertHelper::isInteger($value)) { |
142
|
|
|
return (int)$value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $default; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Treats an option as an array, and returns its value |
150
|
|
|
* only if it contains an array - otherwise, an empty |
151
|
|
|
* array is returned. |
152
|
|
|
* |
153
|
|
|
* @param string $name |
154
|
|
|
* @return array<int|string,mixed> |
155
|
|
|
*/ |
156
|
|
|
public function getArrayOption(string $name) : array |
157
|
|
|
{ |
158
|
|
|
$val = $this->getOption($name); |
159
|
|
|
if(is_array($val)) { |
160
|
|
|
return $val; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return array(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Checks whether the specified option exists - even |
168
|
|
|
* if it has a NULL value. |
169
|
|
|
* |
170
|
|
|
* @param string $name |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function hasOption(string $name) : bool |
174
|
|
|
{ |
175
|
|
|
if(!isset($this->options)) { |
176
|
|
|
$this->options = $this->getDefaultOptions(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return array_key_exists($name, $this->options); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns all options in one associative array. |
184
|
|
|
* |
185
|
|
|
* @return array<string,mixed> |
186
|
|
|
*/ |
187
|
|
|
public function getOptions() : array |
188
|
|
|
{ |
189
|
|
|
if(!isset($this->options)) { |
190
|
|
|
$this->options = $this->getDefaultOptions(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this->options; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Checks whether the option's value is the one specified. |
198
|
|
|
* |
199
|
|
|
* @param string $name |
200
|
|
|
* @param mixed $value |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
public function isOption(string $name, $value) : bool |
204
|
|
|
{ |
205
|
|
|
return $this->getOption($name) === $value; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|