1 | <?php |
||
22 | class Parser |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * These regex define the structure of a dynamic segment in a pattern. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | |||
31 | const DYNAMIC_REGEX = "{\s*(\w*)\s*(?::\s*([^{}]*(?:{(?-1)}*)*))?\s*}"; |
||
32 | |||
33 | /** |
||
34 | * Some regex wildcards for easily definition of dynamic routes. ps. all keys and values must start with : |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | |||
39 | protected $wildcards = [ |
||
40 | ":uid" => ":uid-[a-zA-Z0-9]", |
||
41 | ":slug" => ":[a-z0-9-]", |
||
42 | ":string" => ":\w", |
||
43 | ":int" => ":\d", |
||
44 | ":integer" => ":\d", |
||
45 | ":float" => ":[-+]?\d*?[.]?\d", |
||
46 | ":double" => ":[-+]?\d*?[.]?\d", |
||
47 | ":hex" => ":0[xX][0-9a-fA-F]", |
||
48 | ":octal" => ":0[1-7][0-7]", |
||
49 | ":bool" => ":1|0|true|false|yes|no", |
||
50 | ":boolean" => ":1|0|true|false|yes|no", |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Separate routes pattern with optional parts into n new patterns. |
||
55 | * |
||
56 | * @param string $pattern |
||
57 | * |
||
58 | * @throws BadRouteException |
||
59 | * @return array |
||
60 | */ |
||
61 | |||
62 | 56 | public function parsePattern($pattern) |
|
72 | |||
73 | /** |
||
74 | * Parse all the possible patterns seeking for an incorrect or incompatible pattern. |
||
75 | * |
||
76 | * @param string[] $segments Segments are all the possible patterns made on top of a pattern with optional segments. |
||
77 | * @param int $closingNumber The count of optional segments. |
||
78 | * @param string $withoutClosing The pattern without the closing token of an optional segment. aka: ] |
||
79 | * |
||
80 | * @throws BadRouteException |
||
81 | */ |
||
82 | |||
83 | 56 | protected function parseSegments(array $segments, $closingNumber, $withoutClosing) |
|
91 | |||
92 | /** |
||
93 | * @param string[] $segments |
||
94 | * |
||
95 | * @throws BadRouteException |
||
96 | * @return array |
||
97 | */ |
||
98 | |||
99 | 54 | protected function buildSegments(array $segments) |
|
116 | |||
117 | /** |
||
118 | * @return string[] |
||
119 | */ |
||
120 | |||
121 | 7 | public function getWildcards() |
|
128 | |||
129 | /** |
||
130 | * @return string[] |
||
131 | */ |
||
132 | |||
133 | 1 | public function getWildcardTokens() |
|
137 | |||
138 | /** |
||
139 | * @param string $wildcard |
||
140 | * @return string|null |
||
141 | */ |
||
142 | |||
143 | 1 | public function getWildcard($wildcard) |
|
147 | |||
148 | /** |
||
149 | * @param string $wildcard |
||
150 | * @param string $pattern |
||
151 | * |
||
152 | * @return self |
||
153 | */ |
||
154 | |||
155 | 1 | public function setWildcard($wildcard, $pattern) |
|
160 | |||
161 | } |
||
162 |