1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\mvc\router; |
4
|
|
|
|
5
|
|
|
class Routes implements \ArrayAccess, \Iterator |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
private $config; |
12
|
|
|
private $position = 0; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param array $config |
16
|
|
|
*/ |
17
|
|
|
public function __construct(array $config) |
18
|
|
|
{ |
19
|
|
|
$this->config = $config; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public function getData(): array |
26
|
|
|
{ |
27
|
|
|
return $this->config; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $offset |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function offsetExists($offset): bool |
36
|
|
|
{ |
37
|
|
|
return isset($this->config[$offset]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $offset |
42
|
|
|
* |
43
|
|
|
* @return mixed|null |
44
|
|
|
*/ |
45
|
|
|
public function offsetGet($offset) |
46
|
|
|
{ |
47
|
|
|
return $this->config[$offset] ?? null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $offset |
52
|
|
|
* @param mixed $value |
53
|
|
|
*/ |
54
|
|
|
public function offsetSet($offset, $value) |
55
|
|
|
{ |
56
|
|
|
$this->config[$offset] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $offset |
61
|
|
|
*/ |
62
|
|
|
public function offsetUnset($offset) |
63
|
|
|
{ |
64
|
|
|
unset($this->config[$offset]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the current element |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function current() |
72
|
|
|
{ |
73
|
|
|
return $this->config[$this->position]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Move forward to next element |
78
|
|
|
*/ |
79
|
|
|
public function next() |
80
|
|
|
{ |
81
|
|
|
$this->position++; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the key of the current element |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function key() |
89
|
|
|
{ |
90
|
|
|
return $this->position; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Checks if current position is valid |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function valid() |
98
|
|
|
{ |
99
|
|
|
return isset($this->config[$this->position]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Rewind the Iterator to the first element |
104
|
|
|
*/ |
105
|
|
|
public function rewind() |
106
|
|
|
{ |
107
|
|
|
$this->position = 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $routeDefinition |
112
|
|
|
* @param string|null $beforeRoute |
113
|
|
|
*/ |
114
|
|
|
public function addRoute(array $routeDefinition, string $beforeRoute = null) |
115
|
|
|
{ |
116
|
|
|
if ($beforeRoute) { |
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var int $k |
119
|
|
|
* @var array $route |
120
|
|
|
*/ |
121
|
|
|
foreach ((array)$this->config as $k => $route) { |
122
|
|
|
if ($route[1] === $beforeRoute) { |
123
|
|
|
$k > 1 |
124
|
|
|
? \array_splice($this->config, $k-1, 0, $routeDefinition) |
125
|
|
|
: \array_unshift($this->config, $routeDefinition); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} else { |
129
|
|
|
\array_unshift($this->config, $routeDefinition); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: