1 | <?php |
||
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() |
|
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) |
|
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) |
|
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) |
|
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) |
|
176 | |||
177 | /** |
||
178 | * Unsets the key |
||
179 | * |
||
180 | * @param mixed $key The key to unset |
||
181 | */ |
||
182 | 2 | public function offsetUnset($key) |
|
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) |
|
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) |
|
234 | } |
||
235 |