1 | <?php namespace Ballen\Collection; |
||
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() |
|
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() |
|
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) |
|
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() |
|
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() |
|
204 | |||
205 | /** |
||
206 | * Return the first item in the collection. |
||
207 | * @return mixed |
||
208 | */ |
||
209 | 1 | public function first() |
|
213 | |||
214 | /** |
||
215 | * Returns the last item in the collection. |
||
216 | * @return mixed |
||
217 | */ |
||
218 | 1 | public function last() |
|
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 = ' ') |
|
241 | |||
242 | /** |
||
243 | * Checks to see if the collection is empty. |
||
244 | * @return boolean |
||
245 | */ |
||
246 | 2 | public function isEmpty() |
|
250 | |||
251 | /** |
||
252 | * Get an iterator for the collection. |
||
253 | * @return \ArrayIterator |
||
254 | */ |
||
255 | 1 | public function getIterator() |
|
259 | |||
260 | /** |
||
261 | * Converts the collection to it's string representation (JSON) |
||
262 | * @return string |
||
263 | */ |
||
264 | 1 | public function __toString() |
|
268 | } |
||
269 |