1 | <?php |
||
13 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
14 | { |
||
15 | /** @var array */ |
||
16 | protected $items; |
||
17 | |||
18 | /** |
||
19 | * Collection constructor. |
||
20 | * |
||
21 | * @param array $items |
||
22 | */ |
||
23 | 22 | public function __construct(array $items = []) |
|
28 | |||
29 | /** |
||
30 | * Generate a collection from an array of items. |
||
31 | * I created this method so that it's possible to extend a collection more easily. |
||
32 | * |
||
33 | * @param array $items |
||
34 | * |
||
35 | * @return Collection |
||
36 | */ |
||
37 | 2 | public static function factory(array $items = []) |
|
41 | |||
42 | /** |
||
43 | * Get collection as an array |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | 7 | public function toArray() |
|
51 | |||
52 | /** |
||
53 | * Determine if collection has a given key |
||
54 | * |
||
55 | * @param mixed $key The key to look for |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | 11 | public function has($key) |
|
63 | |||
64 | /** |
||
65 | * Get item by key, with an optional default return value |
||
66 | * |
||
67 | * @param mixed $key |
||
68 | * @param mixed $default |
||
69 | * |
||
70 | * @return mixed |
||
71 | */ |
||
72 | 5 | public function get($key, $default = null) |
|
80 | |||
81 | /** |
||
82 | * Add an item with no regard to key |
||
83 | * |
||
84 | * @param mixed $value |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | 3 | public function add($value) |
|
94 | |||
95 | /** |
||
96 | * Set an item at a given key |
||
97 | * |
||
98 | * @param mixed $key |
||
99 | * @param mixed $value |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | 2 | public function set($key, $value) |
|
109 | |||
110 | /** |
||
111 | * Delete an item by key |
||
112 | * |
||
113 | * @param mixed $key |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | 3 | public function delete($key) |
|
123 | |||
124 | /** |
||
125 | * Clear the collection of all its items. |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | 1 | public function clear() |
|
135 | |||
136 | /** |
||
137 | * Determine if collection contains given value |
||
138 | * |
||
139 | * @param mixed $val |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | 1 | public function contains($val) |
|
147 | |||
148 | /** |
||
149 | * Fetch item from collection by key and remove it from collection |
||
150 | * |
||
151 | * @param mixed $key |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | 1 | public function pull($key) |
|
163 | |||
164 | /** |
||
165 | * Join collection items using a delimiter |
||
166 | * |
||
167 | * @param string $delim |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 1 | public function join($delim = '') |
|
175 | |||
176 | /** |
||
177 | * Determine if collection has any items |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 3 | public function isEmpty() |
|
185 | |||
186 | |||
187 | |||
188 | /** ++++ ++++ **/ |
||
189 | /** ++ Interface Compliance ++ **/ |
||
190 | /** ++++ ++++ **/ |
||
191 | |||
192 | /** |
||
193 | * @return array |
||
194 | */ |
||
195 | 1 | public function jsonSerialize() |
|
196 | { |
||
197 | 1 | return $this->toArray(); |
|
198 | } |
||
199 | |||
200 | /** ++++ ++++ **/ |
||
201 | /** ++ Array Access Methods ++ **/ |
||
202 | /** ++++ ++++ **/ |
||
203 | |||
204 | /** |
||
205 | * {@inheritDoc} |
||
206 | */ |
||
207 | 1 | public function offsetExists($offset) |
|
211 | |||
212 | /** |
||
213 | * {@inheritDoc} |
||
214 | */ |
||
215 | 2 | public function offsetGet($offset) |
|
223 | |||
224 | /** |
||
225 | * {@inheritDoc} |
||
226 | */ |
||
227 | 1 | public function offsetUnset($offset) |
|
231 | |||
232 | /** |
||
233 | * {@inheritDoc} |
||
234 | */ |
||
235 | 1 | public function offsetSet($offset, $value) |
|
243 | |||
244 | /** ++++ ++++ **/ |
||
245 | /** ++ Iterator Methods ++ **/ |
||
246 | /** ++++ ++++ **/ |
||
247 | |||
248 | /** |
||
249 | * {@inheritDoc} |
||
250 | */ |
||
251 | 1 | public function current() |
|
255 | |||
256 | /** |
||
257 | * {@inheritDoc} |
||
258 | */ |
||
259 | 1 | public function key() |
|
263 | |||
264 | /** |
||
265 | * {@inheritDoc} |
||
266 | */ |
||
267 | 1 | public function next() |
|
271 | |||
272 | /** |
||
273 | * {@inheritDoc} |
||
274 | */ |
||
275 | 22 | public function rewind() |
|
279 | |||
280 | /** |
||
281 | * {@inheritDoc} |
||
282 | */ |
||
283 | 1 | public function valid() |
|
287 | |||
288 | /** ++++ ++++ **/ |
||
289 | /** ++ Countable Method ++ **/ |
||
290 | /** ++++ ++++ **/ |
||
291 | |||
292 | /** |
||
293 | * {@inheritDoc} |
||
294 | */ |
||
295 | 4 | public function count() |
|
299 | } |