1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Collections; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Comparable\Comparator; |
14
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
15
|
|
|
use Cubiche\Core\Specification\Criteria; |
16
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
17
|
|
|
use Cubiche\Core\Collections\DataSource\ArrayDataSource; |
18
|
|
|
use Cubiche\Core\Collections\Exception\InvalidKeyException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Array Collection Class. |
22
|
|
|
* |
23
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
24
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ArrayCollection implements ArrayCollectionInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $items; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $items |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $items = array()) |
37
|
|
|
{ |
38
|
|
|
$this->items = $items; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function add($item) |
45
|
|
|
{ |
46
|
|
|
$this->items[] = $item; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function addAll($items) |
53
|
|
|
{ |
54
|
|
|
foreach ($items as $item) { |
55
|
|
|
$this->add($item); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function remove($item) |
63
|
|
|
{ |
64
|
|
|
$criteria = Criteria::eq($item); |
65
|
|
|
foreach ($this->items as $key => $value) { |
66
|
|
|
if ($criteria->evaluate($value)) { |
67
|
|
|
unset($this->items[$key]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function removeAt($key) |
76
|
|
|
{ |
77
|
|
|
if ($this->containsKey($key)) { |
78
|
|
|
unset($this->items[$key]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function clear() |
86
|
|
|
{ |
87
|
|
|
$this->items = array(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function contains($item) |
94
|
|
|
{ |
95
|
|
|
return \in_array($item, $this->items, true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function containsKey($key) |
102
|
|
|
{ |
103
|
|
|
$this->validateKey($key); |
104
|
|
|
|
105
|
|
|
return isset($this->items[$key]) || \array_key_exists($key, $this->items); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function get($key) |
112
|
|
|
{ |
113
|
|
|
$this->validateKey($key); |
114
|
|
|
|
115
|
|
|
return isset($this->items[$key]) ? $this->items[$key] : null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function set($key, $value) |
122
|
|
|
{ |
123
|
|
|
$this->validateKey($key); |
124
|
|
|
|
125
|
|
|
$this->items[$key] = $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function count() |
132
|
|
|
{ |
133
|
|
|
return \count($this->items); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function getIterator() |
140
|
|
|
{ |
141
|
|
|
return new \ArrayIterator($this->items); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function slice($offset, $length = null) |
148
|
|
|
{ |
149
|
|
|
return new self(\array_slice($this->items, $offset, $length, true)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function find(SpecificationInterface $criteria) |
156
|
|
|
{ |
157
|
|
|
return new DataSourceCollection(new ArrayDataSource($this->items, $criteria)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function findOne(SpecificationInterface $criteria) |
164
|
|
|
{ |
165
|
|
|
return (new ArrayDataSource($this->items, $criteria))->findOne(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function toArray() |
172
|
|
|
{ |
173
|
|
|
return $this->items; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function sort(ComparatorInterface $criteria = null) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
if ($criteria === null) { |
182
|
|
|
$criteria = new Comparator(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
uasort($this->items, function ($a, $b) use ($criteria) { |
186
|
|
|
return $criteria->compare($a, $b); |
187
|
|
|
}); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function sortByKey(ComparatorInterface $criteria = null) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
if ($criteria === null) { |
196
|
|
|
$criteria = new Comparator(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
uksort($this->items, function ($a, $b) use ($criteria) { |
200
|
|
|
return $criteria->compare($a, $b); |
201
|
|
|
}); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function sorted(ComparatorInterface $criteria) |
208
|
|
|
{ |
209
|
|
|
return new DataSourceCollection(new ArrayDataSource($this->items, null, $criteria)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
|
|
public function keys() |
216
|
|
|
{ |
217
|
|
|
return new self(\array_keys($this->items)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function values() |
224
|
|
|
{ |
225
|
|
|
return new self(\array_values($this->items)); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
|
|
public function offsetExists($offset) |
232
|
|
|
{ |
233
|
|
|
return $this->containsKey($offset); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritdoc} |
238
|
|
|
*/ |
239
|
|
|
public function offsetGet($offset) |
240
|
|
|
{ |
241
|
|
|
return $this->get($offset); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
247
|
|
|
public function offsetSet($offset, $value) |
248
|
|
|
{ |
249
|
|
|
if (!isset($offset)) { |
250
|
|
|
return $this->add($value); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->set($offset, $value); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function offsetUnset($offset) |
260
|
|
|
{ |
261
|
|
|
unset($this->items[$offset]); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Validates that a key is valid. |
266
|
|
|
* |
267
|
|
|
* @param int|string $key |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
* |
271
|
|
|
* @throws InvalidKeyException If the key is invalid. |
272
|
|
|
*/ |
273
|
|
|
protected function validateKey($key) |
274
|
|
|
{ |
275
|
|
|
if (!is_string($key) && !is_int($key)) { |
276
|
|
|
throw InvalidKeyException::forKey($key); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return true; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.