1 | <?php |
||
27 | class UriParser |
||
28 | { |
||
29 | use HostIpTrait; |
||
30 | |||
31 | use HostnameTrait; |
||
32 | |||
33 | use PathFormatterTrait; |
||
34 | |||
35 | use ValidatorTrait; |
||
36 | |||
37 | const REGEXP_URI = ',^((?<scheme>[^:/?\#]+):)? |
||
38 | (?<authority>//([^/?\#]*))? |
||
39 | (?<path>[^?\#]*) |
||
40 | (?<query>\?([^\#]*))? |
||
41 | (?<fragment>\#(.*))?,x'; |
||
42 | |||
43 | const REGEXP_AUTHORITY = ',^(?<userinfo>(?<ucontent>.*?)@)?(?<hostname>.*?)?$,'; |
||
44 | |||
45 | const REGEXP_REVERSE_HOSTNAME = ',^((?<port>[^(\[\])]*):)?(?<host>.*)?$,'; |
||
46 | |||
47 | const REGEXP_SCHEME = ',^([a-z]([-a-z0-9+.]+)?)?$,i'; |
||
48 | |||
49 | const REGEXP_INVALID_USER = ',[/?#@:],'; |
||
50 | |||
51 | const REGEXP_INVALID_PASS = ',[/?#@],'; |
||
52 | |||
53 | /** |
||
54 | * default components hash table |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $components = [ |
||
59 | 'scheme' => null, 'user' => null, 'pass' => null, 'host' => null, |
||
60 | 'port' => null, 'path' => null, 'query' => null, 'fragment' => null, |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * Parse a string as an URI according to the regexp form rfc3986 |
||
65 | * |
||
66 | * @param string $uri The URI to parse |
||
67 | * |
||
68 | * @return array the array is similar to PHP's parse_url hash response |
||
69 | */ |
||
70 | 440 | public function parse($uri) |
|
84 | |||
85 | /** |
||
86 | * Parse a string as an URI according to the regexp form rfc3986 |
||
87 | * |
||
88 | * @param string $uri The URI to parse |
||
89 | * |
||
90 | * @return array the array is similar to PHP's parse_url hash response |
||
91 | */ |
||
92 | public function __invoke($uri) |
||
96 | 440 | ||
97 | 440 | /** |
|
98 | * Extract URI parts |
||
99 | 440 | * |
|
100 | 436 | * @see http://tools.ietf.org/html/rfc3986#appendix-B |
|
101 | * |
||
102 | * @param string $uri The URI to split |
||
103 | 4 | * |
|
104 | 4 | * @return string[] |
|
105 | 4 | */ |
|
106 | protected function extractUriParts($uri) |
||
121 | |||
122 | /** |
||
123 | * Normalize URI components hash |
||
124 | * |
||
125 | * @param array $components a hash representation of the URI components |
||
126 | * similar to PHP parse_url function result |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 440 | public function normalizeUriHash(array $components) |
|
134 | 154 | ||
135 | /** |
||
136 | * Parse a URI authority part into its components |
||
137 | 352 | * |
|
138 | 352 | * @param string $authority |
|
139 | 2 | * |
|
140 | * @return array |
||
141 | */ |
||
142 | 350 | protected function parseAuthority($authority) |
|
162 | 350 | ||
163 | 350 | /** |
|
164 | 350 | * Parse the hostname into its components Host and Port |
|
165 | 350 | * |
|
166 | 350 | * No validation is done on the port or host component found |
|
167 | 350 | * |
|
168 | 350 | * @param string $hostname |
|
169 | 344 | * |
|
170 | * @return array |
||
171 | 342 | */ |
|
172 | protected function parseHostname($hostname) |
||
185 | 334 | ||
186 | /** |
||
187 | 344 | * validate the host component |
|
188 | * |
||
189 | * @param string $host |
||
190 | * |
||
191 | * @return int|null |
||
192 | */ |
||
193 | 340 | protected function filterHost($host) |
|
201 | 340 | ||
202 | /** |
||
203 | 340 | * @inheritdoc |
|
204 | 2 | */ |
|
205 | protected function setIsAbsolute($host) |
||
209 | |||
210 | /** |
||
211 | * @inheritdoc |
||
212 | */ |
||
213 | protected function assertLabelsCount(array $labels) |
||
219 | 44 | ||
220 | 4 | /** |
|
221 | * Format the user info |
||
222 | 40 | * |
|
223 | 38 | * @param string $user |
|
224 | 36 | * @param string $pass |
|
225 | 36 | * |
|
226 | 38 | * @return string |
|
227 | */ |
||
228 | public function buildUserInfo($user, $pass) |
||
240 | 46 | ||
241 | 44 | /** |
|
242 | * Filter and format the user for URI string representation |
||
243 | * |
||
244 | 2 | * @param null|string $user |
|
245 | * |
||
246 | * @throws InvalidArgumentException If the user is invalid |
||
247 | * |
||
248 | * @return null|string |
||
249 | */ |
||
250 | protected function filterUser($user) |
||
258 | 40 | ||
259 | 38 | /** |
|
260 | * Filter and format the pass for URI string representation |
||
261 | * |
||
262 | 2 | * @param null|string $pass |
|
263 | * |
||
264 | * @throws InvalidArgumentException If the pass is invalid |
||
265 | * |
||
266 | * @return null|string |
||
267 | */ |
||
268 | protected function filterPass($pass) |
||
276 | } |
||
277 |