1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
4
|
|
|
|
5
|
|
|
use Bavix\Exceptions\Runtime; |
6
|
|
|
|
7
|
|
|
class Path implements \Serializable, \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $defaultRegex; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $pattern; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $value; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $regex; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Path constructor. |
32
|
|
|
* |
33
|
|
|
* @param string $defaultRegex |
34
|
|
|
* @param string $value |
35
|
|
|
* @param array $regex |
36
|
|
|
*/ |
37
|
7 |
|
public function __construct(string $defaultRegex, string $value, array $regex = []) |
38
|
|
|
{ |
39
|
7 |
|
$this->defaultRegex = $defaultRegex; |
40
|
7 |
|
$this->value = $value; |
41
|
7 |
|
$this->regex = $regex; |
42
|
7 |
|
$this->processing(); |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* processing: |
47
|
|
|
* path: '/(<lang:\w+>)' -> '/(<lang>)' |
48
|
|
|
* re: [] -> ['lang' => '\w+'] |
49
|
|
|
* |
50
|
|
|
* if attr exists -> throws |
51
|
|
|
*/ |
52
|
7 |
|
protected function processing(): void |
53
|
|
|
{ |
54
|
7 |
|
$this->value = \preg_replace_callback( |
55
|
7 |
|
'~\<(?<key>' . $this->defaultRegex . '+):(?<value>.+?)>~', |
56
|
7 |
|
function (array $matches) { |
57
|
|
|
|
58
|
4 |
|
if (!empty($this->regex[$matches['key']])) { |
59
|
|
|
throw new Runtime(\sprintf( |
60
|
|
|
'duplicate in registry key `%s` for path `%s`', |
61
|
|
|
$matches['key'], |
62
|
|
|
$this->value |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
if (!empty($matches['value'])) { |
67
|
4 |
|
$this->regex[$matches['key']] = $matches['value']; |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
return '<' . $matches['key'] . '>'; |
71
|
7 |
|
}, |
72
|
7 |
|
$this->value |
73
|
|
|
); |
74
|
7 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
6 |
|
public function getPattern(): string |
80
|
|
|
{ |
81
|
6 |
|
if (!$this->pattern) { |
82
|
6 |
|
$this->pattern = \preg_replace_callback( |
83
|
6 |
|
'~\<(?<key>' . $this->defaultRegex . '+)\>~', |
84
|
6 |
|
function ($matches) { |
85
|
6 |
|
return '(?<' . $matches['key'] . '>' . |
86
|
6 |
|
$this->regexAttribute($matches['key']) . |
87
|
6 |
|
')'; |
88
|
6 |
|
}, |
89
|
6 |
|
$this->quote($this->value) |
90
|
|
|
); |
91
|
|
|
} |
92
|
6 |
|
return $this->pattern; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $name |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
6 |
|
protected function regexAttribute(string $name): string |
101
|
|
|
{ |
102
|
6 |
|
return $this->regex[$name] ?? $this->defaultRegex; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $value |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
6 |
|
protected function quote(string $value): string |
111
|
|
|
{ |
112
|
6 |
|
$path = \preg_quote($value, '()'); |
113
|
6 |
|
$path = \strtr($path, [ |
114
|
6 |
|
'\\(' => '(', |
115
|
|
|
'\\)' => ')', |
116
|
|
|
'\\<' => '<', |
117
|
|
|
'\\>' => '>', |
118
|
|
|
]); |
119
|
|
|
|
120
|
6 |
|
return \str_replace(')', ')?', $path); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getValue(): string |
127
|
|
|
{ |
128
|
|
|
return $this->value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getRegex(): array |
135
|
|
|
{ |
136
|
|
|
return $this->regex; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param self $parent |
141
|
|
|
*/ |
142
|
4 |
|
public function hinge(self $parent): void |
143
|
|
|
{ |
144
|
4 |
|
$this->pattern = null; |
145
|
4 |
|
$this->value = $parent->value . $this->value; |
146
|
4 |
|
$this->regex = \array_merge( |
147
|
4 |
|
$parent->regex, |
148
|
4 |
|
$this->regex |
149
|
|
|
); |
150
|
4 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function __sleep(): array |
156
|
|
|
{ |
157
|
|
|
return \array_keys($this->jsonSerialize()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
|
|
public function jsonSerialize(): array |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
'defaultRegex' => $this->defaultRegex, |
167
|
|
|
'pattern' => $this->pattern, |
168
|
|
|
'regex' => $this->regex, |
169
|
|
|
'value' => $this->value, |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function serialize(): string |
177
|
|
|
{ |
178
|
|
|
return \serialize($this->jsonSerialize()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
184
|
|
|
public function unserialize($serialized): void |
185
|
|
|
{ |
186
|
|
|
foreach (\unserialize($serialized, (array)null) as $key => $value) { |
187
|
|
|
$this->$key = $value; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|