1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Support; |
12
|
|
|
|
13
|
|
|
trait Options |
14
|
|
|
{ |
15
|
|
|
protected array $defaultOptions = []; |
16
|
|
|
protected array $options = []; |
17
|
|
|
|
18
|
|
|
public function __construct(array $options = []) |
19
|
|
|
{ |
20
|
|
|
$this->defaultOptions = $options; |
21
|
|
|
$this->setOptions($options); |
22
|
|
|
$this->init(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function init() { |
26
|
|
|
|
27
|
|
|
} |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set options array |
31
|
|
|
* @param array $options |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function setOptions(array $options): void |
35
|
|
|
{ |
36
|
|
|
$this->options = $options; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get options array |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getOptions(): array |
44
|
|
|
{ |
45
|
|
|
return $this->options; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set an option value |
50
|
|
|
* @param string $key |
51
|
|
|
* @param $value |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function setOption(string $key, $value = null): void |
55
|
|
|
{ |
56
|
|
|
$this->options[$key] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Retrieve an option value by key |
61
|
|
|
* - Return default value if null |
62
|
|
|
* @param string $key The key to retrieve |
63
|
|
|
* @param mixed|null $default Returned if the option couldn't be found |
64
|
|
|
* @return mixed|null |
65
|
|
|
*/ |
66
|
|
|
public function getOption(string $key, $default = null) |
67
|
|
|
{ |
68
|
|
|
return $this->options[$key] ?? $default; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Remove an option by key |
73
|
|
|
* @param string $key |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function removeOption(string $key): void |
77
|
|
|
{ |
78
|
|
|
if (isset($this->options[$key])) { |
79
|
|
|
unset($this->options[$key]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Reset options to default values |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function resetOptions(): void |
88
|
|
|
{ |
89
|
|
|
$this->setOptions($this->defaultOptions); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Clear options by forcing an empty array |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function clearOptions(): void |
97
|
|
|
{ |
98
|
|
|
$this->options = []; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|