Failed Conditions
Push — master ( 5aed68...741277 )
by Mickael
13s queued 11s
created

ParserCollection::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Author: mickael Louzet @micklouzet
5
 * File: ParserCollection.php
6
 * Created: 05/12/2019
7
 */
8
9
declare(strict_types=1);
10
11
namespace ComposerLockFileParser;
12
13
use ComposerLockFileParser\SearcherTrait;
14
15
class ParserCollection extends AbstractSearcher implements \Countable, \Iterator, \ArrayAccess
16
{
17
    use SearcherTrait;
18
19
    private $values = [];
20
21
    private $position = 0;
22
23
    /**
24
     * This constructor is there in order to be able to create a collection with
25
     * its values already added
26
     */
27
    public function __construct(array $values = [])
28
    {
29
        foreach ($values as $k => $value) {
30
            $this->offsetSet($k, $value);
31
        }
32
    }
33
34
    /**
35
     * Implementation of method declared in \Countable.
36
     * Provides support for count()
37
     */
38
    public function count()
39
    {
40
        return count($this->values);
41
    }
42
43
    /**
44
     * Implementation of method declared in \Iterator
45
     * Resets the internal cursor to the beginning of the array
46
     */
47
    public function rewind()
48
    {
49
        $this->position = 0;
50
    }
51
52
    /**
53
     * Implementation of method declared in \Iterator
54
     * Used to get the current key (as for instance in a foreach()-structure
55
     */
56
    public function key()
57
    {
58
        return $this->position;
59
    }
60
61
    /**
62
     * Implementation of method declared in \Iterator
63
     * Used to get the value at the current cursor position
64
     */
65
    public function current()
66
    {
67
        return $this->values[$this->position];
68
    }
69
70
    /**
71
     * Implementation of method declared in \Iterator
72
     * Used to move the cursor to the next position
73
     */
74
    public function next()
75
    {
76
        $this->position++;
77
    }
78
79
    /**
80
     * Implementation of method declared in \Iterator
81
     * Checks if the current cursor position is valid
82
     */
83
    public function valid()
84
    {
85
        return isset($this->values[$this->position]);
86
    }
87
88
    /**
89
     * Implementation of method declared in \ArrayAccess
90
     * Used to be able to use functions like isset()
91
     */
92
    public function offsetExists($offset)
93
    {
94
        return isset($this->values[$offset]);
95
    }
96
97
    /**
98
     * Implementation of method declared in \ArrayAccess
99
     * Used for direct access array-like ($collection[$offset]);
100
     */
101
    public function offsetGet($offset)
102
    {
103
        return $this->values[$offset];
104
    }
105
106
    /**
107
     * Implementation of method declared in \ArrayAccess
108
     * Used for direct setting of values
109
     */
110
    public function offsetSet($offset, $value)
111
    {
112
        // if (!is_int($value)) {
113
        //     throw new \InvalidArgumentException("Must be an int");
114
        // }
115
116
        if (empty($offset)) { //this happens when you do $collection[] = 1;
117
            $this->values[] = $value;
118
        } else {
119
            $this->values[$offset] = $value;
120
        }
121
    }
122
123
    /**
124
     * Implementation of method declared in \ArrayAccess
125
     * Used for unset()
126
     */
127
    public function offsetUnset($offset)
128
    {
129
        unset($this->values[$offset]);
130
    }
131
132
}