1 | <?php |
||
14 | class AssociativeArray |
||
15 | { |
||
16 | /** |
||
17 | * True associative array storing the data. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $data = []; |
||
22 | |||
23 | /** |
||
24 | * Separation string for recursive search. |
||
25 | * |
||
26 | * @var |
||
27 | */ |
||
28 | protected $separator; |
||
29 | |||
30 | /** |
||
31 | * Block this object so that it can't be modified |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $readOnly = false; |
||
36 | |||
37 | /** |
||
38 | * AssociativeArray constructor. |
||
39 | * |
||
40 | * @param array $data The original array with the data. |
||
41 | * @param string $keySeparator |
||
42 | */ |
||
43 | 15 | public function __construct($data = [], $keySeparator = '/') |
|
48 | |||
49 | /** |
||
50 | * Get value from, if is not set then return default value(null) |
||
51 | * |
||
52 | * @param string[]|string $key Key or path to the value |
||
53 | * (either array or string separated with the separator) |
||
54 | * @param mixed $default Default value to return if none was find |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | 9 | public function get($key, $default = null) { |
|
61 | |||
62 | /** |
||
63 | * Check if a value for the key exists. |
||
64 | * |
||
65 | * @param string[]|string $key Key or path to the value |
||
66 | * (either array or string separated with the separator) |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | 1 | public function keyExist($key) |
|
74 | |||
75 | /** |
||
76 | * Set data inside |
||
77 | * |
||
78 | * @param string[]|string $key Key or path to the value to set |
||
79 | * (either array or string separated with the separator) |
||
80 | * @param mixed $value Value to put |
||
81 | */ |
||
82 | 4 | public function set($key, $value) { |
|
86 | |||
87 | /** |
||
88 | * Empty the data stored. |
||
89 | */ |
||
90 | 1 | public function clear() |
|
91 | { |
||
92 | 1 | $this->checkReadOnly(); |
|
93 | 1 | $this->data = []; |
|
94 | 1 | } |
|
95 | |||
96 | |||
97 | /** |
||
98 | * Get the array with all the data. |
||
99 | * |
||
100 | * @return array All the data |
||
101 | */ |
||
102 | 4 | public function getArray(){ |
|
105 | |||
106 | |||
107 | /** |
||
108 | * Replace the current array entirely. |
||
109 | * |
||
110 | * @param array $data new data |
||
111 | */ |
||
112 | public function setData($data) { |
||
116 | |||
117 | /** |
||
118 | * Makes the data inside read only !! |
||
119 | */ |
||
120 | 1 | public function makeReadOnly() { |
|
123 | |||
124 | /** |
||
125 | * Checks if the associative array is read only. |
||
126 | * |
||
127 | * @throws ReadOnlyException |
||
128 | */ |
||
129 | 5 | public function checkReadOnly() { |
|
134 | |||
135 | /** |
||
136 | * Get value from array, if is not set then return default value(null) |
||
137 | * |
||
138 | * @param array $data Array to get data from |
||
139 | * @param string[]|string $key Key or path to the value |
||
140 | * (either array or string separated with the separator) |
||
141 | * @param mixed $default Default value to return if none was find |
||
142 | * @param string $separator Separator to use |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | 9 | public static function getFromKey($data, $key, $default = null, $separator = '/') |
|
153 | |||
154 | /** |
||
155 | * Private unsecured function for getFromKey |
||
156 | * |
||
157 | * @param $data |
||
158 | * @param $key |
||
159 | * @param $default |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | 9 | private static function recursiveGetFromKey($data, $key, $default) |
|
172 | |||
173 | /** |
||
174 | * Get value from array, if is not set then return default value(null) |
||
175 | * |
||
176 | * @param array $data Array to get data from |
||
177 | * @param string[]|string $key Key or path to the value |
||
178 | * (either array or string separated with the separator) |
||
179 | * @param string $separator Separator to use |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | 1 | public static function checkKeyExist($data, $key, $separator = '/') |
|
190 | |||
191 | /** |
||
192 | * @param array $data Array to get data from |
||
193 | * @param string[] $key Key or path to the value |
||
194 | * (either array or string separated with the separator) |
||
195 | * @param $data |
||
196 | * @param $key |
||
197 | * |
||
198 | * @return mixed |
||
199 | */ |
||
200 | 1 | private static function recursiveKeyExist($data, $key) |
|
209 | |||
210 | /** |
||
211 | * Set data inside an array |
||
212 | * |
||
213 | * @param array $data array to set new value in |
||
214 | * @param string[]|string $key Key or path to the value to set |
||
215 | * (either array or string separated with the separator) |
||
216 | * @param mixed $value Value to put |
||
217 | * @param string $separator separator to use with the string |
||
218 | */ |
||
219 | 3 | public static function setFromKey(&$data, $key, $value, $separator = '/') |
|
226 | |||
227 | /** |
||
228 | * Private unsecured method to set data in array |
||
229 | * |
||
230 | * @param $data |
||
231 | * @param $key |
||
232 | * @param $value |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | 3 | private static function recursiveSetFromKey($data, $key, $value) |
|
254 | } |
||
255 |