Completed
Push — master ( c3c281...ab94e9 )
by Gabriel
03: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 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Waredesk;
4
5
use Iterator;
6
use Countable;
7
use JsonSerializable;
8
9
abstract class Collection implements Iterator, Countable, JsonSerializable
10
{
11
    protected $items;
12
    protected $key;
13
14
    public function __construct(array $items = [])
15
    {
16
        $this->items = $items;
17
    }
18
19
    public function add($item)
20
    {
21
        $this->items[] = $item;
22
    }
23
24
    public function jsonSerialize()
25
    {
26
        return array_map(function (JsonSerializable $item) {
27
            return $item->jsonSerialize();
28
        }, $this->items);
29
    }
30
31
    public function count()
32
    {
33
        return count($this->items);
34
    }
35
36
    public function current()
37
    {
38
        return current($this->items);
39
    }
40
41
    public function next()
42
    {
43
        return next($this->items);
44
    }
45
46
    public function key()
47
    {
48
        return key($this->items);
49
    }
50
51
    public function valid()
52
    {
53
        $key = key($this->items);
54
        return ($key !== null && $key !== false);
55
    }
56
57
    public function rewind()
58
    {
59
        reset($this->items);
60
    }
61
}
62