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
|
|
|
} |