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