Skip to content

Commit c0d563c

Browse files
committed
Improve IDNAConverterTrait
1 parent 85d948c commit c0d563c

File tree

7 files changed

+58
-61
lines changed

7 files changed

+58
-61
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All Notable changes to `PHP Domain Parser` **5.x** series will be documented in
1010

1111
- `PublicSuffix::labels` and `Domain::labels` to return the VO labels see [#241](https://github.com/jeremykendall/php-domain-parser/pull/241)
1212

13+
- `IDNAConverterTrait::parse` (internal)
14+
1315
### Fixed
1416

1517
- Don't swallow cache errors [#232](https://github.com/jeremykendall/php-domain-parser/issues/232)
@@ -21,7 +23,7 @@ All Notable changes to `PHP Domain Parser` **5.x** series will be documented in
2123

2224
### Removed
2325

24-
- None
26+
- IDNAConverterTrait::setLabels (internal)
2527

2628
# 5.4.0 - 2018-11-22
2729

src/Domain.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ public function __construct(
114114
) {
115115
$this->asciiIDNAOption = $asciiIDNAOption;
116116
$this->unicodeIDNAOption = $unicodeIDNAOption;
117-
[$this->labels, $infos] = $this->setLabels($domain, $asciiIDNAOption, $unicodeIDNAOption);
117+
$infos = $this->parse($domain, $asciiIDNAOption, $unicodeIDNAOption);
118+
$this->labels = $infos['labels'];
119+
$this->isTransitionalDifferent = $infos['isTransitionalDifferent'];
118120
if ([] !== $this->labels) {
119121
$this->domain = implode('.', array_reverse($this->labels));
120122
}
@@ -123,7 +125,6 @@ public function __construct(
123125
);
124126
$this->registrableDomain = $this->setRegistrableDomain();
125127
$this->subDomain = $this->setSubDomain();
126-
$this->isTransitionalDifferent = $infos['isTransitionalDifferent'];
127128
}
128129

129130
/**

src/IDNAConverterTrait.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ private function transformToAscii(string $domain, int $option): array
137137
return [strtolower($domain), ['isTransitionalDifferent' => false]];
138138
}
139139

140-
$output = idn_to_ascii($domain, $option, INTL_IDNA_VARIANT_UTS46, $info);
141-
if (0 !== $info['errors']) {
142-
throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($info['errors'])));
140+
$output = idn_to_ascii($domain, $option, INTL_IDNA_VARIANT_UTS46, $infos);
141+
if (0 !== $infos['errors']) {
142+
throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($infos['errors'])));
143143
}
144144

145145
// @codeCoverageIgnoreStart
@@ -149,7 +149,7 @@ private function transformToAscii(string $domain, int $option): array
149149
// @codeCoverageIgnoreEnd
150150

151151
if (false === strpos($output, '%')) {
152-
return [$output, $info];
152+
return [$output, $infos];
153153
}
154154

155155
throw new InvalidDomain(sprintf('The host `%s` is invalid: it contains invalid characters', $domain));
@@ -188,7 +188,7 @@ private function idnToUnicode(string $domain, int $option = IDNA_DEFAULT): strin
188188
* Filter and format the domain to ensure it is valid.
189189
* Returns an array containing the formatted domain name in lowercase
190190
* with its associated labels in reverse order
191-
* For example: setLabels('wWw.uLb.Ac.be') should return ['www.ulb.ac.be', ['be', 'ac', 'ulb', 'www']];.
191+
* For example: parse('wWw.uLb.Ac.be') should return ['www.ulb.ac.be', ['be', 'ac', 'ulb', 'www']];.
192192
*
193193
* @param mixed $domain
194194
* @param int $asciiOption
@@ -198,18 +198,18 @@ private function idnToUnicode(string $domain, int $option = IDNA_DEFAULT): strin
198198
*
199199
* @return array
200200
*/
201-
private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOption = 0): array
201+
private function parse($domain = null, int $asciiOption = 0, int $unicodeOption = 0): array
202202
{
203203
if ($domain instanceof DomainInterface) {
204204
$domain = $domain->getContent();
205205
}
206206

207207
if (null === $domain) {
208-
return [[], ['isTransitionalDifferent' => false]];
208+
return ['labels' => [], 'isTransitionalDifferent' => false];
209209
}
210210

211211
if ('' === $domain) {
212-
return [[''], ['isTransitionalDifferent' => false]];
212+
return ['labels' => [''], 'isTransitionalDifferent' => false];
213213
}
214214

215215
if (!is_scalar($domain) && !method_exists($domain, '__toString')) {
@@ -233,8 +233,8 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
233233
^(?:(?&reg_name)\.){0,126}(?&reg_name)\.?$/ix';
234234
if (1 === preg_match($domain_name, $formatted_domain)) {
235235
return [
236-
array_reverse(explode('.', strtolower($formatted_domain))),
237-
['isTransitionalDifferent' => false],
236+
'labels' => array_reverse(explode('.', strtolower($formatted_domain))),
237+
'isTransitionalDifferent' => false,
238238
];
239239
}
240240

@@ -251,10 +251,8 @@ private function setLabels($domain = null, int $asciiOption = 0, int $unicodeOpt
251251
}
252252

253253
list($ascii_domain, $infos) = $this->transformToAscii($domain, $asciiOption);
254+
$infos['labels'] = array_reverse(explode('.', $this->idnToUnicode($ascii_domain, $unicodeOption)));
254255

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

src/PublicSuffix.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use function array_keys;
2222
use function array_reverse;
2323
use function count;
24-
use function explode;
2524
use function implode;
2625
use function in_array;
2726
use function reset;
@@ -93,12 +92,13 @@ public function __construct(
9392
int $asciiIDNAOption = IDNA_DEFAULT,
9493
int $unicodeIDNAOption = IDNA_DEFAULT
9594
) {
96-
[$this->labels, $infos] = $this->setLabels($publicSuffix, $asciiIDNAOption, $unicodeIDNAOption);
95+
$infos = $this->parse($publicSuffix, $asciiIDNAOption, $unicodeIDNAOption);
96+
$this->labels = $infos['labels'];
97+
$this->isTransitionalDifferent = $infos['isTransitionalDifferent'];
9798
$this->publicSuffix = $this->setPublicSuffix();
9899
$this->section = $this->setSection($section);
99100
$this->asciiIDNAOption = $asciiIDNAOption;
100101
$this->unicodeIDNAOption = $unicodeIDNAOption;
101-
$this->isTransitionalDifferent = $infos['isTransitionalDifferent'];
102102
}
103103

104104
/**
@@ -348,11 +348,7 @@ public function toAscii()
348348
return $this;
349349
}
350350

351-
$clone = clone $this;
352-
$clone->publicSuffix = $publicSuffix;
353-
$clone->labels = array_reverse(explode('.', $publicSuffix));
354-
355-
return $clone;
351+
return new self($publicSuffix, $this->section, $this->asciiIDNAOption, $this->unicodeIDNAOption);
356352
}
357353

358354
/**
@@ -364,11 +360,12 @@ public function toUnicode()
364360
return $this;
365361
}
366362

367-
$clone = clone $this;
368-
$clone->publicSuffix = $this->idnToUnicode($this->publicSuffix, $this->unicodeIDNAOption);
369-
$clone->labels = array_reverse(explode('.', $clone->publicSuffix));
370-
371-
return $clone;
363+
return new self(
364+
$this->idnToUnicode($this->publicSuffix, $this->unicodeIDNAOption),
365+
$this->section,
366+
$this->asciiIDNAOption,
367+
$this->unicodeIDNAOption
368+
);
372369
}
373370

374371
/**

tests/DomainTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function provideWrongConstructor()
9191
/**
9292
* @dataProvider invalidDomainProvider
9393
* @covers ::__construct
94-
* @covers ::setLabels
94+
* @covers ::parse
9595
* @covers ::idnToAscii
9696
* @covers ::getIdnErrors
9797
* @param string $domain
@@ -212,7 +212,7 @@ public function testLabels()
212212
}
213213

214214
/**
215-
* @covers ::setLabels
215+
* @covers ::parse
216216
* @covers ::setPublicSuffix
217217
* @covers ::normalize
218218
* @covers ::setRegistrableDomain
@@ -312,7 +312,7 @@ public function toUnicodeProvider()
312312
}
313313

314314
/**
315-
* @covers ::setLabels
315+
* @covers ::parse
316316
* @covers ::setPublicSuffix
317317
* @covers ::normalize
318318
* @covers ::setRegistrableDomain

tests/PublicSuffixTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testInternalPhpMethod()
5252

5353
/**
5454
* @covers ::__construct
55-
* @covers ::setLabels
55+
* @covers ::parse
5656
* @covers ::setSection
5757
* @covers ::getContent
5858
* @covers ::toUnicode
@@ -64,7 +64,7 @@ public function testPSToUnicodeWithUrlEncode()
6464

6565
/**
6666
* @covers ::__construct
67-
* @covers ::setLabels
67+
* @covers ::parse
6868
* @covers ::setPublicSuffix
6969
* @covers ::setSection
7070
* @covers ::isKnown
@@ -97,7 +97,7 @@ public function PSProvider()
9797

9898
/**
9999
* @covers ::__construct
100-
* @covers ::setLabels
100+
* @covers ::parse
101101
* @covers ::setPublicSuffix
102102
* @dataProvider invalidPublicSuffixProvider
103103
*
@@ -119,7 +119,7 @@ public function invalidPublicSuffixProvider()
119119

120120
/**
121121
* @covers ::__construct
122-
* @covers ::setLabels
122+
* @covers ::parse
123123
* @covers ::idnToAscii
124124
*/
125125
public function testPSToAsciiThrowsException()
@@ -312,31 +312,30 @@ public function testResolveWithCustomIDNAOptions(
312312
[$publicSuffix->getAsciiIDNAOption(), $publicSuffix->getUnicodeIDNAOption()],
313313
[$instance->getAsciiIDNAOption(), $instance->getUnicodeIDNAOption()]
314314
);
315-
self::assertSame($publicSuffix->isTransitionalDifferent(), $publicSuffix->toAscii()->isTransitionalDifferent());
316315
}
317316

318317
public function customIDNAProvider()
319318
{
320319
return [
321-
'without deviation characters'=>[
320+
'without deviation characters' => [
322321
'example.com',
323322
'example.com',
324323
'example.com',
325324
'example.com',
326325
],
327-
'without deviation characters with label'=>[
326+
'without deviation characters with label' => [
328327
'www.example.com',
329328
'www.example.com',
330329
'www.example.com',
331330
'www.example.com',
332331
],
333-
'with deviation in domain'=>[
332+
'with deviation in domain' => [
334333
'www.faß.de',
335334
'www.faß.de',
336335
'www.xn--fa-hia.de',
337336
'www.faß.de',
338337
],
339-
'with deviation in label'=>[
338+
'with deviation in label' => [
340339
'faß.test.de',
341340
'faß.test.de',
342341
'xn--fa-hia.test.de',

0 commit comments

Comments
 (0)