1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\ApiGateway\Kernel\Support; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use Countable; |
8
|
|
|
use Freyo\ApiGateway\Kernel\Contracts\Arrayable; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use JsonSerializable; |
11
|
|
|
use Serializable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Collection. |
15
|
|
|
*/ |
16
|
|
|
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable, Arrayable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The collection data. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $items = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* set data. |
27
|
|
|
* |
28
|
|
|
* @param mixed $items |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $items = []) |
31
|
|
|
{ |
32
|
|
|
foreach ($items as $key => $value) { |
33
|
|
|
$this->set($key, $value); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return all items. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function all() |
43
|
|
|
{ |
44
|
|
|
return $this->items; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return specific items. |
49
|
|
|
* |
50
|
|
|
* @param array $keys |
51
|
|
|
* |
52
|
|
|
* @return \Freyo\ApiGateway\Kernel\Support\Collection |
53
|
|
|
*/ |
54
|
|
|
public function only(array $keys) |
55
|
|
|
{ |
56
|
|
|
$return = []; |
57
|
|
|
|
58
|
|
|
foreach ($keys as $key) { |
59
|
|
|
$value = $this->get($key); |
60
|
|
|
|
61
|
|
|
if (!is_null($value)) { |
62
|
|
|
$return[$key] = $value; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return new static($return); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all items except for those with the specified keys. |
71
|
|
|
* |
72
|
|
|
* @param mixed $keys |
73
|
|
|
* |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
|
|
public function except($keys) |
77
|
|
|
{ |
78
|
|
|
$keys = is_array($keys) ? $keys : func_get_args(); |
79
|
|
|
|
80
|
|
|
return new static(Arr::except($this->items, $keys)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Merge data. |
85
|
|
|
* |
86
|
|
|
* @param Collection|array $items |
87
|
|
|
* |
88
|
|
|
* @return \Freyo\ApiGateway\Kernel\Support\Collection |
89
|
|
|
*/ |
90
|
|
|
public function merge($items) |
91
|
|
|
{ |
92
|
|
|
foreach ($items as $key => $value) { |
93
|
|
|
$this->set($key, $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new static($this->all()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* To determine Whether the specified element exists. |
101
|
|
|
* |
102
|
|
|
* @param string $key |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function has($key) |
107
|
|
|
{ |
108
|
|
|
return !is_null(Arr::get($this->items, $key)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieve the first item. |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function first() |
117
|
|
|
{ |
118
|
|
|
return reset($this->items); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve the last item. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function last() |
127
|
|
|
{ |
128
|
|
|
$end = end($this->items); |
129
|
|
|
|
130
|
|
|
reset($this->items); |
131
|
|
|
|
132
|
|
|
return $end; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* add the item value. |
137
|
|
|
* |
138
|
|
|
* @param string $key |
139
|
|
|
* @param mixed $value |
140
|
|
|
*/ |
141
|
|
|
public function add($key, $value) |
142
|
|
|
{ |
143
|
|
|
Arr::set($this->items, $key, $value); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the item value. |
148
|
|
|
* |
149
|
|
|
* @param string $key |
150
|
|
|
* @param mixed $value |
151
|
|
|
*/ |
152
|
|
|
public function set($key, $value) |
153
|
|
|
{ |
154
|
|
|
Arr::set($this->items, $key, $value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Retrieve item from Collection. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* @param mixed $default |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
public function get($key, $default = null) |
166
|
|
|
{ |
167
|
|
|
return Arr::get($this->items, $key, $default); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Remove item form Collection. |
172
|
|
|
* |
173
|
|
|
* @param string $key |
174
|
|
|
*/ |
175
|
|
|
public function forget($key) |
176
|
|
|
{ |
177
|
|
|
Arr::forget($this->items, $key); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Build to array. |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function toArray() |
186
|
|
|
{ |
187
|
|
|
return $this->all(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Build to json. |
192
|
|
|
* |
193
|
|
|
* @param int $option |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function toJson($option = JSON_UNESCAPED_UNICODE) |
198
|
|
|
{ |
199
|
|
|
return json_encode($this->all(), $option); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* To string. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function __toString() |
208
|
|
|
{ |
209
|
|
|
return $this->toJson(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
214
|
|
|
* Specify data which should be serialized to JSON. |
215
|
|
|
* |
216
|
|
|
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php |
217
|
|
|
* |
218
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
219
|
|
|
* which is a value of any type other than a resource |
220
|
|
|
*/ |
221
|
|
|
public function jsonSerialize() |
222
|
|
|
{ |
223
|
|
|
return $this->items; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
228
|
|
|
* String representation of object. |
229
|
|
|
* |
230
|
|
|
* @see http://php.net/manual/en/serializable.serialize.php |
231
|
|
|
* |
232
|
|
|
* @return string the string representation of the object or null |
233
|
|
|
*/ |
234
|
|
|
public function serialize() |
235
|
|
|
{ |
236
|
|
|
return serialize($this->items); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
241
|
|
|
* Retrieve an external iterator. |
242
|
|
|
* |
243
|
|
|
* @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
244
|
|
|
* |
245
|
|
|
* @return \ArrayIterator An instance of an object implementing <b>Iterator</b> or |
246
|
|
|
* <b>Traversable</b> |
247
|
|
|
*/ |
248
|
|
|
public function getIterator() |
249
|
|
|
{ |
250
|
|
|
return new ArrayIterator($this->items); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
255
|
|
|
* Count elements of an object. |
256
|
|
|
* |
257
|
|
|
* @see http://php.net/manual/en/countable.count.php |
258
|
|
|
* |
259
|
|
|
* @return int The custom count as an integer. |
260
|
|
|
* </p> |
261
|
|
|
* <p> |
262
|
|
|
* The return value is cast to an integer |
263
|
|
|
*/ |
264
|
|
|
public function count() |
265
|
|
|
{ |
266
|
|
|
return count($this->items); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
271
|
|
|
* Constructs the object. |
272
|
|
|
* |
273
|
|
|
* @see http://php.net/manual/en/serializable.unserialize.php |
274
|
|
|
* |
275
|
|
|
* @param string $serialized <p> |
276
|
|
|
* The string representation of the object. |
277
|
|
|
* </p> |
278
|
|
|
* |
279
|
|
|
* @return mixed|void |
280
|
|
|
*/ |
281
|
|
|
public function unserialize($serialized) |
282
|
|
|
{ |
283
|
|
|
return $this->items = unserialize($serialized); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get a data by key. |
288
|
|
|
* |
289
|
|
|
* @param string $key |
290
|
|
|
* |
291
|
|
|
* @return mixed |
292
|
|
|
*/ |
293
|
|
|
public function __get($key) |
294
|
|
|
{ |
295
|
|
|
return $this->get($key); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Assigns a value to the specified data. |
300
|
|
|
* |
301
|
|
|
* @param string $key |
302
|
|
|
* @param mixed $value |
303
|
|
|
*/ |
304
|
|
|
public function __set($key, $value) |
305
|
|
|
{ |
306
|
|
|
$this->set($key, $value); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Whether or not an data exists by key. |
311
|
|
|
* |
312
|
|
|
* @param string $key |
313
|
|
|
* |
314
|
|
|
* @return bool |
315
|
|
|
*/ |
316
|
|
|
public function __isset($key) |
317
|
|
|
{ |
318
|
|
|
return $this->has($key); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Unset an data by key. |
323
|
|
|
* |
324
|
|
|
* @param string $key |
325
|
|
|
*/ |
326
|
|
|
public function __unset($key) |
327
|
|
|
{ |
328
|
|
|
$this->forget($key); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* var_export. |
333
|
|
|
* |
334
|
|
|
* @return array |
335
|
|
|
*/ |
336
|
|
|
public function __set_state() |
337
|
|
|
{ |
338
|
|
|
return $this->all(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
343
|
|
|
* Whether a offset exists. |
344
|
|
|
* |
345
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetexists.php |
346
|
|
|
* |
347
|
|
|
* @param mixed $offset <p> |
348
|
|
|
* An offset to check for. |
349
|
|
|
* </p> |
350
|
|
|
* |
351
|
|
|
* @return bool true on success or false on failure. |
352
|
|
|
* The return value will be casted to boolean if non-boolean was returned |
353
|
|
|
*/ |
354
|
|
|
public function offsetExists($offset) |
355
|
|
|
{ |
356
|
|
|
return $this->has($offset); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
361
|
|
|
* Offset to unset. |
362
|
|
|
* |
363
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetunset.php |
364
|
|
|
* |
365
|
|
|
* @param mixed $offset <p> |
366
|
|
|
* The offset to unset. |
367
|
|
|
* </p> |
368
|
|
|
*/ |
369
|
|
|
public function offsetUnset($offset) |
370
|
|
|
{ |
371
|
|
|
if ($this->offsetExists($offset)) { |
372
|
|
|
$this->forget($offset); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
378
|
|
|
* Offset to retrieve. |
379
|
|
|
* |
380
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetget.php |
381
|
|
|
* |
382
|
|
|
* @param mixed $offset <p> |
383
|
|
|
* The offset to retrieve. |
384
|
|
|
* </p> |
385
|
|
|
* |
386
|
|
|
* @return mixed Can return all value types |
387
|
|
|
*/ |
388
|
|
|
public function offsetGet($offset) |
389
|
|
|
{ |
390
|
|
|
return $this->offsetExists($offset) ? $this->get($offset) : null; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
395
|
|
|
* Offset to set. |
396
|
|
|
* |
397
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetset.php |
398
|
|
|
* |
399
|
|
|
* @param mixed $offset <p> |
400
|
|
|
* The offset to assign the value to. |
401
|
|
|
* </p> |
402
|
|
|
* @param mixed $value <p> |
403
|
|
|
* The value to set. |
404
|
|
|
* </p> |
405
|
|
|
*/ |
406
|
|
|
public function offsetSet($offset, $value) |
407
|
|
|
{ |
408
|
|
|
$this->set($offset, $value); |
409
|
|
|
} |
410
|
|
|
} |