BaseReader::current()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jackal\Copycat\Reader;
4
5
/**
6
 * Class BaseReader
7
 * @package Jackal\Copycat\Reader
8
 */
9
abstract class BaseReader implements ReaderInterface
10
{
11
    /**
12
     * @var int
13
     */
14
    private $cursor = 0;
15
16
    /**
17
     * @var array
18
     */
19
    private $items = [];
20
21
    /**
22
     * @param array $item
23
     */
24
    protected function addItem(array $item)
25
    {
26
        $this->items[] = $item;
27
    }
28
29
    /**
30
     * @param array $items
31
     */
32
    protected function setItems(array $items)
33
    {
34
        $this->items = $items;
35
    }
36
37
    /**
38
     * @param $index
39
     * @return array
40
     */
41
    public function get($index)
42
    {
43
        if (!isset($this->items[$index])) {
44
            return null;
45
        }
46
47
        return $this->items[$index];
48
    }
49
50
    public function all()
51
    {
52
        return $this->items;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function first()
59
    {
60
        if (!isset($this->items[0])) {
61
            return null;
62
        }
63
64
        return $this->items[0];
65
    }
66
67
    /**
68
     * Count elements of an object
69
     * @link http://php.net/manual/en/countable.count.php
70
     * @return int The custom count as an integer.
71
     * </p>
72
     * <p>
73
     * The return value is cast to an integer.
74
     * @since 5.1.0
75
     */
76
    public function count()
77
    {
78
        return count($this->items);
79
    }
80
81
    /**
82
     * Return the current element
83
     * @link http://php.net/manual/en/iterator.current.php
84
     * @return mixed Can return any type.
85
     * @since 5.0.0
86
     */
87
    public function current()
88
    {
89
        return $this->items[$this->cursor];
90
    }
91
92
    /**
93
     * Move forward to next element
94
     * @link http://php.net/manual/en/iterator.next.php
95
     * @return void Any returned value is ignored.
96
     * @since 5.0.0
97
     */
98
    public function next()
99
    {
100
        $this->cursor++;
101
    }
102
103
    /**
104
     * Return the key of the current element
105
     * @link http://php.net/manual/en/iterator.key.php
106
     * @return mixed scalar on success, or null on failure.
107
     * @since 5.0.0
108
     */
109
    public function key()
110
    {
111
        return $this->cursor;
112
    }
113
114
    /**
115
     * Checks if current position is valid
116
     * @link http://php.net/manual/en/iterator.valid.php
117
     * @return boolean The return value will be casted to boolean and then evaluated.
118
     * Returns true on success or false on failure.
119
     * @since 5.0.0
120
     */
121
    public function valid()
122
    {
123
        return array_key_exists($this->cursor, $this->items);
124
    }
125
126
    /**
127
     * Rewind the Iterator to the first element
128
     * @link http://php.net/manual/en/iterator.rewind.php
129
     * @return void Any returned value is ignored.
130
     * @since 5.0.0
131
     */
132
    public function rewind()
133
    {
134
        $this->cursor = 0;
135
    }
136
}
137