1 | <?php |
||
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 | 31 | public function __construct($items = null) |
|
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) |
|
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) |
|
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 | 30 | public function push($item) |
|
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) |
|
93 | |||
94 | /** |
||
95 | * Get and remove the first item from the collection. |
||
96 | * @return mixed |
||
97 | */ |
||
98 | 1 | public function shift() |
|
102 | |||
103 | /** |
||
104 | * Pop an item off the end of the collection. |
||
105 | * @return Collection |
||
106 | */ |
||
107 | 2 | public function pop() |
|
112 | |||
113 | /** |
||
114 | * Pull an item from the collection and remove it from the collection. |
||
115 | * @param string $key |
||
116 | * @return mixed|false |
||
117 | */ |
||
118 | 1 | public function pull($key) |
|
127 | |||
128 | /** |
||
129 | * Removes an item from the collection. |
||
130 | * @param string $key |
||
131 | * @return \Ballen\Collection\Collection |
||
132 | */ |
||
133 | 3 | public function remove($key) |
|
141 | |||
142 | /** |
||
143 | * Get all items from the collection. |
||
144 | * @return CollectionExport |
||
145 | */ |
||
146 | 10 | public function all() |
|
150 | |||
151 | /** |
||
152 | * Checks if the collection has a the specified key set. |
||
153 | * @param string $key The key name to check if it exists. |
||
154 | * @return boolean |
||
155 | */ |
||
156 | 7 | public function has($key) |
|
160 | |||
161 | /** |
||
162 | * Get a specific item from the collection. |
||
163 | * @param string $key The collection (array) key to return. |
||
164 | * @param string $default Optional default value if the key doesn't exist (defaulted to false) |
||
165 | * @return string |
||
166 | */ |
||
167 | 3 | public function get($key, $default = false) |
|
174 | |||
175 | /** |
||
176 | * The total number of items in the collection. |
||
177 | * @return int |
||
178 | */ |
||
179 | 7 | public function count() |
|
183 | |||
184 | /** |
||
185 | * Iterate over each of the items in the collection and execute the callback. |
||
186 | * @param callable $callback |
||
187 | * @return Collection |
||
188 | */ |
||
189 | 2 | public function each(callable $callback) |
|
198 | |||
199 | /** |
||
200 | * Retrieve a random item from the collection. |
||
201 | * @return mixed |
||
202 | */ |
||
203 | 1 | public function random() |
|
207 | |||
208 | /** |
||
209 | * Return the first item in the collection. |
||
210 | * @return mixed |
||
211 | */ |
||
212 | 1 | public function first() |
|
216 | |||
217 | /** |
||
218 | * Returns the last item in the collection. |
||
219 | * @return mixed |
||
220 | */ |
||
221 | 1 | public function last() |
|
225 | |||
226 | /** |
||
227 | * Shuffles (randomises) the items in the collection. |
||
228 | * @return Collection |
||
229 | */ |
||
230 | 1 | public function shuffle() |
|
235 | |||
236 | /** |
||
237 | * Converts the colletion into a string. |
||
238 | * @return string |
||
239 | */ |
||
240 | 1 | public function implode($glue = ' ') |
|
244 | |||
245 | /** |
||
246 | * Checks to see if the collection is empty. |
||
247 | * @return boolean |
||
248 | */ |
||
249 | 2 | public function isEmpty() |
|
253 | |||
254 | /** |
||
255 | * Get an iterator for the collection. |
||
256 | * @return \ArrayIterator |
||
257 | */ |
||
258 | 1 | public function getIterator() |
|
262 | |||
263 | /** |
||
264 | * Converts the collection to it's string representation (JSON) |
||
265 | * @return string |
||
266 | */ |
||
267 | 1 | public function __toString() |
|
271 | |||
272 | /** |
||
273 | * Converts the collection to JSON. |
||
274 | * @return string |
||
275 | */ |
||
276 | 1 | public function toJson() |
|
280 | } |
||
281 |