Collection::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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