|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeoBase\Countries; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use ArrayIterator; |
|
7
|
|
|
use Countable; |
|
8
|
|
|
use IteratorAggregate; |
|
9
|
|
|
use JsonSerializable; |
|
10
|
|
|
|
|
11
|
|
|
class ArrayCollection implements Countable, IteratorAggregate, ArrayAccess, JsonSerializable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $elements; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $elements |
|
20
|
|
|
*/ |
|
21
|
8 |
|
public function __construct(array $elements = []) |
|
22
|
|
|
{ |
|
23
|
8 |
|
$this->elements = $elements; |
|
24
|
8 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function toArray() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->elements; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function jsonSerialize() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->elements; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $key |
|
44
|
|
|
* |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function order($key) |
|
48
|
|
|
{ |
|
49
|
1 |
|
if (strpos($key, '.') !== false) { |
|
50
|
1 |
|
$this->orderByChildCollection(explode('.', $key)); |
|
51
|
|
|
|
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
trigger_error('feature not implemented'); // todo implement feature |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $keys |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
1 |
|
private function orderByChildCollection($keys) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$ordered = []; |
|
65
|
1 |
|
foreach ($this->getIterator() as $item) { |
|
66
|
1 |
|
$value = $this->getChildValueRecursive($item, $keys); |
|
67
|
1 |
|
$ordered[$value] = $item; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
ksort($ordered); |
|
71
|
1 |
|
$ordered = array_values($ordered); |
|
72
|
1 |
|
$this->elements = $ordered; |
|
73
|
|
|
|
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $item |
|
79
|
|
|
* @param $keys |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed|null |
|
82
|
|
|
*/ |
|
83
|
1 |
|
private function getChildValueRecursive($item, $keys) |
|
84
|
|
|
{ |
|
85
|
1 |
|
$currentKey = current($keys); |
|
86
|
1 |
|
$remainingKeys = array_splice($keys, 1); |
|
87
|
1 |
|
if (method_exists($item, "get{$currentKey}")) { |
|
88
|
1 |
|
$method = "get{$currentKey}"; |
|
89
|
1 |
|
} elseif (method_exists($item, $currentKey)) { |
|
90
|
|
|
$method = $currentKey; |
|
91
|
1 |
|
} elseif (method_exists($item, 'get')) { |
|
92
|
1 |
|
$value = $item->get($currentKey); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
if (!isset($value) && isset($method)) { |
|
96
|
1 |
|
$value = call_user_func([$item, 'get'.current($keys)]); |
|
97
|
1 |
|
} elseif (!isset($value) && property_exists($item, $currentKey)) { |
|
98
|
|
|
$value = $item->{$currentKey}; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
if (isset($value)) { |
|
102
|
1 |
|
if (count($remainingKeys)) { |
|
103
|
1 |
|
return $this->getChildValueRecursive($value, $remainingKeys); |
|
104
|
|
|
} else { |
|
105
|
1 |
|
return $value; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $offset |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function offsetExists($offset) |
|
116
|
|
|
{ |
|
117
|
|
|
return isset($this->elements[$offset]) || array_key_exists($offset, $this->elements); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $offset |
|
122
|
|
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
public function offsetGet($offset) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->get($offset); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $offset |
|
132
|
|
|
* @param mixed $value |
|
133
|
|
|
*/ |
|
134
|
|
|
public function offsetSet($offset, $value) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->set($offset, $value); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $offset |
|
141
|
|
|
* |
|
142
|
|
|
* @return null |
|
143
|
|
|
*/ |
|
144
|
|
|
public function offsetUnset($offset) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->remove($offset); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return int |
|
151
|
|
|
*/ |
|
152
|
5 |
|
public function count() |
|
153
|
|
|
{ |
|
154
|
5 |
|
return count($this->elements); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return ArrayIterator |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function getIterator() |
|
161
|
|
|
{ |
|
162
|
1 |
|
return new ArrayIterator($this->elements); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $key |
|
167
|
|
|
* |
|
168
|
|
|
* @return null|mixed |
|
169
|
|
|
*/ |
|
170
|
2 |
|
public function get($key) |
|
171
|
|
|
{ |
|
172
|
2 |
|
if (isset($this->elements[$key])) { |
|
173
|
2 |
|
return $this->elements[$key]; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $key |
|
179
|
|
|
* @param mixed $value |
|
180
|
|
|
*/ |
|
181
|
|
|
public function set($key, $value) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->elements[$key] = $value; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param mixed $value |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
8 |
|
public function add($value) |
|
192
|
|
|
{ |
|
193
|
8 |
|
$this->elements[] = $value; |
|
194
|
|
|
|
|
195
|
8 |
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $key |
|
200
|
|
|
* |
|
201
|
|
|
* @return null|mixed |
|
202
|
|
|
*/ |
|
203
|
|
|
public function remove($key) |
|
204
|
|
|
{ |
|
205
|
|
|
if (isset($this->elements[$key]) || array_key_exists($key, $this->elements)) { |
|
206
|
|
|
$removed = $this->elements[$key]; |
|
207
|
|
|
unset($this->elements[$key]); |
|
208
|
|
|
|
|
209
|
|
|
return $removed; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|