Completed
Push — master ( fa2f9e...da6516 )
by Gabriel
02:23
created

Collection::first()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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