|
1
|
|
|
<?php |
|
2
|
|
|
namespace Suricate; |
|
3
|
|
|
|
|
4
|
|
|
class Collection implements \IteratorAggregate, \Countable, \ArrayAccess, Interfaces\ICollection |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
protected $items = array(); |
|
8
|
|
|
protected $mapping = array(); // to be deprecated ? |
|
9
|
|
|
|
|
10
|
|
|
public $pagination = array( |
|
11
|
|
|
'nbPages' => 0, |
|
12
|
|
|
'page' => 1, |
|
13
|
|
|
'nbItems' => 0, |
|
14
|
|
|
); |
|
15
|
|
|
|
|
16
|
|
|
//protected $iteratorPosition = 0; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
17 |
|
public function __construct($items = array()) |
|
19
|
|
|
{ |
|
20
|
17 |
|
$this->items = $items; |
|
21
|
17 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function paginate($nbItemPerPage, $currentPage = 1) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->pagination['page'] = $currentPage; |
|
26
|
|
|
$this->pagination['nbItems'] = count($this->items); |
|
27
|
|
|
$this->pagination['nbPages'] = ceil($this->pagination['nbItems'] / $nbItemPerPage); |
|
28
|
|
|
|
|
29
|
|
|
$this->items = array_slice($this->items, ($currentPage - 1) * $nbItemPerPage, $nbItemPerPage); |
|
30
|
|
|
|
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getPossibleValuesFor($args, $key = null) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!is_array($args)) { |
|
37
|
|
|
$args = array('format' => '%s', 'data' => array($args)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$values = array(); |
|
41
|
|
|
foreach ($this->items as $itemKey => $item) { |
|
42
|
|
|
$itemValues = array(); |
|
43
|
|
|
foreach ($args['data'] as $arg) { |
|
44
|
|
|
$itemValues[] = dataGet($item, $arg); |
|
45
|
|
|
} |
|
46
|
|
|
$arrayKey = ($key !== null) ? dataGet($item, $key) : null; |
|
47
|
|
|
$values[$arrayKey] = vsprintf($args['format'], $itemValues); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $values; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getValuesFor($name) |
|
54
|
|
|
{ |
|
55
|
|
|
$values = array(); |
|
56
|
|
|
foreach ($this->items as $item) { |
|
57
|
|
|
$values[] = dataGet($item, $name); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $values; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
10 |
|
public function getItems() |
|
64
|
|
|
{ |
|
65
|
10 |
|
return $this->items; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/*public function addItemLink($linkId) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$this->items[$this->itemOffset] = $linkId; |
|
71
|
|
|
// add mapping between item->index and $position in items pool |
|
72
|
|
|
$this->mapping[$this->itemOffset] = $linkId; |
|
73
|
|
|
|
|
74
|
|
|
$this->itemOffset++; |
|
75
|
|
|
}*/ |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
public function getItemFromKey($key) |
|
80
|
|
|
{ |
|
81
|
|
|
$invertedMapping = array_flip($this->mapping); |
|
82
|
|
|
if (isset($invertedMapping[$key])) { |
|
83
|
|
|
return $this->items[$invertedMapping[$key]]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
// Implementation of Countable Interface |
|
89
|
1 |
|
public function count() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return count($this->items); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Implementation of IteratorAggregate Interface |
|
95
|
|
|
public function getIterator() |
|
96
|
|
|
{ |
|
97
|
|
|
return new \ArrayIterator($this->items); |
|
98
|
|
|
} |
|
99
|
|
|
/*public function current() |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
return $this->offsetGet($this->iteratorPosition); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function next() |
|
105
|
|
|
{ |
|
106
|
|
|
++$this->iteratorPosition; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function key() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->iteratorPosition; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function valid() |
|
115
|
|
|
{ |
|
116
|
|
|
return isset($this->items[$this->iteratorPosition]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function rewind() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->iteratorPosition = 0; |
|
122
|
|
|
}*/ |
|
123
|
|
|
|
|
124
|
|
|
// Implementation of ArrayAccess Interface |
|
125
|
1 |
|
public function offsetExists($offset) |
|
126
|
|
|
{ |
|
127
|
1 |
|
return isset($this->items[$offset]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function offsetGet($offset) |
|
131
|
|
|
{ |
|
132
|
|
|
$item =isset($this->items[$offset]) ? $this->items[$offset] : null; |
|
133
|
|
|
if (gettype($item) == 'object' || $item == null) { |
|
134
|
|
|
return $item; |
|
135
|
|
|
} else { |
|
136
|
|
|
// Lazy load |
|
137
|
|
|
$itemType = $this::ITEM_TYPE; |
|
138
|
|
|
$itemToLoad = new $itemType; |
|
139
|
|
|
$itemToLoad->load($this->items[$offset]); |
|
140
|
|
|
|
|
141
|
|
|
$this->items[$offset] = $itemToLoad; |
|
142
|
|
|
|
|
143
|
|
|
return $this->items[$offset]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function offsetSet($offset, $value) |
|
148
|
|
|
{ |
|
149
|
|
|
if (is_null($offset)) { |
|
150
|
|
|
$this->items[] = $value; |
|
151
|
|
|
} else { |
|
152
|
|
|
$this->items[$offset] = $value; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function offsetUnset($offset) |
|
157
|
|
|
{ |
|
158
|
|
|
unset($this->items[$offset]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
private function cleanStr($str) |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
|
|
164
|
|
|
$str = mb_strtolower($str, 'utf-8'); |
|
165
|
|
|
$str = strtr( |
|
166
|
|
|
$str, |
|
167
|
|
|
array( |
|
168
|
|
|
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'a'=>'a', 'a'=>'a', 'a'=>'a', 'ç'=>'c', 'c'=>'c', 'c'=>'c', 'c'=>'c', 'c'=>'c', 'd'=>'d', 'd'=>'d', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'g'=>'g', 'g'=>'g', 'g'=>'g', 'h'=>'h', 'h'=>'h', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', '?'=>'i', 'j'=>'j', 'k'=>'k', '?'=>'k', 'l'=>'l', 'l'=>'l', 'l'=>'l', '?'=>'l', 'l'=>'l', 'ñ'=>'n', 'n'=>'n', 'n'=>'n', 'n'=>'n', '?'=>'n', '?'=>'n', 'ð'=>'o', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'o'=>'o', 'o'=>'o', 'o'=>'o', 'œ'=>'o', 'ø'=>'o', 'r'=>'r', 'r'=>'r', 's'=>'s', 's'=>'s', 's'=>'s', 'š'=>'s', '?'=>'s', 't'=>'t', 't'=>'t', 't'=>'t', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'w'=>'w', 'ý'=>'y', 'ÿ'=>'y', 'y'=>'y', 'z'=>'z', 'z'=>'z', 'ž'=>'z' |
|
169
|
|
|
) |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
return $str; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// to be deprecated |
|
176
|
|
|
public function getFirstItem() |
|
177
|
|
|
{ |
|
178
|
|
|
foreach ($this->items as $currentItem) { |
|
179
|
|
|
return $currentItem; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// to be deprecated |
|
184
|
|
|
public function getRandom($nb = 1) |
|
185
|
|
|
{ |
|
186
|
|
|
$keys = (array) array_rand($this->items, $nb); |
|
187
|
|
|
$result = array(); |
|
188
|
|
|
foreach ($keys as $currentKey) { |
|
189
|
|
|
$result[$currentKey] = $this->items[$currentKey]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $result; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// Helpers |
|
196
|
1 |
|
public function first() |
|
197
|
|
|
{ |
|
198
|
1 |
|
foreach ($this->items as $currentItem) { |
|
199
|
1 |
|
return $currentItem; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
public function last() |
|
204
|
|
|
{ |
|
205
|
1 |
|
if (count($this->items)) { |
|
206
|
1 |
|
return end($this->items); |
|
207
|
|
|
} else { |
|
208
|
1 |
|
return null; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
public function isEmpty() |
|
213
|
|
|
{ |
|
214
|
1 |
|
return empty($this->items); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
1 |
|
public function sum($field = null) |
|
218
|
|
|
{ |
|
219
|
1 |
|
if ($field == null) { |
|
220
|
1 |
|
return array_sum($this->items); |
|
221
|
|
|
} |
|
222
|
|
|
$result = 0; |
|
223
|
|
|
foreach ($this->items as $item) { |
|
224
|
|
|
$result += dataGet($item, $field); |
|
225
|
|
|
} |
|
226
|
|
|
return $result; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function random($nbItems = 1) |
|
230
|
|
|
{ |
|
231
|
|
|
if ($this->isEmpty()) { |
|
232
|
|
|
return null; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$keys = array_rand($this->items, $nbItems); |
|
236
|
|
|
|
|
237
|
|
|
if (is_array($keys)) { |
|
238
|
|
|
return array_intersect_key($this->items, array_flip($keys)); |
|
239
|
|
|
} else { |
|
240
|
|
|
return $this->items[$keys]; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function shuffle() |
|
245
|
|
|
{ |
|
246
|
|
|
shuffle($this->items); |
|
247
|
|
|
|
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
1 |
|
public function unique() |
|
252
|
|
|
{ |
|
253
|
1 |
|
return new static(array_unique($this->items)); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function each(\Closure $callback) |
|
257
|
|
|
{ |
|
258
|
|
|
array_map($callback, $this->items); |
|
259
|
|
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function sort(\Closure $closure) |
|
263
|
|
|
{ |
|
264
|
|
|
uasort($this->items, $closure); |
|
265
|
|
|
|
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function sortBy($field, $reverse = false) |
|
270
|
|
|
{ |
|
271
|
|
|
if ($reverse) { |
|
272
|
|
View Code Duplication |
$sortFunction = function ($a, $b) use ($field) { |
|
|
|
|
|
|
273
|
|
|
$first = dataGet($a, $field); |
|
274
|
|
|
$second = dataGet($b, $field); |
|
275
|
|
|
if ($first == $second) { |
|
276
|
|
|
return 0; |
|
277
|
|
|
} |
|
278
|
|
|
return ($first > $second) ? -1 : 1; |
|
279
|
|
|
}; |
|
280
|
|
|
} else { |
|
281
|
|
View Code Duplication |
$sortFunction = function ($a, $b) use ($field) { |
|
|
|
|
|
|
282
|
|
|
$first = dataGet($a, $field); |
|
283
|
|
|
$second = dataGet($b, $field); |
|
284
|
|
|
if ($first == $second) { |
|
285
|
|
|
return 0; |
|
286
|
|
|
} |
|
287
|
|
|
return ($first < $second) ? -1 : 1; |
|
288
|
|
|
}; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
usort($this->items, $sortFunction); |
|
293
|
|
|
|
|
294
|
|
|
return $this; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
1 |
|
public function filter(\Closure $closure) |
|
298
|
|
|
{ |
|
299
|
1 |
|
return new static(array_filter($this->items, $closure)); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
public function search($value, $strict = false) |
|
303
|
|
|
{ |
|
304
|
|
|
return array_search($value, $this->items, $strict); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
1 |
|
public function has($key) |
|
308
|
|
|
{ |
|
309
|
1 |
|
return $this->offsetExists($key); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
1 |
|
public function keys() |
|
313
|
|
|
{ |
|
314
|
1 |
|
return array_keys($this->items); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
1 |
|
public function prepend($item) |
|
318
|
|
|
{ |
|
319
|
1 |
|
array_unshift($this->items, $item); |
|
320
|
|
|
|
|
321
|
1 |
|
return $this; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
1 |
|
public function push($item) |
|
325
|
|
|
{ |
|
326
|
1 |
|
$this->items[] = $item; |
|
327
|
|
|
|
|
328
|
1 |
|
return $this; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
1 |
|
public function put($key, $val) |
|
332
|
|
|
{ |
|
333
|
1 |
|
$this->items[$key] = $val; |
|
334
|
|
|
|
|
335
|
1 |
|
return $this; |
|
336
|
|
|
} |
|
337
|
1 |
|
public function shift() |
|
338
|
|
|
{ |
|
339
|
1 |
|
return array_shift($this->items); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
1 |
|
public function pop() |
|
343
|
|
|
{ |
|
344
|
1 |
|
return array_pop($this->items); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
1 |
|
public function reverse() |
|
348
|
|
|
{ |
|
349
|
1 |
|
return new static(array_reverse($this->items)); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
public function reduce(callable $callback, $initial = null) |
|
353
|
|
|
{ |
|
354
|
|
|
return array_reduce($this->items, $callback, $initial); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
1 |
|
public function slice($offset, $length = null, $preserveKeys = false) |
|
358
|
|
|
{ |
|
359
|
1 |
|
return new static(array_slice($this->items, $offset, $length, $preserveKeys)); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
1 |
|
public function take($limit = null) |
|
363
|
|
|
{ |
|
364
|
1 |
|
if ($limit < 0) { |
|
365
|
|
|
return $this->slice(abs($limit), $limit); |
|
366
|
|
|
} else { |
|
367
|
1 |
|
return $this->slice(0, $limit); |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
public function splice($offset, $length = null, $replacement = array()) |
|
372
|
|
|
{ |
|
373
|
|
|
return new static(array_splice($this->items, $offset, $length, $replacement)); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
public function chunk($size, $preserveKeys = false) |
|
377
|
|
|
{ |
|
378
|
|
|
$result = new static; |
|
379
|
|
|
foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) { |
|
380
|
|
|
$result->push(new static($chunk)); |
|
381
|
|
|
} |
|
382
|
|
|
return $result; |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.