1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\vars library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @package m1/vars |
12
|
|
|
* @version 1.0.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Vars\Resource; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract Resource enables normal and dot notation array access on resources |
23
|
|
|
* |
24
|
|
|
* @since 0.1.0 |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractResource implements \ArrayAccess |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The resource content |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $content = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Sets the resource contents |
37
|
|
|
* |
38
|
|
|
* @param array $content |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
1 |
|
public function setContent($content) |
43
|
|
|
{ |
44
|
1 |
|
$this->content = $content; |
45
|
1 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the content of the resource |
50
|
|
|
* |
51
|
|
|
* @return array The content |
52
|
|
|
*/ |
53
|
63 |
|
public function getContent() |
54
|
|
|
{ |
55
|
63 |
|
return $this->content; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Normal `$example[$key]` access for the array |
60
|
|
|
* |
61
|
|
|
* @param mixed $key The key to get the value for |
62
|
|
|
* |
63
|
|
|
* @return array|bool|null The resource key value |
64
|
|
|
*/ |
65
|
4 |
|
public function offsetGet($key) |
66
|
|
|
{ |
67
|
4 |
|
return $this->internalGet($this->content, $key); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Object oriented get access for the array |
72
|
|
|
* |
73
|
|
|
* @param mixed $key The key to get the value for |
74
|
|
|
* |
75
|
|
|
* @return array|bool|null The resource key value |
76
|
|
|
*/ |
77
|
9 |
|
public function get($key) |
78
|
|
|
{ |
79
|
9 |
|
return $this->internalGet($this->content, $key); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The internal get function for getting values by their key |
85
|
|
|
* |
86
|
|
|
* @param array $array The array to use -- will always be $this->content |
87
|
|
|
* @param mixed $key The key to find the value for |
88
|
|
|
* @param bool $exists Whether to return null or false dependant on the calling function |
89
|
|
|
* |
90
|
|
|
* @return array|bool|null The resource key value |
91
|
|
|
*/ |
92
|
9 |
|
private function internalGet(array $array, $key, $exists = false) |
93
|
|
|
{ |
94
|
9 |
|
if (isset($array[$key])) { |
95
|
4 |
|
return (!$exists) ? $array[$key] : true; |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
$parts = explode('.', $key); |
99
|
|
|
|
100
|
5 |
|
foreach ($parts as $part) { |
101
|
5 |
|
if (!is_array($array) || !isset($array[$part])) { |
102
|
2 |
|
return (!$exists) ? null : false; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
$array = $array[$part]; |
106
|
4 |
|
} |
107
|
|
|
|
108
|
3 |
|
return (!$exists) ? $array : true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Normal `$example[$key] = 'hello'` access for the array |
113
|
|
|
* |
114
|
|
|
* @param mixed $key The key to set the value for |
115
|
|
|
* @param mixed $value The value to set |
116
|
|
|
*/ |
117
|
2 |
|
public function offsetSet($key, $value) |
118
|
|
|
{ |
119
|
2 |
|
$this->internalSet($this->content, $key, $value); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Object oriented set access for the array |
124
|
|
|
* |
125
|
|
|
* @param string $key The key to set the value for |
126
|
|
|
* @param string $value The value to set |
127
|
|
|
*/ |
128
|
54 |
|
public function set($key, $value) |
129
|
|
|
{ |
130
|
54 |
|
$this->internalSet($this->content, $key, $value); |
131
|
54 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Object oriented set access for the array |
135
|
|
|
* |
136
|
|
|
* @param array $array The array to use -- will always be based on $this->content but can be used recursively |
137
|
|
|
* @param mixed $key The key to set the value for |
138
|
|
|
* @param mixed $value The value to set |
139
|
|
|
* |
140
|
|
|
* @return array Returns the modified array |
141
|
|
|
*/ |
142
|
54 |
|
private function internalSet(array &$array, $key, $value) |
143
|
|
|
{ |
144
|
54 |
|
if (is_null($key)) { |
145
|
1 |
|
return $array = $value; |
146
|
|
|
} |
147
|
|
|
|
148
|
54 |
|
$keys = explode('.', $key); |
149
|
|
|
|
150
|
54 |
|
while (count($keys) > 1) { |
151
|
12 |
|
$key = array_shift($keys); |
152
|
|
|
|
153
|
12 |
|
if (! isset($array[$key]) || ! is_array($array[$key])) { |
154
|
12 |
|
$array[$key] = []; |
155
|
12 |
|
} |
156
|
|
|
|
157
|
12 |
|
$array = &$array[$key]; |
158
|
12 |
|
} |
159
|
|
|
|
160
|
54 |
|
$array[array_shift($keys)] = $value; |
161
|
|
|
|
162
|
54 |
|
return $array; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Checks whether the key exists |
167
|
|
|
* |
168
|
|
|
* @param mixed $key The key to check |
169
|
|
|
* |
170
|
|
|
* @return bool Does the key exist |
171
|
|
|
*/ |
172
|
2 |
|
public function offsetExists($key) |
173
|
|
|
{ |
174
|
2 |
|
return $this->internalGet($this->content, $key, true); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Unsets the key |
179
|
|
|
* |
180
|
|
|
* @param mixed $key The key to unset |
181
|
|
|
*/ |
182
|
2 |
|
public function offsetUnset($key) |
183
|
|
|
{ |
184
|
2 |
|
$this->internalUnset($this->content, $key); |
185
|
2 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Internal unset for the key |
189
|
|
|
* |
190
|
|
|
* @param array $array The array to use -- will always be based on $this->content but can be used recursively |
191
|
|
|
* @param mixed $key The key to unset |
192
|
|
|
*/ |
193
|
2 |
|
protected function internalUnset(array &$array, $key) |
194
|
|
|
{ |
195
|
2 |
|
$parts = explode(".", $key); |
196
|
|
|
|
197
|
2 |
|
while (count($parts) > 1) { |
198
|
1 |
|
$part = array_shift($parts); |
199
|
|
|
|
200
|
1 |
|
if (isset($array[$part]) && is_array($array[$part])) { |
201
|
1 |
|
$array =& $array[$part]; |
202
|
1 |
|
} |
203
|
1 |
|
} |
204
|
|
|
|
205
|
2 |
|
unset($array[array_shift($parts)]); |
206
|
2 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Port of array_key_exists to \ArrayAccess |
210
|
|
|
* |
211
|
|
|
* @param mixed $key The key to check if exists |
212
|
|
|
* |
213
|
|
|
* @return bool Does the key exist |
214
|
|
|
*/ |
215
|
4 |
|
public function arrayKeyExists($key) |
216
|
|
|
{ |
217
|
|
|
|
218
|
4 |
|
if (array_key_exists($key, $this->content)) { |
219
|
2 |
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
2 |
|
$parts = explode('.', $key); |
223
|
2 |
|
$arr = $this->content; |
224
|
2 |
|
foreach ($parts as $part) { |
225
|
2 |
|
if (!is_array($arr) || !array_key_exists($part, $arr)) { |
226
|
1 |
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
$arr = $arr[$part]; |
230
|
1 |
|
} |
231
|
|
|
|
232
|
1 |
|
return true; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|