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