Skip to content

Commit 85d948c

Browse files
committed
Reduce call to intl extension
1 parent 67d676c commit 85d948c

File tree

4 files changed

+64
-65
lines changed

4 files changed

+64
-65
lines changed

src/Domain.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,6 @@ final class Domain implements DomainInterface, JsonSerializable
9999
*/
100100
private $isTransitionalDifferent;
101101

102-
/**
103-
* {@inheritdoc}
104-
*/
105-
public static function __set_state(array $properties): self
106-
{
107-
return new self(
108-
$properties['domain'],
109-
$properties['publicSuffix'],
110-
$properties['asciiIDNAOption'] ?? IDNA_DEFAULT,
111-
$properties['unicodeIDNAOption'] ?? IDNA_DEFAULT
112-
);
113-
}
114-
115102
/**
116103
* New instance.
117104
* @param null|mixed $domain
@@ -127,8 +114,7 @@ public function __construct(
127114
) {
128115
$this->asciiIDNAOption = $asciiIDNAOption;
129116
$this->unicodeIDNAOption = $unicodeIDNAOption;
130-
$this->labels = $this->setLabels($domain, $asciiIDNAOption, $unicodeIDNAOption);
131-
$this->isTransitionalDifferent = $this->hasTransitionalDifference($domain);
117+
[$this->labels, $infos] = $this->setLabels($domain, $asciiIDNAOption, $unicodeIDNAOption);
132118
if ([] !== $this->labels) {
133119
$this->domain = implode('.', array_reverse($this->labels));
134120
}
@@ -137,6 +123,20 @@ public function __construct(
137123
);
138124
$this->registrableDomain = $this->setRegistrableDomain();
139125
$this->subDomain = $this->setSubDomain();
126+
$this->isTransitionalDifferent = $infos['isTransitionalDifferent'];
127+
}
128+
129+
/**
130+
* {@inheritdoc}
131+
*/
132+
public static function __set_state(array $properties): self
133+
{
134+
return new self(
135+
$properties['domain'],
136+
$properties['publicSuffix'],
137+
$properties['asciiIDNAOption'] ?? IDNA_DEFAULT,
138+
$properties['unicodeIDNAOption'] ?? IDNA_DEFAULT
139+
);
140140
}
141141

142142
/**

src/IDNAConverterTrait.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use function idn_to_utf8;
2626
use function implode;
2727
use function is_scalar;
28-
use function is_string;
2928
use function method_exists;
3029
use function preg_match;
3130
use function rawurldecode;
@@ -135,7 +134,7 @@ private function transformToAscii(string $domain, int $option): array
135134
static $pattern = '/[^\x20-\x7f]/';
136135

137136
if (1 !== preg_match($pattern, $domain)) {
138-
return [strtolower($domain), false];
137+
return [strtolower($domain), ['isTransitionalDifferent' => false]];
139138
}
140139

141140
$output = idn_to_ascii($domain, $option, INTL_IDNA_VARIANT_UTS46, $info);
@@ -150,7 +149,7 @@ private function transformToAscii(string $domain, int $option): array
150149
// @codeCoverageIgnoreEnd
151150

152151
if (false === strpos($output, '%')) {
153-
return [$output, $info['isTransitionalDifferent']];
152+
return [$output, $info];
154153
}
155154

156155
throw new InvalidDomain(sprintf('The host `%s` is invalid: it contains invalid characters', $domain));
@@ -190,11 +189,14 @@ private function idnToUnicode(string $domain, int $option = IDNA_DEFAULT): strin
190189
* Returns an array containing the formatted domain name in lowercase
191190
* with its associated labels in reverse order
192191
* For example: setLabels('wWw.uLb.Ac.be') should return ['www.ulb.ac.be', ['be', 'ac', 'ulb', 'www']];.
193-
* @param mixed $domain
194-
* @param int $asciiOption
195-
* @param int $unicodeOption
192+
*
193+
* @param mixed $domain
194+
* @param int $asciiOption
195+
* @param int $unicodeOption
196+
*
196197
* @throws InvalidDomain If the domain is invalid
197-
* @return string[]
198+
*
199+
* @return array
198200
*/
199201
private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOption = 0): array
200202
{
@@ -203,11 +205,11 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
203205
}
204206

205207
if (null === $domain) {
206-
return [];
208+
return [[], ['isTransitionalDifferent' => false]];
207209
}
208210

209211
if ('' === $domain) {
210-
return [''];
212+
return [[''], ['isTransitionalDifferent' => false]];
211213
}
212214

213215
if (!is_scalar($domain) && !method_exists($domain, '__toString')) {
@@ -230,7 +232,10 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
230232
)
231233
^(?:(?&reg_name)\.){0,126}(?&reg_name)\.?$/ix';
232234
if (1 === preg_match($domain_name, $formatted_domain)) {
233-
return array_reverse(explode('.', strtolower($formatted_domain)));
235+
return [
236+
array_reverse(explode('.', strtolower($formatted_domain))),
237+
['isTransitionalDifferent' => false],
238+
];
234239
}
235240

