Collection   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 397
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 1
dl 0
loc 397
rs 8.6
c 0
b 0
f 0

29 Methods

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