Completed
Push — master ( b2620d...cacd0e )
by Gabriel
02:27
created

Collection::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 3
    public function __construct(array $items = [])
15
    {
16 3
        $this->items = $items;
17 3
    }
18
19
    public function replace(array $items = [])
20
    {
21
        $this->items = $items;
22
    }
23
24 3
    public function add($item)
25
    {
26 3
        $this->items[] = $item;
27 3
    }
28
29 2
    public function first()
30
    {
31 2
        return isset($this->items[0]) ? $this->items[0] : null;
32
    }
33
34
    public function jsonSerialize()
35
    {
36 2
        return array_map(function (JsonSerializable $item) {
37 2
            return $item->jsonSerialize();
38 2
        }, $this->items);
39
    }
40
41 1
    public function count()
42
    {
43 1
        return count($this->items);
44
    }
45
46
    public function current()
47
    {
48
        return current($this->items);
49
    }
50
51
    public function next()
52
    {
53
        return next($this->items);
54
    }
55
56
    public function key()
57
    {
58
        return key($this->items);
59
    }
60
61
    public function valid()
62
    {
63
        $key = key($this->items);
64
        return ($key !== null && $key !== false);
65
    }
66
67
    public function rewind()
68
    {
69
        reset($this->items);
70
    }
71
}
72