|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Meraki\Route; |
|
5
|
|
|
|
|
6
|
|
|
use IteratorAggregate; |
|
7
|
|
|
use Countable; |
|
8
|
|
|
use Meraki\Route\Rule; |
|
9
|
|
|
use LogicException; |
|
10
|
|
|
use ArrayIterator; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A store for route rules. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nathan Bishop <[email protected]> (https://nathanbishop.name) |
|
16
|
|
|
* @copyright 2019 Nathan Bishop |
|
17
|
|
|
* @license The MIT license. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Collection implements IteratorAggregate, Countable |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Rule[] [$rules description] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $rules = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Add a route to the collection. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Rule $rule The route object to add. |
|
30
|
|
|
* @throws LogicException When trying to add a route that already exists. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function add(Rule $rule): void |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->contains($rule)) { |
|
35
|
|
|
throw new LogicException('Route rule already exists.'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->rules[] = $rule; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Remove a route from the collection. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Rule $rule The route object to remove. |
|
45
|
|
|
* @throws LogicException When trying to remove a route that has not beenadded previously. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function remove(Rule $rule): void |
|
48
|
|
|
{ |
|
49
|
|
|
$index = $this->indexOf($rule); |
|
50
|
|
|
|
|
51
|
|
|
if ($index === -1) { |
|
52
|
|
|
throw new LogicException('Cannot remove a rule that is not in the collection.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
unset($this->rules[$index]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if a route is in the collection. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Rule $rule The route to check for. |
|
62
|
|
|
* @return boolean `true` if the rule exists, otherwise `false`. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function contains(Rule $rule): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->indexOf($rule) !== -1; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the order in which a route was added. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Rule $rule The route object to check for. |
|
73
|
|
|
* @return integer The index (0 or a positive integer if it exists, -1 if it doesn't) of the route. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function indexOf(Rule $rule): int |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($this->rules as $key => $value) { |
|
78
|
|
|
if ($value === $rule) { |
|
79
|
|
|
return $key; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return -1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Merge another route collection into this one. |
|
88
|
|
|
* |
|
89
|
|
|
* @param self $rules The other collection of routes to add. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function merge(self $rules): void |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($rules as $rule) { |
|
94
|
|
|
$this->add($rule); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Quickly determine if there are any routes contained in the collection. |
|
100
|
|
|
* |
|
101
|
|
|
* @return boolean `true`, if the collection has not routes, `false` otherwise. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function isEmpty(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return empty($this->rules); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Count the number of routes added. |
|
110
|
|
|
* |
|
111
|
|
|
* @return integer The number of routes added. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function count(): int |
|
114
|
|
|
{ |
|
115
|
|
|
return count($this->rules); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get an iterator that can be used with the `foreach` and other like constructs. |
|
120
|
|
|
* |
|
121
|
|
|
* @return ArrayIterator An iterator for the currently contained routes. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getIterator(): ArrayIterator |
|
124
|
|
|
{ |
|
125
|
|
|
return new ArrayIterator($this->rules); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|