Completed
Push — master ( 4e457c...e6ff17 )
by Gabriel
02:37
created

Collection::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Waredesk;
4
5
use Iterator;
6
use Countable;
7
use JsonSerializable;
8
use ArrayAccess;
9
10
abstract class Collection implements Iterator, Countable, ArrayAccess, JsonSerializable
11
{
12
    protected $items;
13
    protected $key;
14
15 6
    public function __construct(array $items = [])
16
    {
17 6
        $this->items = $items;
18 6
    }
19
20 1
    public function __clone()
21
    {
22 1
        foreach ($this->items as $key => $item) {
23 1
            $this->items[$key] = clone $item;
24
        }
25 1
    }
26
27 3
    public function reset()
28
    {
29 3
        $this->items = [];
30 3
    }
31
32
    public function replace(array $items = [])
33
    {
34
        $this->items = $items;
35
    }
36
37 6
    public function add($item)
38
    {
39 6
        $this->items[] = $item;
40 6
    }
41
42 4
    public function first()
43
    {
44 4
        return isset($this->items[0]) ? $this->items[0] : null;
45
    }
46
47
    public function jsonSerialize()
48
    {
49 3
        return array_map(function (JsonSerializable $item) {
50 3
            return $item->jsonSerialize();
51 3
        }, $this->items);
52
    }
53
54 3
    public function count()
55
    {
56 3
        return count($this->items);
57
    }
58
59 1
    public function current()
60
    {
61 1
        return current($this->items);
62
    }
63
64 1
    public function next()
65
    {
66 1
        return next($this->items);
67
    }
68
69 1
    public function key()
70
    {
71 1
        return key($this->items);
72
    }
73
74 5
    public function valid()
75
    {
76 5
        $key = key($this->items);
77 5
        return ($key !== null && $key !== false);
78
    }
79
80 5
    public function rewind()
81
    {
82 5
        reset($this->items);
83 5
    }
84
85
    public function offsetExists($offset): bool
86
    {
87
        return array_key_exists($offset, $this->items);
88
    }
89
90
    public function offsetGet($offset)
91
    {
92
        return $this->items[$offset];
93
    }
94
95
    public function offsetSet($offset, $value)
96
    {
97
        $this->items[$offset] = $value;
98
    }
99
100 1
    public function offsetUnset($offset)
101
    {
102 1
        unset($this->items[$offset]);
103 1
    }
104
}
105