236241
// a domain name can not contains URI delimiters or space
@@ -245,18 +250,11 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
245250
throw new InvalidDomain(sprintf('The domain `%s` is invalid: the labels are malformed', $domain));
246251
}
247252

248-
$ascii_domain = $this->idnToAscii($domain, $asciiOption);
249-
250-
return array_reverse(explode('.', $this->idnToUnicode($ascii_domain, $unicodeOption)));
251-
}
252-
253-
private function hasTransitionalDifference($domain): bool
254-
{
255-
if (!is_string($domain) || '' === $domain) {
256-
return false;
257-
}
258-
list(, $isTransitionalDifferent) = $this->transformToAscii($domain, IDNA_DEFAULT);
253+
list($ascii_domain, $infos) = $this->transformToAscii($domain, $asciiOption);
259254

260-
return $isTransitionalDifferent;
255+
return [
256+
array_reverse(explode('.', $this->idnToUnicode($ascii_domain, $unicodeOption))),
257+
$infos,
258+
];
261259
}
262260
}

src/PublicSuffix.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ final class PublicSuffix implements DomainInterface, JsonSerializable, PublicSuf
7979
* @var bool
8080
*/
8181
private $isTransitionalDifferent;
82+
83+
/**
84+
* New instance.
85+
* @param mixed $publicSuffix
86+
* @param string $section
87+
* @param int $asciiIDNAOption
88+
* @param int $unicodeIDNAOption
89+
*/
90+
public function __construct(
91+
$publicSuffix = null,
92+
string $section = '',
93+
int $asciiIDNAOption = IDNA_DEFAULT,
94+
int $unicodeIDNAOption = IDNA_DEFAULT
95+
) {
96+
[$this->labels, $infos] = $this->setLabels($publicSuffix, $asciiIDNAOption, $unicodeIDNAOption);
97+
$this->publicSuffix = $this->setPublicSuffix();
98+
$this->section = $this->setSection($section);
99+
$this->asciiIDNAOption = $asciiIDNAOption;
100+
$this->unicodeIDNAOption = $unicodeIDNAOption;
101+
$this->isTransitionalDifferent = $infos['isTransitionalDifferent'];
102+
}
103+
82104
/**
83105
* {@inheritdoc}
84106
*/
@@ -116,27 +138,6 @@ public static function createFromDomain(Domain $domain): self
116138
);
117139
}
118140

119-
/**
120-
* New instance.
121-
* @param mixed $publicSuffix
122-
* @param string $section
123-
* @param int $asciiIDNAOption
124-
* @param int $unicodeIDNAOption
125-
*/
126-
public function __construct(
127-
$publicSuffix = null,
128-
string $section = '',
129-
int $asciiIDNAOption = IDNA_DEFAULT,
130-
int $unicodeIDNAOption = IDNA_DEFAULT
131-
) {
132-
$this->labels = $this->setLabels($publicSuffix, $asciiIDNAOption, $unicodeIDNAOption);
133-
$this->isTransitionalDifferent = $this->hasTransitionalDifference($publicSuffix);
134-
$this->publicSuffix = $this->setPublicSuffix();
135-
$this->section = $this->setSection($section);
136-
$this->asciiIDNAOption = $asciiIDNAOption;
137-
$this->unicodeIDNAOption = $unicodeIDNAOption;
138-
}
139-
140141
/**
141142
* Set the public suffix content.
142143
*

tests/DomainTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,13 +1184,13 @@ public function testIsTransitionalDifference(Domain $domain, bool $expected)
11841184
public function transitionalProvider()
11851185
{
11861186
return [
1187-
'simple' => [new Domain('example.com', new PublicSuffix('com')), false],
1188-
'idna' => [new Domain('français.fr', new PublicSuffix('fr')), false],
1189-
'in domain' => [new Domain('faß.de', new PublicSuffix('de')), true],
1190-
'in domain 2' => [new Domain('βόλος.com', new PublicSuffix('com')), true],
1191-
'in domain 3' => [new Domain('ශ්‍රී.com', new PublicSuffix('com')), true],
1192-
'in domain 4' => [new Domain('نامه‌ای.com', new PublicSuffix('com')), true],
1193-
'in label' => [new Domain('faß.test.de', new PublicSuffix('de')), true],
1187+
'simple' => [new Domain('example.com'), false],
1188+
'idna' => [new Domain('français.fr'), false],
1189+
'in domain 1' => [new Domain('faß.de'), true],
1190+
'in domain 2' => [new Domain('βόλος.com'), true],
1191+
'in domain 3' => [new Domain('ශ්‍රී.com'), true],
1192+
'in domain 4' => [new Domain('نامه‌ای.com'), true],
1193+
'in domain 5' => [new Domain('faß.test.de'), true],
11941194
];
11951195
}
11961196

0 commit comments

Comments
 (0)