|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Dispatcher\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Iterator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CommandsStack |
|
11
|
|
|
* @package Nip\Dispatcher\Commands |
|
12
|
|
|
*/ |
|
13
|
|
|
class CommandsCollection implements ArrayAccess, Iterator |
|
14
|
|
|
{ |
|
15
|
|
|
protected $items = []; |
|
16
|
|
|
|
|
17
|
|
|
protected $hops = 0; |
|
18
|
|
|
|
|
19
|
|
|
protected $maxHops = 30; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public function overflow() |
|
25
|
|
|
{ |
|
26
|
4 |
|
return $this->getHops() > $this->getMaxHops(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return int |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function getMaxHops(): int |
|
33
|
|
|
{ |
|
34
|
4 |
|
return $this->maxHops; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param int $maxHops |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function setMaxHops(int $maxHops) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->maxHops = $maxHops; |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return int |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function getHops(): int |
|
49
|
|
|
{ |
|
50
|
4 |
|
return $this->hops; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param int $hops |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setHops(int $hops) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->hops = $hops; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function offsetExists($offset) |
|
65
|
|
|
{ |
|
66
|
|
|
return isset($this->items[$offset]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function offsetGet($offset) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->items[$offset]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
* @param Command $value |
|
80
|
|
|
*/ |
|
81
|
4 |
|
public function offsetSet($offset, $value) |
|
82
|
|
|
{ |
|
83
|
4 |
|
if ($offset == null) { |
|
84
|
4 |
|
$this->items[] = $value; |
|
85
|
|
|
} else { |
|
86
|
|
|
$this->items[$offset] = $value; |
|
87
|
|
|
} |
|
88
|
4 |
|
$this->hops = count($this->items); |
|
89
|
4 |
|
if ($this->overflow()) { |
|
90
|
1 |
|
throw new Exception( |
|
91
|
1 |
|
"Maximum number of hops ($this->maxHops) has been reached for {$value->getString()}" |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
4 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function offsetUnset($offset) |
|
100
|
|
|
{ |
|
101
|
|
|
unset($this->items[$offset]); |
|
102
|
|
|
$this->hops = count($this); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return the current element |
|
107
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
108
|
|
|
* @return mixed Can return any type. |
|
109
|
|
|
* @since 5.0.0 |
|
110
|
|
|
*/ |
|
111
|
|
|
public function current() |
|
112
|
|
|
{ |
|
113
|
|
|
// TODO: Implement current() method. |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Move forward to next element |
|
118
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
|
119
|
|
|
* @return void Any returned value is ignored. |
|
120
|
|
|
* @since 5.0.0 |
|
121
|
|
|
*/ |
|
122
|
|
|
public function next() |
|
123
|
|
|
{ |
|
124
|
|
|
// TODO: Implement next() method. |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Return the key of the current element |
|
129
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
130
|
|
|
* @return mixed scalar on success, or null on failure. |
|
131
|
|
|
* @since 5.0.0 |
|
132
|
|
|
*/ |
|
133
|
|
|
public function key() |
|
134
|
|
|
{ |
|
135
|
|
|
// TODO: Implement key() method. |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Checks if current position is valid |
|
140
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
141
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
|
142
|
|
|
* Returns true on success or false on failure. |
|
143
|
|
|
* @since 5.0.0 |
|
144
|
|
|
*/ |
|
145
|
|
|
public function valid() |
|
146
|
|
|
{ |
|
147
|
|
|
// TODO: Implement valid() method. |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Rewind the Iterator to the first element |
|
152
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
153
|
|
|
* @return void Any returned value is ignored. |
|
154
|
|
|
* @since 5.0.0 |
|
155
|
|
|
*/ |
|
156
|
|
|
public function rewind() |
|
157
|
|
|
{ |
|
158
|
|
|
// TODO: Implement rewind() method. |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|