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