1 | <?php |
||
2 | |||
3 | namespace Elgg\Collections; |
||
4 | |||
5 | use ArrayAccess; |
||
6 | use Countable; |
||
7 | use InvalidArgumentException; |
||
8 | use InvalidParameterException; |
||
9 | use SeekableIterator; |
||
10 | |||
11 | /** |
||
12 | * A collection of unique items |
||
13 | */ |
||
14 | class Collection implements CollectionInterface, |
||
15 | ArrayAccess, |
||
16 | SeekableIterator, |
||
17 | Countable { |
||
18 | |||
19 | /** |
||
20 | * @var CollectionItemInterface[] |
||
21 | */ |
||
22 | protected $items = []; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $item_class; |
||
28 | |||
29 | /** |
||
30 | * Constructor |
||
31 | * |
||
32 | * @param CollectionItemInterface[] $items Items |
||
33 | * @param string $item_class Member class |
||
34 | * Restrict members of the collection to instances of this class |
||
35 | */ |
||
36 | 144 | public function __construct($items = [], $item_class = null) { |
|
37 | 144 | if ($item_class) { |
|
38 | 128 | if (!is_subclass_of($item_class, CollectionItemInterface::class)) { |
|
39 | 1 | throw new InvalidArgumentException('Item class must implement ' . CollectionItemInterface::class); |
|
40 | } |
||
41 | |||
42 | 127 | $this->item_class = $item_class; |
|
43 | } |
||
44 | |||
45 | 143 | foreach ($items as $item) { |
|
46 | 46 | $this->add($item); |
|
47 | } |
||
48 | 142 | } |
|
49 | |||
50 | /** |
||
51 | * Validate if item is a valid collection item |
||
52 | * |
||
53 | * @param mixed $item Item |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | 105 | protected function assertValidItem($item) { |
|
58 | 105 | $class = $this->item_class ? : CollectionItemInterface::class; |
|
59 | |||
60 | 105 | if (!$item instanceof $class) { |
|
61 | 1 | throw new InvalidParameterException('Collection ' . __CLASS__ . ' only accepts instances of ' . $class); |
|
62 | } |
||
63 | 105 | } |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 81 | public function all() { |
|
69 | 81 | return $this->items; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 5 | public function count() { |
|
76 | 5 | return count($this->items); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 93 | public function add($item) { |
|
83 | 93 | $this->assertValidItem($item); |
|
84 | |||
85 | 93 | $this->items[$item->getId()] = $item; |
|
86 | |||
87 | 93 | return $this; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 18 | public function get($id) { |
|
94 | 18 | return elgg_extract($id, $this->items); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 33 | public function has($id) { |
|
101 | 33 | return array_key_exists($id, $this->items); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 3 | public function remove($id) { |
|
108 | 3 | unset($this->items[$id]); |
|
109 | 3 | } |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 34 | public function fill($items) { |
|
115 | 34 | $this->items = []; |
|
116 | 34 | foreach ($items as $item) { |
|
117 | 32 | $this->add($item); |
|
118 | } |
||
119 | |||
120 | 34 | return $this; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 20 | public function merge($items) { |
|
127 | 20 | foreach ($items as $item) { |
|
128 | 20 | $this->add($item); |
|
129 | } |
||
130 | |||
131 | 20 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 61 | public function filter(callable $callback = null) { |
|
138 | 61 | if ($callback) { |
|
139 | 59 | $items = array_filter($this->items, $callback); |
|
140 | } else { |
||
141 | 60 | $items = array_values($this->items); |
|
142 | } |
||
143 | |||
144 | 61 | return new static($items, $this->item_class); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 60 | public function sort(callable $callback = null) { |
|
151 | 60 | if (!$callback) { |
|
152 | 1 | $callback = function (CollectionItemInterface $f1, CollectionItemInterface $f2) { |
|
153 | 1 | $p1 = $f1->getPriority() ? : 500; |
|
154 | 1 | $p2 = $f2->getPriority() ? : 500; |
|
155 | 1 | if ($p1 === $p2) { |
|
156 | return 0; |
||
157 | } |
||
158 | |||
159 | 1 | return $p1 < $p2 ? -1 : 1; |
|
160 | 1 | }; |
|
161 | } |
||
162 | |||
163 | 60 | uasort($this->items, $callback); |
|
164 | |||
165 | 60 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Walk through members of the collection and apply a callback |
||
170 | * |
||
171 | * Unlike CollectionInterface::map(), this method does not return the result of mapping, |
||
172 | * rather returns the exact same instance of the collection after walking through |
||
173 | * its members by reference |
||
174 | * |
||
175 | * @see CollectionInterface::map() |
||
176 | * |
||
177 | * @param callable $callback Callback function |
||
178 | * |
||
179 | * @return static |
||
180 | */ |
||
181 | 59 | public function walk(callable $callback) { |
|
182 | 59 | foreach ($this->items as $id => $item) { |
|
183 | 32 | call_user_func($callback, $item, $id); |
|
184 | } |
||
185 | |||
186 | 59 | return $this; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 59 | public function map(callable $callback) { |
|
193 | 59 | $map = []; |
|
194 | |||
195 | 59 | $items = $this->filter()->all(); |
|
196 | 59 | foreach ($items as $id => &$item) { |
|
197 | 32 | $map[$id] = call_user_func($callback, $item, $id); |
|
198 | } |
||
199 | |||
200 | 59 | return $map; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 6 | public function offsetExists($offset) { |
|
207 | 6 | return $this->has($offset); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 5 | public function offsetGet($offset) { |
|
214 | 5 | return $this->get($offset); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 35 | public function offsetSet($offset, $value) { |
|
221 | 35 | $this->assertValidItem($value); |
|
222 | |||
223 | 35 | $key = $value->getId(); |
|
224 | 35 | $this->items[$key] = $value; |
|
225 | 35 | } |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 1 | public function offsetUnset($offset) { |
|
231 | 1 | unset($this->items[$offset]); |
|
232 | 1 | } |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 36 | public function current() { |
|
238 | 36 | return current($this->items); |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | 36 | public function next() { |
|
245 | 36 | next($this->items); |
|
246 | 36 | } |
|
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | 2 | public function key() { |
|
252 | 2 | return key($this->items); |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | 61 | public function valid() { |
|
259 | 61 | return key($this->items) !== null; |
|
260 | } |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | 61 | public function rewind() { |
|
266 | 61 | reset($this->items); |
|
267 | 61 | } |
|
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function seek($position) { |
||
273 | $keys = array_keys($this->items); |
||
274 | |||
275 | if (isset($keys[$position])) { |
||
276 | throw new \OutOfBoundsException(); |
||
277 | } |
||
278 | |||
279 | $key = $keys[$position]; |
||
280 | |||
281 | return $this->items[$key]; |
||
0 ignored issues
–
show
|
|||
282 | } |
||
283 | |||
284 | } |
||
285 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: