@@ -42,18 +42,18 @@ discard block |
||
| 42 | 42 | protected bool $isValid = false; |
| 43 | 43 | protected bool $encodeUTF = false; |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @var array{code:int,message:string}|NULL |
|
| 47 | - */ |
|
| 45 | + /** |
|
| 46 | + * @var array{code:int,message:string}|NULL |
|
| 47 | + */ |
|
| 48 | 48 | protected ?array $error = null; |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Stores a list of all unicode characters in the URL |
|
| 52 | - * that have been filtered out before parsing it with |
|
| 53 | - * parse_url. |
|
| 54 | - * |
|
| 55 | - * @var array<string,string> |
|
| 56 | - */ |
|
| 50 | + /** |
|
| 51 | + * Stores a list of all unicode characters in the URL |
|
| 52 | + * that have been filtered out before parsing it with |
|
| 53 | + * parse_url. |
|
| 54 | + * |
|
| 55 | + * @var array<string,string> |
|
| 56 | + */ |
|
| 57 | 57 | protected array $unicodeChars = array(); |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -78,10 +78,10 @@ discard block |
||
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | - * |
|
| 82 | - * @param string $url The target URL. |
|
| 83 | - * @param bool $encodeUTF Whether to URL encode any plain text unicode characters. |
|
| 84 | - */ |
|
| 81 | + * |
|
| 82 | + * @param string $url The target URL. |
|
| 83 | + * @param bool $encodeUTF Whether to URL encode any plain text unicode characters. |
|
| 84 | + */ |
|
| 85 | 85 | public function __construct(string $url, bool $encodeUTF) |
| 86 | 86 | { |
| 87 | 87 | $this->url = $url; |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | $this->validate(); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Retrieves the array as parsed by PHP's parse_url, |
|
| 97 | - * filtered and adjusted as necessary. |
|
| 98 | - * |
|
| 99 | - * @return array<string,mixed> |
|
| 100 | - */ |
|
| 95 | + /** |
|
| 96 | + * Retrieves the array as parsed by PHP's parse_url, |
|
| 97 | + * filtered and adjusted as necessary. |
|
| 98 | + * |
|
| 99 | + * @return array<string,mixed> |
|
| 100 | + */ |
|
| 101 | 101 | public function getInfo() : array |
| 102 | 102 | { |
| 103 | 103 | return $this->info; |
@@ -153,11 +153,11 @@ discard block |
||
| 153 | 153 | $this->url = implode(':', $parts); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Finds any non-url encoded unicode characters in |
|
| 158 | - * the URL, and encodes them before the URL is |
|
| 159 | - * passed to parse_url. |
|
| 160 | - */ |
|
| 156 | + /** |
|
| 157 | + * Finds any non-url encoded unicode characters in |
|
| 158 | + * the URL, and encodes them before the URL is |
|
| 159 | + * passed to parse_url. |
|
| 160 | + */ |
|
| 161 | 161 | protected function filterUnicodeChars() : void |
| 162 | 162 | { |
| 163 | 163 | $chars = ConvertHelper::string2array($this->url); |
@@ -228,23 +228,23 @@ discard block |
||
| 228 | 228 | $this->isValid = true; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Goes through all information in the parse_url result |
|
| 233 | - * array, and attempts to fix any user errors in formatting |
|
| 234 | - * that can be recovered from, mostly regarding stray spaces. |
|
| 235 | - */ |
|
| 231 | + /** |
|
| 232 | + * Goes through all information in the parse_url result |
|
| 233 | + * array, and attempts to fix any user errors in formatting |
|
| 234 | + * that can be recovered from, mostly regarding stray spaces. |
|
| 235 | + */ |
|
| 236 | 236 | protected function filterParsed() : void |
| 237 | 237 | { |
| 238 | 238 | $this->info = (new ParsedInfoFilter($this->url, $this->info))->filter(); |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * Recursively goes through the array, and converts all previously |
|
| 243 | - * URL encoded characters with their unicode character counterparts. |
|
| 244 | - * |
|
| 245 | - * @param array<string,mixed> $subject |
|
| 246 | - * @return array<string,mixed> |
|
| 247 | - */ |
|
| 241 | + /** |
|
| 242 | + * Recursively goes through the array, and converts all previously |
|
| 243 | + * URL encoded characters with their unicode character counterparts. |
|
| 244 | + * |
|
| 245 | + * @param array<string,mixed> $subject |
|
| 246 | + * @return array<string,mixed> |
|
| 247 | + */ |
|
| 248 | 248 | protected function restoreUnicodeChars(array $subject) : array |
| 249 | 249 | { |
| 250 | 250 | $result = array(); |
@@ -268,13 +268,13 @@ discard block |
||
| 268 | 268 | return $result; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Replaces all URL encoded unicode characters |
|
| 273 | - * in the string with the unicode character. |
|
| 274 | - * |
|
| 275 | - * @param string $string |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 271 | + /** |
|
| 272 | + * Replaces all URL encoded unicode characters |
|
| 273 | + * in the string with the unicode character. |
|
| 274 | + * |
|
| 275 | + * @param string $string |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | 278 | protected function restoreUnicodeChar(string $string) : string |
| 279 | 279 | { |
| 280 | 280 | if(strpos($string, '%') !== false) |
@@ -295,32 +295,32 @@ discard block |
||
| 295 | 295 | ); |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | - /** |
|
| 299 | - * Checks whether the URL that was parsed is valid. |
|
| 300 | - * @return bool |
|
| 301 | - */ |
|
| 298 | + /** |
|
| 299 | + * Checks whether the URL that was parsed is valid. |
|
| 300 | + * @return bool |
|
| 301 | + */ |
|
| 302 | 302 | public function isValid() : bool |
| 303 | 303 | { |
| 304 | 304 | return $this->isValid; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | - /** |
|
| 308 | - * If the validation failed, retrieves the validation |
|
| 309 | - * error message. |
|
| 310 | - * |
|
| 311 | - * @return string |
|
| 312 | - */ |
|
| 307 | + /** |
|
| 308 | + * If the validation failed, retrieves the validation |
|
| 309 | + * error message. |
|
| 310 | + * |
|
| 311 | + * @return string |
|
| 312 | + */ |
|
| 313 | 313 | public function getErrorMessage() : string |
| 314 | 314 | { |
| 315 | 315 | return $this->error['message'] ?? ''; |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - /** |
|
| 319 | - * If the validation failed, retrieves the validation |
|
| 320 | - * error code. |
|
| 321 | - * |
|
| 322 | - * @return int |
|
| 323 | - */ |
|
| 318 | + /** |
|
| 319 | + * If the validation failed, retrieves the validation |
|
| 320 | + * error code. |
|
| 321 | + * |
|
| 322 | + * @return int |
|
| 323 | + */ |
|
| 324 | 324 | public function getErrorCode() : int |
| 325 | 325 | { |
| 326 | 326 | return $this->error['code'] ?? -1; |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | $result = parse_url($this->url); |
| 111 | 111 | $this->info = array(); |
| 112 | 112 | |
| 113 | - if(!is_array($result)) |
|
| 113 | + if (!is_array($result)) |
|
| 114 | 114 | { |
| 115 | 115 | $this->fixBrokenURL(); |
| 116 | 116 | $result = parse_url($this->url); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - if(is_array($result)) |
|
| 119 | + if (is_array($result)) |
|
| 120 | 120 | { |
| 121 | 121 | $this->info = $result; |
| 122 | 122 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | // if the URL contains any URL characters, and we |
| 127 | 127 | // do not want them URL encoded, restore them. |
| 128 | - if(!$this->encodeUTF && !empty($this->unicodeChars)) |
|
| 128 | + if (!$this->encodeUTF && !empty($this->unicodeChars)) |
|
| 129 | 129 | { |
| 130 | 130 | $this->info = $this->restoreUnicodeChars($this->info); |
| 131 | 131 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | private function fixBrokenURL() : void |
| 139 | 139 | { |
| 140 | - if(strpos($this->url, ':') === false) { |
|
| 140 | + if (strpos($this->url, ':') === false) { |
|
| 141 | 141 | return; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | // else, as unlikely as it may be. |
| 146 | 146 | $parts = explode(':', $this->url); |
| 147 | 147 | |
| 148 | - while(strpos($parts[1], '///') === 0) |
|
| 148 | + while (strpos($parts[1], '///') === 0) |
|
| 149 | 149 | { |
| 150 | 150 | $parts[1] = str_replace('///', '//', $parts[1]); |
| 151 | 151 | } |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | $keep = array(); |
| 166 | 166 | |
| 167 | - foreach($chars as $char) |
|
| 167 | + foreach ($chars as $char) |
|
| 168 | 168 | { |
| 169 | - if(preg_match('/\p{L}/uix', $char)) |
|
| 169 | + if (preg_match('/\p{L}/uix', $char)) |
|
| 170 | 170 | { |
| 171 | 171 | $encoded = rawurlencode($char); |
| 172 | 172 | |
| 173 | - if($encoded !== $char) |
|
| 173 | + if ($encoded !== $char) |
|
| 174 | 174 | { |
| 175 | 175 | $this->unicodeChars[$encoded] = $char; |
| 176 | 176 | $char = $encoded; |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | |
| 186 | 186 | protected function detectType() : bool |
| 187 | 187 | { |
| 188 | - foreach(self::$detectorClasses as $className) |
|
| 188 | + foreach (self::$detectorClasses as $className) |
|
| 189 | 189 | { |
| 190 | 190 | $detector = ClassHelper::requireObjectInstanceOf( |
| 191 | 191 | BaseURLTypeDetector::class, |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | // Use the adjusted data |
| 198 | 198 | $this->info = $detector->getInfo(); |
| 199 | 199 | |
| 200 | - if($detected) { |
|
| 200 | + if ($detected) { |
|
| 201 | 201 | $this->isValid = true; |
| 202 | 202 | return true; |
| 203 | 203 | } |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | |
| 209 | 209 | protected function validate() : void |
| 210 | 210 | { |
| 211 | - foreach(self::$validatorClasses as $validatorClass) |
|
| 211 | + foreach (self::$validatorClasses as $validatorClass) |
|
| 212 | 212 | { |
| 213 | 213 | $validator = ClassHelper::requireObjectInstanceOf( |
| 214 | 214 | BaseURLValidator::class, |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | |
| 220 | 220 | $this->info = $validator->getInfo(); |
| 221 | 221 | |
| 222 | - if($result !== true) { |
|
| 222 | + if ($result !== true) { |
|
| 223 | 223 | $this->isValid = false; |
| 224 | 224 | return; |
| 225 | 225 | } |
@@ -249,9 +249,9 @@ discard block |
||
| 249 | 249 | { |
| 250 | 250 | $result = array(); |
| 251 | 251 | |
| 252 | - foreach($subject as $key => $val) |
|
| 252 | + foreach ($subject as $key => $val) |
|
| 253 | 253 | { |
| 254 | - if(is_array($val)) |
|
| 254 | + if (is_array($val)) |
|
| 255 | 255 | { |
| 256 | 256 | $val = $this->restoreUnicodeChars($val); |
| 257 | 257 | } |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | */ |
| 278 | 278 | protected function restoreUnicodeChar(string $string) : string |
| 279 | 279 | { |
| 280 | - if(strpos($string, '%') !== false) |
|
| 280 | + if (strpos($string, '%') !== false) |
|
| 281 | 281 | { |
| 282 | 282 | return str_replace(array_keys($this->unicodeChars), array_values($this->unicodeChars), $string); |
| 283 | 283 | } |