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