Skip to content

Commit e99bc5e

Browse files
committed
fix hasTransitionalDifference
1 parent 1fa861e commit e99bc5e

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/IDNAConverterTrait.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
namespace Pdp;
1717

18-
use function is_string;
1918
use Pdp\Exception\InvalidDomain;
2019
use TypeError;
2120
use UnexpectedValueException;
@@ -26,6 +25,7 @@
2625
use function idn_to_utf8;
2726
use function implode;
2827
use function is_scalar;
28+
use function is_string;
2929
use function iterator_to_array;
3030
use function method_exists;
3131
use function preg_match;
@@ -111,31 +111,36 @@ private static function getIdnErrors(int $error_bit): string
111111
* @return string
112112
*/
113113
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
114120
{
115121
$domain = rawurldecode($domain);
116122
static $pattern = '/[^\x20-\x7f]/';
117123
if (!preg_match($pattern, $domain)) {
118-
return strtolower($domain);
124+
return [strtolower($domain), false];
119125
}
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'])));
124130
}
125-
131+
126132
// @codeCoverageIgnoreStart
127133
if (false === $output) {
128134
throw new UnexpectedValueException(sprintf('The Intl extension is misconfigured for %s, please correct this issue before proceeding.', PHP_OS));
129135
}
130136
// @codeCoverageIgnoreEnd
131-
137+
132138
if (false === strpos($output, '%')) {
133-
return $output;
139+
return [$output, $info['isTransitionalDifferent']];
134140
}
135-
141+
136142
throw new InvalidDomain(sprintf('The host `%s` is invalid: it contains invalid characters', $domain));
137143
}
138-
139144
/**
140145
* Converts the input to its IDNA UNICODE form.
141146
*
@@ -232,11 +237,10 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
232237

233238
private function hasTransitionalDifference($domain):bool
234239
{
235-
if(!is_string($domain) || empty($domain)){
240+
if(!is_string($domain) || '' === $domain){
236241
return false;
237242
}
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;
241245
}
242246
}

0 commit comments

Comments
 (0)