@@ -22,140 +22,140 @@ |
||
22 | 22 | class Parser |
23 | 23 | { |
24 | 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 | - public function parsePattern($pattern) |
|
63 | - { |
|
64 | - $withoutClosing = rtrim($pattern, "]"); |
|
65 | - $closingNumber = strlen($pattern) - strlen($withoutClosing); |
|
66 | - |
|
67 | - $segments = preg_split("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\[~x", $withoutClosing); |
|
68 | - $this->parseSegments($segments, $closingNumber, $withoutClosing); |
|
69 | - |
|
70 | - return $this->buildSegments($segments); |
|
71 | - } |
|
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 | - protected function parseSegments(array $segments, $closingNumber, $withoutClosing) |
|
84 | - { |
|
85 | - if ($closingNumber !== count($segments) - 1) { |
|
86 | - if (preg_match("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\]~x", $withoutClosing)) { |
|
87 | - throw new BadRouteException(BadRouteException::OPTIONAL_SEGMENTS_ON_MIDDLE); |
|
88 | - } else throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param string[] $segments |
|
94 | - * |
|
95 | - * @throws BadRouteException |
|
96 | - * @return array |
|
97 | - */ |
|
98 | - |
|
99 | - protected function buildSegments(array $segments) |
|
100 | - { |
|
101 | - $pattern = ""; |
|
102 | - $patterns = []; |
|
103 | - $wildcardTokens = array_keys($this->wildcards); |
|
104 | - $wildcardRegex = $this->wildcards; |
|
105 | - |
|
106 | - foreach ($segments as $n => $segment) { |
|
107 | - if ($segment === "" && $n !== 0) { |
|
108 | - throw new BadRouteException(BadRouteException::EMPTY_OPTIONAL_PARTS); |
|
109 | - } |
|
110 | - |
|
111 | - $patterns[] = $pattern .= str_replace($wildcardTokens, $wildcardRegex, $segment); |
|
112 | - } |
|
113 | - |
|
114 | - return $patterns; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @return string[] |
|
119 | - */ |
|
120 | - |
|
121 | - public function getWildcards() |
|
122 | - { |
|
123 | - $wildcards = []; |
|
124 | - foreach ($this->wildcards as $token => $regex) |
|
125 | - $wildcards[substr($token, 1)] = substr($regex, 1); |
|
126 | - return $wildcards; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return string[] |
|
131 | - */ |
|
132 | - |
|
133 | - public function getWildcardTokens() |
|
134 | - { |
|
135 | - return $this->wildcards; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param string $wildcard |
|
140 | - * @return string|null |
|
141 | - */ |
|
142 | - |
|
143 | - public function getWildcard($wildcard) |
|
144 | - { |
|
145 | - return isset($this->wildcards[":$wildcard"]) ? substr($this->wildcards[":$wildcard"], 1) : null; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @param string $wildcard |
|
150 | - * @param string $pattern |
|
151 | - * |
|
152 | - * @return self |
|
153 | - */ |
|
154 | - |
|
155 | - public function setWildcard($wildcard, $pattern) |
|
156 | - { |
|
157 | - $this->wildcards[":$wildcard"] = ":$pattern"; |
|
158 | - return $this; |
|
159 | - } |
|
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 | + public function parsePattern($pattern) |
|
63 | + { |
|
64 | + $withoutClosing = rtrim($pattern, "]"); |
|
65 | + $closingNumber = strlen($pattern) - strlen($withoutClosing); |
|
66 | + |
|
67 | + $segments = preg_split("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\[~x", $withoutClosing); |
|
68 | + $this->parseSegments($segments, $closingNumber, $withoutClosing); |
|
69 | + |
|
70 | + return $this->buildSegments($segments); |
|
71 | + } |
|
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 | + protected function parseSegments(array $segments, $closingNumber, $withoutClosing) |
|
84 | + { |
|
85 | + if ($closingNumber !== count($segments) - 1) { |
|
86 | + if (preg_match("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\]~x", $withoutClosing)) { |
|
87 | + throw new BadRouteException(BadRouteException::OPTIONAL_SEGMENTS_ON_MIDDLE); |
|
88 | + } else throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param string[] $segments |
|
94 | + * |
|
95 | + * @throws BadRouteException |
|
96 | + * @return array |
|
97 | + */ |
|
98 | + |
|
99 | + protected function buildSegments(array $segments) |
|
100 | + { |
|
101 | + $pattern = ""; |
|
102 | + $patterns = []; |
|
103 | + $wildcardTokens = array_keys($this->wildcards); |
|
104 | + $wildcardRegex = $this->wildcards; |
|
105 | + |
|
106 | + foreach ($segments as $n => $segment) { |
|
107 | + if ($segment === "" && $n !== 0) { |
|
108 | + throw new BadRouteException(BadRouteException::EMPTY_OPTIONAL_PARTS); |
|
109 | + } |
|
110 | + |
|
111 | + $patterns[] = $pattern .= str_replace($wildcardTokens, $wildcardRegex, $segment); |
|
112 | + } |
|
113 | + |
|
114 | + return $patterns; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @return string[] |
|
119 | + */ |
|
120 | + |
|
121 | + public function getWildcards() |
|
122 | + { |
|
123 | + $wildcards = []; |
|
124 | + foreach ($this->wildcards as $token => $regex) |
|
125 | + $wildcards[substr($token, 1)] = substr($regex, 1); |
|
126 | + return $wildcards; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return string[] |
|
131 | + */ |
|
132 | + |
|
133 | + public function getWildcardTokens() |
|
134 | + { |
|
135 | + return $this->wildcards; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param string $wildcard |
|
140 | + * @return string|null |
|
141 | + */ |
|
142 | + |
|
143 | + public function getWildcard($wildcard) |
|
144 | + { |
|
145 | + return isset($this->wildcards[":$wildcard"]) ? substr($this->wildcards[":$wildcard"], 1) : null; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @param string $wildcard |
|
150 | + * @param string $pattern |
|
151 | + * |
|
152 | + * @return self |
|
153 | + */ |
|
154 | + |
|
155 | + public function setWildcard($wildcard, $pattern) |
|
156 | + { |
|
157 | + $this->wildcards[":$wildcard"] = ":$pattern"; |
|
158 | + return $this; |
|
159 | + } |
|
160 | 160 | |
161 | 161 | } |
@@ -85,7 +85,9 @@ discard block |
||
85 | 85 | if ($closingNumber !== count($segments) - 1) { |
86 | 86 | if (preg_match("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\]~x", $withoutClosing)) { |
87 | 87 | throw new BadRouteException(BadRouteException::OPTIONAL_SEGMENTS_ON_MIDDLE); |
88 | - } else throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
88 | + } else { |
|
89 | + throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
90 | + } |
|
89 | 91 | } |
90 | 92 | } |
91 | 93 | |
@@ -121,8 +123,9 @@ discard block |
||
121 | 123 | public function getWildcards() |
122 | 124 | { |
123 | 125 | $wildcards = []; |
124 | - foreach ($this->wildcards as $token => $regex) |
|
125 | - $wildcards[substr($token, 1)] = substr($regex, 1); |
|
126 | + foreach ($this->wildcards as $token => $regex) { |
|
127 | + $wildcards[substr($token, 1)] = substr($regex, 1); |
|
128 | + } |
|
126 | 129 | return $wildcards; |
127 | 130 | } |
128 | 131 |