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 | 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 | public static function factory(array $items = []) |
||
41 | |||
42 | /** |
||
43 | * Get collection as an array |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | 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 | 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 | 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 | 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 | public function set($key, $value) |
||
109 | |||
110 | /** |
||
111 | * Delete an item by key |
||
112 | * |
||
113 | * @param mixed $key |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function delete($key) |
||
123 | |||
124 | /** |
||
125 | * Clear the collection of all its items. |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function clear() |
||
135 | |||
136 | /** |
||
137 | * Determine if collection contains given value |
||
138 | * |
||
139 | * @param mixed $val |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | 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 | public function pull($key) |
||
163 | |||
164 | /** |
||
165 | * Join collection items using a delimiter |
||
166 | * |
||
167 | * @param string $delim |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function join($delim = '') |
||
175 | |||
176 | /** |
||
177 | * Determine if collection has any items |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function isEmpty() |
||
185 | |||
186 | /** ++++ ++++ **/ |
||
187 | /** ++ Interface Compliance ++ **/ |
||
188 | /** ++++ ++++ **/ |
||
189 | |||
190 | /** |
||
191 | * @return array |
||
192 | */ |
||
193 | public function jsonSerialize() |
||
197 | |||
198 | /** ++++ ++++ **/ |
||
199 | /** ++ Array Access Methods ++ **/ |
||
200 | /** ++++ ++++ **/ |
||
201 | |||
202 | /** |
||
203 | * {@inheritDoc} |
||
204 | */ |
||
205 | public function offsetExists($offset) |
||
209 | |||
210 | /** |
||
211 | * {@inheritDoc} |
||
212 | */ |
||
213 | public function offsetGet($offset) |
||
221 | |||
222 | /** |
||
223 | * {@inheritDoc} |
||
224 | */ |
||
225 | public function offsetUnset($offset) |
||
229 | |||
230 | /** |
||
231 | * {@inheritDoc} |
||
232 | */ |
||
233 | public function offsetSet($offset, $value) |
||
241 | |||
242 | /** ++++ ++++ **/ |
||
243 | /** ++ Iterator Methods ++ **/ |
||
244 | /** ++++ ++++ **/ |
||
245 | |||
246 | /** |
||
247 | * {@inheritDoc} |
||
248 | */ |
||
249 | public function current() |
||
253 | |||
254 | /** |
||
255 | * {@inheritDoc} |
||
256 | */ |
||
257 | public function key() |
||
261 | |||
262 | /** |
||
263 | * {@inheritDoc} |
||
264 | */ |
||
265 | public function next() |
||
269 | |||
270 | /** |
||
271 | * {@inheritDoc} |
||
272 | */ |
||
273 | public function rewind() |
||
277 | |||
278 | /** |
||
279 | * {@inheritDoc} |
||
280 | */ |
||
281 | public function valid() |
||
285 | |||
286 | /** ++++ ++++ **/ |
||
287 | /** ++ Countable Method ++ **/ |
||
288 | /** ++++ ++++ **/ |
||
289 | |||
290 | /** |
||
291 | * {@inheritDoc} |
||
292 | */ |
||
293 | public function count() |
||
297 | } |