|
15 | 15 |
|
16 | 16 | namespace Pdp;
|
17 | 17 |
|
18 |
| -use function is_string; |
19 | 18 | use Pdp\Exception\InvalidDomain;
|
20 | 19 | use TypeError;
|
21 | 20 | use UnexpectedValueException;
|
|
26 | 25 | use function idn_to_utf8;
|
27 | 26 | use function implode;
|
28 | 27 | use function is_scalar;
|
| 28 | +use function is_string; |
29 | 29 | use function iterator_to_array;
|
30 | 30 | use function method_exists;
|
31 | 31 | use function preg_match;
|
@@ -111,31 +111,36 @@ private static function getIdnErrors(int $error_bit): string
|
111 | 111 | * @return string
|
112 | 112 | */
|
113 | 113 | private function idnToAscii(string $domain, int $IDNAOption = IDNA_DEFAULT): string
|
| 114 | + { |
| 115 | + list($domain,) = $this->transformToAscii($domain, $IDNAOption); |
| 116 | + return $domain; |
| 117 | + } |
| 118 | + |
| 119 | + private function transformToAscii(string $domain, int $option):array |
114 | 120 | {
|
115 | 121 | $domain = rawurldecode($domain);
|
116 | 122 | static $pattern = '/[^\x20-\x7f]/';
|
117 | 123 | if (!preg_match($pattern, $domain)) {
|
118 |
| - return strtolower($domain); |
| 124 | + return [strtolower($domain), false]; |
119 | 125 | }
|
120 |
| - |
121 |
| - $output = idn_to_ascii($domain, $IDNAOption, INTL_IDNA_VARIANT_UTS46, $arr); |
122 |
| - if (0 !== $arr['errors']) { |
123 |
| - throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors']))); |
| 126 | + |
| 127 | + $output = idn_to_ascii($domain, $option, INTL_IDNA_VARIANT_UTS46, $info); |
| 128 | + if (0 !== $info['errors']) { |
| 129 | + throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($info['errors']))); |
124 | 130 | }
|
125 |
| - |
| 131 | + |
126 | 132 | // @codeCoverageIgnoreStart
|
127 | 133 | if (false === $output) {
|
128 | 134 | throw new UnexpectedValueException(sprintf('The Intl extension is misconfigured for %s, please correct this issue before proceeding.', PHP_OS));
|
129 | 135 | }
|
130 | 136 | // @codeCoverageIgnoreEnd
|
131 |
| - |
| 137 | + |
132 | 138 | if (false === strpos($output, '%')) {
|
133 |
| - return $output; |
| 139 | + return [$output, $info['isTransitionalDifferent']]; |
134 | 140 | }
|
135 |
| - |
| 141 | + |
136 | 142 | throw new InvalidDomain(sprintf('The host `%s` is invalid: it contains invalid characters', $domain));
|
137 | 143 | }
|
138 |
| - |
139 | 144 | /**
|
140 | 145 | * Converts the input to its IDNA UNICODE form.
|
141 | 146 | *
|
@@ -232,11 +237,10 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
|
232 | 237 |
|
233 | 238 | private function hasTransitionalDifference($domain):bool
|
234 | 239 | {
|
235 |
| - if(!is_string($domain) || empty($domain)){ |
| 240 | + if(!is_string($domain) || '' === $domain){ |
236 | 241 | return false;
|
237 | 242 | }
|
238 |
| - $info = []; |
239 |
| - idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46, $info); |
240 |
| - return (bool)$info['isTransitionalDifferent'] === true; |
| 243 | + list(, $isTransitionalDifferent) = $this->transformToAscii($domain, IDNA_DEFAULT); |
| 244 | + return $isTransitionalDifferent; |
241 | 245 | }
|
242 | 246 | }
|
0 commit comments