Skip to content

Commit 5dd957f

Browse files
committed
fix #234 resolve isTransitionalDifferent method
1 parent 374390e commit 5dd957f

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/Domain.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ final class Domain implements DomainInterface, JsonSerializable
9898
* @var bool
9999
*/
100100
private $isTransitionalDifferent;
101+
101102
/**
102103
* {@inheritdoc}
103104
*/
@@ -127,6 +128,7 @@ public function __construct(
127128
$this->asciiIDNAOption = $asciiIDNAOption;
128129
$this->unicodeIDNAOption = $unicodeIDNAOption;
129130
$this->labels = $this->setLabels($domain, $asciiIDNAOption, $unicodeIDNAOption);
131+
$this->isTransitionalDifferent = $this->hasTransitionalDifference($domain);
130132
if ([] !== $this->labels) {
131133
$this->domain = implode('.', array_reverse($this->labels));
132134
}
@@ -375,7 +377,7 @@ public function getPublicSuffix()
375377
*/
376378
public function isResolvable(): bool
377379
{
378-
return 1 < count($this->labels ?? []) && '.' !== substr($this->domain, -1, 1);
380+
return 1 < count($this->labels) && '.' !== substr($this->domain, -1, 1);
379381
}
380382

381383
/**
@@ -761,13 +763,6 @@ public function withIDNAOptions(int $forAscii, int $forUnicode)
761763
**/
762764
public function isTransitionalDifferent(): bool
763765
{
764-
if ($this->isTransitionalDifferent === null) {
765-
try {
766-
$this->idnToAscii($this->getContent(), $this->asciiIDNAOption);
767-
} catch (Throwable $e) {
768-
$this->isTransitionalDifferent = false;
769-
}
770-
}
771766
return $this->isTransitionalDifferent;
772767
}
773768
}

src/IDNAConverterTrait.php

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

1616
namespace Pdp;
1717

18+
use function is_string;
1819
use Pdp\Exception\InvalidDomain;
1920
use TypeError;
2021
use UnexpectedValueException;
@@ -115,7 +116,6 @@ private function idnToAscii(string $domain, int $IDNAOption = IDNA_DEFAULT): str
115116
$domain = rawurldecode($domain);
116117
static $pattern = '/[^\x20-\x7f]/';
117118
if (!preg_match($pattern, $domain)) {
118-
$this->isTransitionalDifferent = false;
119119
return strtolower($domain);
120120
}
121121

@@ -129,11 +129,7 @@ private function idnToAscii(string $domain, int $IDNAOption = IDNA_DEFAULT): str
129129
throw new UnexpectedValueException(sprintf('The Intl extension is misconfigured for %s, please correct this issue before proceeding.', PHP_OS));
130130
}
131131
// @codeCoverageIgnoreEnd
132-
133-
if (property_exists($this, 'isTransitionalDifferent')) {
134-
$this->isTransitionalDifferent = isset($arr['isTransitionalDifferent'])
135-
&& (bool)$arr['isTransitionalDifferent'] === true;
136-
}
132+
137133
if (false === strpos($output, '%')) {
138134
return $output;
139135
}
@@ -234,4 +230,14 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
234230

235231
return array_reverse(explode('.', $this->idnToUnicode($ascii_domain, $unicodeOption)));
236232
}
233+
234+
private function hasTransitionalDifference($domain):bool
235+
{
236+
if(!is_string($domain) || empty($domain)){
237+
return false;
238+
}
239+
$info = [];
240+
idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46, $info);
241+
return (bool)$info['isTransitionalDifferent'] === true;
242+
}
237243
}

src/PublicSuffix.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function __construct(
130130
int $unicodeIDNAOption = IDNA_DEFAULT
131131
) {
132132
$this->labels = $this->setLabels($publicSuffix, $asciiIDNAOption, $unicodeIDNAOption);
133+
$this->isTransitionalDifferent = $this->hasTransitionalDifference($publicSuffix);
133134
$this->publicSuffix = $this->setPublicSuffix();
134135
$this->section = $this->setSection($section);
135136
$this->asciiIDNAOption = $asciiIDNAOption;
@@ -330,20 +331,14 @@ public function getUnicodeIDNAOption(): int
330331
{
331332
return $this->unicodeIDNAOption;
332333
}
334+
333335
/**
334336
* return true if domain contains deviation characters.
335337
* @see http://unicode.org/reports/tr46/#Transition_Considerations
336338
* @return bool
337339
**/
338340
public function isTransitionalDifferent(): bool
339341
{
340-
if ($this->isTransitionalDifferent === null) {
341-
try {
342-
$this->idnToAscii($this->getContent());
343-
} catch (Throwable $e) {
344-
$this->isTransitionalDifferent = false;
345-
}
346-
}
347342
return $this->isTransitionalDifferent;
348343
}
349344
}

tests/DomainTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace Pdp\Tests;
1717

18+
use function array_map;
19+
use function array_reduce;
20+
use function ord;
1821
use Pdp\Domain;
1922
use Pdp\Exception\CouldNotResolvePublicSuffix;
2023
use Pdp\Exception\CouldNotResolveSubDomain;
@@ -24,6 +27,8 @@
2427
use Pdp\PublicSuffix;
2528
use Pdp\Rules;
2629
use PHPUnit\Framework\TestCase;
30+
use function print_r;
31+
use function str_split;
2732
use TypeError;
2833

2934
/**

0 commit comments

Comments
 (0)