1
|
|
|
<?php namespace Ballen\Collection; |
2
|
|
|
|
3
|
|
|
use ArrayIterator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Collection |
7
|
|
|
* |
8
|
|
|
* A Collection class (library) which provides OOP replacement for the |
9
|
|
|
* traditional array data structure. |
10
|
|
|
* |
11
|
|
|
* @author Bobby Allen <[email protected]> |
12
|
|
|
* @license https://opensource.org/licenses/MIT |
13
|
|
|
* @link https://github.com/bobsta63/collection |
14
|
|
|
* @link http://www.bobbyallen.me |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class Collection |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The collection data. |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $items = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create new instance of a Collection. |
28
|
|
|
* @param array $items |
29
|
|
|
*/ |
30
|
32 |
|
public function __construct($items = null) |
31
|
|
|
{ |
32
|
32 |
|
if (!is_null($items) && is_array($items)) { |
33
|
31 |
|
$this->push($items); |
34
|
31 |
|
} |
35
|
32 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Resets the collection with the specified array content. |
39
|
|
|
* @param array $items |
40
|
|
|
* @return Collection |
41
|
|
|
*/ |
42
|
2 |
|
public function reset($items = null) |
43
|
|
|
{ |
44
|
2 |
|
if ((func_get_args() > 0) && is_array($items)) { |
45
|
1 |
|
$this->items = $items; |
46
|
1 |
|
} else { |
47
|
1 |
|
$this->items = []; |
48
|
|
|
} |
49
|
2 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set an item or items into the the collection. |
54
|
|
|
* @param mixed $key |
55
|
|
|
* @param mixed $item |
56
|
|
|
* @return Collection |
57
|
|
|
*/ |
58
|
1 |
|
public function put($key, $item) |
59
|
|
|
{ |
60
|
1 |
|
$this->items[$key] = $item; |
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Push a new item (or array of items) into the collection onto the end |
66
|
|
|
* of the collection. |
67
|
|
|
* @param mixed $item |
68
|
|
|
* @return Collection |
69
|
|
|
*/ |
70
|
31 |
|
public function push($item) |
71
|
|
|
{ |
72
|
31 |
|
if (!is_array($item)) { |
73
|
3 |
|
$this->items = array_merge($this->items, [$item]); |
74
|
3 |
|
} else { |
75
|
31 |
|
$this->items = array_merge($this->items, $item); |
76
|
|
|
} |
77
|
31 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Push an item (or array of items) onto the beginning of the collection. |
82
|
|
|
* @param mixed $item |
83
|
|
|
* @return Collection |
84
|
|
|
*/ |
85
|
1 |
|
public function prepend($item) |
86
|
|
|
{ |
87
|
1 |
|
array_unshift($this->items, $item); |
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get and remove the first item from the collection. |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
1 |
|
public function shift() |
96
|
|
|
{ |
97
|
1 |
|
return array_shift($this->items); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Pop an item off the end of the collection. |
102
|
|
|
* @return Collection |
103
|
|
|
*/ |
104
|
2 |
|
public function pop() |
105
|
|
|
{ |
106
|
2 |
|
array_pop($this->items); |
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Pull an item from the collection and remove it from the collection. |
112
|
|
|
* @param string $key |
113
|
|
|
* @return mixed|false |
114
|
|
|
*/ |
115
|
2 |
|
public function pull($key) |
116
|
|
|
{ |
117
|
2 |
|
if ($this->has($key)) { |
118
|
1 |
|
$pulled = $this->get($key); |
119
|
1 |
|
$this->remove($key); |
120
|
1 |
|
return $pulled; |
121
|
|
|
} |
122
|
1 |
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Removes an item from the collection. |
127
|
|
|
* @param string $key |
128
|
|
|
* @return \Ballen\Collection\Collection |
129
|
|
|
*/ |
130
|
3 |
|
public function remove($key) |
131
|
|
|
{ |
132
|
3 |
|
if ($this->has($key)) { |
133
|
3 |
|
$this->items[$key] = null; |
134
|
3 |
|
unset($this->items[$key]); |
135
|
3 |
|
} |
136
|
3 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get all items from the collection. |
141
|
|
|
* @return CollectionExport |
142
|
|
|
*/ |
143
|
11 |
|
public function all() |
144
|
|
|
{ |
145
|
11 |
|
return new CollectionExport($this->items); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Checks if the collection has a the specified key set. |
150
|
|
|
* @param string $key The key name to check if it exists. |
151
|
|
|
* @return boolean |
152
|
|
|
*/ |
153
|
8 |
|
public function has($key) |
154
|
|
|
{ |
155
|
8 |
|
return isset($this->items[$key]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get a specific item from the collection. |
160
|
|
|
* @param string $key The collection (array) key to return. |
161
|
|
|
* @param string $default Optional default value if the key doesn't exist (defaulted to false) |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
3 |
|
public function get($key, $default = false) |
165
|
|
|
{ |
166
|
3 |
|
if (!isset($this->items[$key])) { |
167
|
1 |
|
return $default; |
|
|
|
|
168
|
|
|
} |
169
|
2 |
|
return $this->items[$key]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* The total number of items in the collection. |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
7 |
|
public function count() |
177
|
|
|
{ |
178
|
7 |
|
return count($this->items); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Iterate over each of the items in the collection and execute the callback. |
183
|
|
|
* @param callable $callback |
184
|
|
|
* @return Collection |
185
|
|
|
*/ |
186
|
2 |
|
public function each(callable $callback) |
187
|
|
|
{ |
188
|
2 |
|
foreach ($this->items as $key => $item) { |
189
|
2 |
|
if ($callback($key, $item) === false) { |
190
|
1 |
|
break; |
191
|
|
|
} |
192
|
2 |
|
} |
193
|
2 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Retrieve a random item from the collection. |
198
|
|
|
* @return mixed |
199
|
|
|
*/ |
200
|
1 |
|
public function random() |
201
|
|
|
{ |
202
|
1 |
|
return array_rand($this->items); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return the first item in the collection. |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
1 |
|
public function first() |
210
|
|
|
{ |
211
|
1 |
|
return array_values($this->items)[0]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns the last item in the collection. |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
1 |
|
public function last() |
219
|
|
|
{ |
220
|
1 |
|
return array_values($this->items)[$this->count() - 1]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Shuffles (randomises) the items in the collection. |
225
|
|
|
* @return Collection |
226
|
|
|
*/ |
227
|
1 |
|
public function shuffle() |
228
|
|
|
{ |
229
|
1 |
|
shuffle($this->items); |
230
|
1 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Converts the colletion into a string. |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
1 |
|
public function implode($glue = ' ') |
238
|
|
|
{ |
239
|
1 |
|
return implode($glue, $this->items); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Checks to see if the collection is empty. |
244
|
|
|
* @return boolean |
245
|
|
|
*/ |
246
|
2 |
|
public function isEmpty() |
247
|
|
|
{ |
248
|
2 |
|
return empty($this->items); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get an iterator for the collection. |
253
|
|
|
* @return \ArrayIterator |
254
|
|
|
*/ |
255
|
1 |
|
public function getIterator() |
256
|
|
|
{ |
257
|
1 |
|
return new ArrayIterator($this->items); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Converts the collection to it's string representation (JSON) |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
1 |
|
public function __toString() |
265
|
|
|
{ |
266
|
1 |
|
return $this->all()->toJson(); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|