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
|
10 |
|
public function __construct($items = array()) |
19
|
|
|
{ |
20
|
10 |
|
$this->items = $items; |
21
|
10 |
|
} |
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
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getPossibleValuesFor($args, $key = null) |
36
|
|
|
{ |
37
|
|
|
if (!is_array($args)) { |
38
|
|
|
$args = array('format' => '%s', 'data' => array($args)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$values = array(); |
42
|
|
|
foreach ($this->items as $itemKey => $item) { |
43
|
|
|
$itemValues = array(); |
44
|
|
|
foreach ($args['data'] as $arg) { |
45
|
|
|
$itemValues[] = dataGet($item, $arg); |
46
|
|
|
} |
47
|
|
|
$arrayKey = ($key !== null) ? dataGet($item, $key) : null; |
48
|
|
|
$values[$arrayKey] = vsprintf($args['format'], $itemValues); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $values; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getValuesFor($name) |
55
|
|
|
{ |
56
|
|
|
$values = array(); |
57
|
|
|
foreach ($this->items as $item) { |
58
|
|
|
$values[] = dataGet($item, $name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $values; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
public function getItems() |
65
|
|
|
{ |
66
|
4 |
|
return $this->items; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/*public function addItemLink($linkId) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->items[$this->itemOffset] = $linkId; |
72
|
|
|
// add mapping between item->index and $position in items pool |
73
|
|
|
$this->mapping[$this->itemOffset] = $linkId; |
74
|
|
|
|
75
|
|
|
$this->itemOffset++; |
76
|
|
|
}*/ |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function getItemFromKey($key) |
81
|
|
|
{ |
82
|
|
|
$invertedMapping = array_flip($this->mapping); |
83
|
|
|
if (isset($invertedMapping[$key])) { |
84
|
|
|
return $this->items[$invertedMapping[$key]]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
// Implementation of Countable Interface |
90
|
1 |
|
public function count() |
91
|
|
|
{ |
92
|
1 |
|
return count($this->items); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Implementation of IteratorAggregate Interface |
96
|
|
|
public function getIterator() |
97
|
|
|
{ |
98
|
|
|
return new \ArrayIterator($this->items); |
99
|
|
|
} |
100
|
|
|
/*public function current() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return $this->offsetGet($this->iteratorPosition); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function next() |
106
|
|
|
{ |
107
|
|
|
++$this->iteratorPosition; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function key() |
111
|
|
|
{ |
112
|
|
|
return $this->iteratorPosition; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function valid() |
116
|
|
|
{ |
117
|
|
|
return isset($this->items[$this->iteratorPosition]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function rewind() |
121
|
|
|
{ |
122
|
|
|
$this->iteratorPosition = 0; |
123
|
|
|
}*/ |
124
|
|
|
|
125
|
|
|
// Implementation of ArrayAccess Interface |
126
|
1 |
|
public function offsetExists($offset) |
127
|
|
|
{ |
128
|
1 |
|
return isset($this->items[$offset]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function offsetGet($offset) |
132
|
|
|
{ |
133
|
|
|
$item =isset($this->items[$offset]) ? $this->items[$offset] : null; |
134
|
|
|
if (gettype($item) == 'object' || $item == null) { |
135
|
|
|
return $item; |
136
|
|
|
} else { |
137
|
|
|
// Lazy load |
138
|
|
|
$itemType = $this::ITEM_TYPE; |
139
|
|
|
$itemToLoad = new $itemType; |
140
|
|
|
$itemToLoad->load($this->items[$offset]); |
141
|
|
|
|
142
|
|
|
$this->items[$offset] = $itemToLoad; |
143
|
|
|
|
144
|
|
|
return $this->items[$offset]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function offsetSet($offset, $value) |
149
|
|
|
{ |
150
|
|
|
if (is_null($offset)) { |
151
|
|
|
$this->items[] = $value; |
152
|
|
|
} else { |
153
|
|
|
$this->items[$offset] = $value; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function offsetUnset($offset) |
158
|
|
|
{ |
159
|
|
|
unset($this->items[$offset]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function cleanStr($str) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
|
165
|
|
|
$str = mb_strtolower($str, 'utf-8'); |
166
|
|
|
$str = strtr( |
167
|
|
|
$str, |
168
|
|
|
array( |
169
|
|
|
'à'=>'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' |
170
|
|
|
) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
return $str; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// to be deprecated |
177
|
|
|
public function getFirstItem() |
178
|
|
|
{ |
179
|
|
|
foreach ($this->items as $currentItem) { |
180
|
|
|
return $currentItem; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// to be deprecated |
185
|
|
|
public function getRandom($nb = 1) |
186
|
|
|
{ |
187
|
|
|
$keys = (array) array_rand($this->items, $nb); |
188
|
|
|
$result = array(); |
189
|
|
|
foreach ($keys as $currentKey) { |
190
|
|
|
$result[$currentKey] = $this->items[$currentKey]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Helpers |
197
|
1 |
|
public function first() |
198
|
|
|
{ |
199
|
1 |
|
foreach ($this->items as $currentItem) { |
200
|
1 |
|
return $currentItem; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
public function last() |
205
|
|
|
{ |
206
|
1 |
|
if (count($this->items)) { |
207
|
1 |
|
return end($this->items); |
208
|
|
|
} else { |
209
|
1 |
|
return null; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
public function isEmpty() |
214
|
|
|
{ |
215
|
1 |
|
return empty($this->items); |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
public function sum($field = null) |
219
|
|
|
{ |
220
|
1 |
|
if ($field == null) { |
221
|
1 |
|
return array_sum($this->items); |
222
|
|
|
} |
223
|
|
|
$result = 0; |
224
|
|
|
foreach ($this->items as $item) { |
225
|
|
|
$result += dataGet($item, $field); |
226
|
|
|
} |
227
|
|
|
return $result; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function random($nbItems = 1) |
231
|
|
|
{ |
232
|
|
|
if ($this->isEmpty()) { |
233
|
|
|
return null; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$keys = array_rand($this->items, $nbItems); |
237
|
|
|
|
238
|
|
|
if (is_array($keys)) { |
239
|
|
|
return array_intersect_key($this->items, array_flip($keys)); |
240
|
|
|
} else { |
241
|
|
|
return $this->items[$keys]; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function shuffle() |
246
|
|
|
{ |
247
|
|
|
shuffle($this->items); |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
public function unique() |
253
|
|
|
{ |
254
|
1 |
|
return new static(array_unique($this->items)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function each(\Closure $callback) |
258
|
|
|
{ |
259
|
|
|
array_map($callback, $this->items); |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function sort(\Closure $closure) |
264
|
|
|
{ |
265
|
|
|
uasort($this->items, $closure); |
266
|
|
|
|
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function sortBy($field, $reverse = false) |
271
|
|
|
{ |
272
|
|
|
if ($reverse) { |
273
|
|
View Code Duplication |
$sortFunction = function($a, $b) use ($field) { |
|
|
|
|
274
|
|
|
$first = dataGet($a, $field); |
275
|
|
|
$second = dataGet($b, $field); |
276
|
|
|
if ($first == $second) { |
277
|
|
|
return 0; |
278
|
|
|
} |
279
|
|
|
return ($first > $second) ? -1 : 1; |
280
|
|
|
}; |
281
|
|
|
} else { |
282
|
|
View Code Duplication |
$sortFunction = function($a, $b) use($field) { |
|
|
|
|
283
|
|
|
$first = dataGet($a, $field); |
284
|
|
|
$second = dataGet($b, $field); |
285
|
|
|
if ($first == $second) { |
286
|
|
|
return 0; |
287
|
|
|
} |
288
|
|
|
return ($first < $second) ? -1 : 1; |
289
|
|
|
}; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
usort($this->items, $sortFunction); |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
1 |
|
public function filter(\Closure $closure) |
299
|
|
|
{ |
300
|
1 |
|
return new static(array_filter($this->items, $closure)); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function search($value, $strict = false) |
304
|
|
|
{ |
305
|
|
|
return array_search($value, $this->items, $strict); |
306
|
|
|
} |
307
|
|
|
|
308
|
1 |
|
public function has($key) |
309
|
|
|
{ |
310
|
1 |
|
return $this->offsetExists($key); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function keys() |
314
|
|
|
{ |
315
|
|
|
return array_keys($this->items); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function prepend($item) |
319
|
|
|
{ |
320
|
|
|
array_unshift($this->items, $item); |
321
|
|
|
|
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
1 |
|
public function push($item) |
326
|
|
|
{ |
327
|
1 |
|
$this->items[] = $item; |
328
|
|
|
|
329
|
1 |
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function put($key, $val) |
333
|
|
|
{ |
334
|
|
|
$this->items[$key] = $val; |
335
|
|
|
|
336
|
|
|
return $this; |
337
|
|
|
} |
338
|
|
|
public function shift() |
339
|
|
|
{ |
340
|
|
|
return array_shift($this->items); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function pop() |
344
|
|
|
{ |
345
|
|
|
return array_pop($this->items); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function reverse() |
349
|
|
|
{ |
350
|
|
|
return new static(array_reverse($this->items)); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function reduce(callable $callback, $initial = null) |
354
|
|
|
{ |
355
|
|
|
return array_reduce($this->items, $callback, $initial); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function slice($offset, $length = null, $preserveKeys = false) |
359
|
|
|
{ |
360
|
|
|
return new static(array_slice($this->items, $offset, $length, $preserveKeys)); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function take($limit = null) |
364
|
|
|
{ |
365
|
|
|
if ($limit < 0) { |
366
|
|
|
return $this->slice(abs($limit), $limit); |
367
|
|
|
} else { |
368
|
|
|
return $this->slice(0, $limit); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function splice($offset, $length = null, $replacement = array()) |
373
|
|
|
{ |
374
|
|
|
return new static(array_splice($this->items, $offset, $length, $replacement)); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function chunk($size, $preserveKeys = false) |
378
|
|
|
{ |
379
|
|
|
$result = new static; |
380
|
|
|
foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) |
381
|
|
|
{ |
382
|
|
|
$result->push(new static($chunk)); |
383
|
|
|
} |
384
|
|
|
return $result; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|