Conditions | 8 |
Paths | 8 |
Total Lines | 35 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 20 |
CRAP Score | 8 |
Changes | 0 |
1 | <?php |
||
33 | 700 | public static function unescapeAndEncode(string $uri): string |
|
34 | { |
||
35 | // Optimization: if the URL only includes characters we know will be kept as-is, then just return the URL as-is. |
||
36 | 700 | if (\preg_match('/^[A-Za-z0-9~!@#$&*()\-_=+;:,.\/?]+$/', $uri)) { |
|
37 | 554 | return $uri; |
|
38 | } |
||
39 | |||
40 | 154 | if (! \mb_check_encoding($uri, 'UTF-8')) { |
|
41 | 2 | throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected'); |
|
42 | } |
||
43 | |||
44 | 152 | $result = ''; |
|
45 | |||
46 | 152 | $chars = \mb_str_split($uri, 1, 'UTF-8'); |
|
47 | |||
48 | 152 | $l = \count($chars); |
|
49 | 152 | for ($i = 0; $i < $l; $i++) { |
|
50 | 140 | $code = $chars[$i]; |
|
51 | 140 | if ($code === '%' && $i + 2 < $l) { |
|
52 | 66 | if (\preg_match('/^[0-9a-f]{2}$/i', $chars[$i + 1] . $chars[$i + 2]) === 1) { |
|
53 | 64 | $result .= '%' . $chars[$i + 1] . $chars[$i + 2]; |
|
54 | 64 | $i += 2; |
|
55 | 64 | continue; |
|
56 | } |
||
57 | } |
||
58 | |||
59 | 102 | if (\ord($code) < 128) { |
|
60 | 102 | $result .= self::ENCODE_CACHE[\ord($code)]; |
|
61 | 102 | continue; |
|
62 | } |
||
63 | |||
64 | 12 | $result .= \rawurlencode($code); |
|
65 | } |
||
66 | |||
67 | 152 | return $result; |
|
68 | } |
||
70 |