/
Collection.php
258 lines (232 loc) · 6.72 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Utility;
use ArrayIterator;
use Cake\Utility\Iterator\FilterIterator;
use Cake\Utility\Iterator\ReplaceIterator;
use InvalidArgumentException;
use IteratorIterator;
class Collection extends IteratorIterator {
public function __construct($items) {
if (is_array($items)) {
$items = new ArrayIterator($items);
}
if (!($items instanceof \Traversable)) {
throw new InvalidArgumentException;
}
parent::__construct($items);
}
public function getIterator() {
return $this->_iterator;
}
/**
* Executes the passed callable for each of the elements in this collection
* and passes both the value and key for them on each step.
* Returns the same collection for chaining.
*
* ###Example:
*
* {{{
* $collection = (new Collection($items))->each(function($value, $key) {
* echo "Element $key: $value";
* });
* }}}
*
* @param callable $c callable function that will receive each of the elements
* in this collection
* @return \Cake\Utility\Collection
*/
public function each(callable $c) {
foreach ($this as $k => $v) {
$c($v, $k);
}
return $this;
}
/**
* Looks through each value in the collection, and returns another collection with
* all the values that pass a truth test. Only the values for which the callback
* returns true will be present in the resulting collection.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration, the key of the element and this collection as
* arguments, in that order.
*
* ##Example:
*
* Filtering odd numbers in an array, at the end only the value 2 will
* be present in the resulting collection:
*
* {{{
* $collection = (new Collection([1, 2, 3]))->filter(function($value, $key) {
* return $value % 2 === 0;
* });
* }}}
*
* @param callable $c the method that will receive each of the elements and
* returns true whether or not they should be in the resulting collection.
* @return \Cake\Utility\Iterator\FilterIterator;
*/
public function filter(callable $c) {
return new FilterIterator($this, $c);
}
/**
* Looks through each value in the collection, and returns another collection with
* all the values that do not pass a truth test. This is the opposite of `filter`.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration, the key of the element and this collection as
* arguments, in that order.
*
* ##Example:
*
* Filtering even numbers in an array, at the end only values 1 and 3 will
* be present in the resulting collection:
*
* {{{
* $collection = (new Collection([1, 2, 3]))->filter(function($value, $key) {
* return $value % 2 === 0;
* });
* }}}
*
* @param callable $c the method that will receive each of the elements and
* returns true whether or not they should be out of the resulting collection.
* @return \Cake\Utility\Iterator\FilterIterator;
*/
public function reject(callable $c) {
return new FilterIterator($this, function ($key, $value, $items) use ($c) {
return !$c($key, $value, $items);
});
}
/**
* Returns true if all values in this collection pass the truth test provided
* in the callback.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration and the key of the element as arguments, in that
* order.
*
* ###Example:
*
* {{{
* $legalAge = (new Collection([24, 45, 60, 15]))->every(function($value, $key) {
* return $value >= 21;
* });
* }}}
*
* @param callable $c a callback function
* @return boolean true if for all elements in this collection the provided
* callback returns true, false otherwise
*/
public function every(callable $c) {
foreach ($this as $key => $value) {
if (!$c($value, $key)) {
return false;
}
}
return true;
}
/**
* Returns true if any of the values in this collection pass the truth test
* provided in the callback.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration and the key of the element as arguments, in that
* order.
*
* ###Example:
*
* {{{
* $hasUnderAge = (new Collection([24, 45, 15]))->every(function($value, $key) {
* return $value < 21;
* });
* }}}
*
* @param callable $c a callback function
* @return boolean true if for all elements in this collection the provided
* callback returns true, false otherwise
*/
public function some(callable $c) {
foreach ($this as $key => $value) {
if ($c($value, $key) === true) {
return true;
}
}
return false;
}
/**
* Returns true if $value is present in this collection. Comparisons are made
* both by value and type.
*
* @param mixed $value the value to check for
* @return boolean true if $value is present in this collection
*/
public function contains($value) {
foreach ($this as $v) {
if ($value === $v) {
return true;
}
}
return false;
}
/**
* Creates an iterator from another iterator that will modify each of the values
* by converting them using a callback function.
*/
/**
* Returns another collection after modifying each of the values in this one using
* the provided callable.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration, the key of the element and this collection as
* arguments, in that order.
*
* ##Example:
*
* Getting a collection of booleans where true indicates if a person is female:
*
* {{{
* $collection = (new Collection($people))->filter(function($person, $key) {
* return $person->sex === 'female';
* });
* }}}
*
* @param callable $c the method that will receive each of the elements and
* returns the new value for the key that is being iterated
* @return \Cake\Utility\Iterator\ReplaceIterator
*/
public function map(callable $c) {
return new ReplaceIterator($this, $c);
}
public function mapReduce(callable $map, callable $reduce) {
}
public function extract($property) {
}
public function max() {
}
public function min() {
}
public function sortBy($property) {
}
public function groupBy($property) {
}
public function indexBy($property) {
}
public function countBy($property) {
}
public function shuffle() {
}
public function sample($size) {
}
}