Skip to content

Commit ff2bf36

Browse files
committed
IDNA options alternate setters
1 parent 2e68002 commit ff2bf36

File tree

8 files changed

+159
-37
lines changed

8 files changed

+159
-37
lines changed

src/Domain.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -771,22 +771,38 @@ public function withoutLabel(int $key, int ...$keys): self
771771
}
772772

773773
/**
774-
* Set IDNA_* options for functions idn_to_ascii, idn_to_utf8.
774+
* Set IDNA_* options for idn_to_ascii.
775+
*
775776
* @see https://www.php.net/manual/en/intl.constants.php
776777
*
777778
* @param int $asciiIDNAOption
779+
*
780+
* @return self
781+
*/
782+
public function withAsciiIDNAOption(int $asciiIDNAOption): self
783+
{
784+
if ($asciiIDNAOption === $this->asciiIDNAOption) {
785+
return $this;
786+
}
787+
788+
return new self($this->domain, $this->publicSuffix, $asciiIDNAOption, $this->unicodeIDNAOption);
789+
}
790+
791+
/**
792+
* Set IDNA_* options for idn_to_utf8.
793+
*
794+
* @see https://www.php.net/manual/en/intl.constants.php
795+
*
778796
* @param int $unicodeIDNAOption
779797
*
780798
* @return self
781799
*/
782-
public function withIDNAOptions(int $asciiIDNAOption, int $unicodeIDNAOption): self
800+
public function withUnicodeIDNAOption(int $unicodeIDNAOption): self
783801
{
784-
if ($asciiIDNAOption === $this->asciiIDNAOption
785-
&& $unicodeIDNAOption === $this->unicodeIDNAOption
786-
) {
802+
if ($unicodeIDNAOption === $this->unicodeIDNAOption) {
787803
return $this;
788804
}
789805

790-
return new self($this->domain, $this->publicSuffix, $asciiIDNAOption, $unicodeIDNAOption);
806+
return new self($this->domain, $this->publicSuffix, $this->asciiIDNAOption, $unicodeIDNAOption);
791807
}
792808
}

src/PublicSuffix.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,38 @@ public function toUnicode()
359359
}
360360

361361
/**
362-
* Set IDNA_* options for functions idn_to_ascii, idn_to_utf8.
362+
* Set IDNA_* options for idn_to_ascii.
363+
*
363364
* @see https://www.php.net/manual/en/intl.constants.php
364365
*
365366
* @param int $asciiIDNAOption
367+
*
368+
* @return self
369+
*/
370+
public function withAsciiIDNAOption(int $asciiIDNAOption): self
371+
{
372+
if ($asciiIDNAOption === $this->asciiIDNAOption) {
373+
return $this;
374+
}
375+
376+
return new self($this->publicSuffix, $this->section, $asciiIDNAOption, $this->unicodeIDNAOption);
377+
}
378+
379+
/**
380+
* Set IDNA_* options for idn_to_utf8.
381+
*
382+
* @see https://www.php.net/manual/en/intl.constants.php
383+
*
366384
* @param int $unicodeIDNAOption
367385
*
368386
* @return self
369387
*/
370-
public function withIDNAOptions(int $asciiIDNAOption, int $unicodeIDNAOption): self
388+
public function withUnicodeIDNAOption(int $unicodeIDNAOption): self
371389
{
372-
if ($asciiIDNAOption === $this->asciiIDNAOption && $unicodeIDNAOption === $this->unicodeIDNAOption) {
390+
if ($unicodeIDNAOption === $this->unicodeIDNAOption) {
373391
return $this;
374392
}
375393

376-
return new self($this->publicSuffix, $this->section, $asciiIDNAOption, $unicodeIDNAOption);
394+
return new self($this->publicSuffix, $this->section, $this->asciiIDNAOption, $unicodeIDNAOption);
377395
}
378396
}

src/Rules.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,22 +310,42 @@ private function findPublicSuffixFromSection(DomainInterface $domain, string $se
310310
}
311311

312312
/**
313-
* Set IDNA_* options for functions idn_to_ascii, idn_to_utf8.
313+
* Set IDNA_* options for idn_to_ascii.
314+
*
314315
* @see https://www.php.net/manual/en/intl.constants.php
315316
*
316317
* @param int $asciiIDNAOption
317-
* @param int $unicodeIDNAOption
318+
*
319+
* @return self
318320
*/
319-
public function withIDNAOptions(int $asciiIDNAOption, int $unicodeIDNAOption): self
321+
public function withAsciiIDNAOption(int $asciiIDNAOption): self
320322
{
321-
if ($asciiIDNAOption === $this->asciiIDNAOption
322-
&& $unicodeIDNAOption === $this->unicodeIDNAOption
323-
) {
323+
if ($asciiIDNAOption === $this->asciiIDNAOption) {
324324
return $this;
325325
}
326326

327327
$clone = clone $this;
328328
$clone->asciiIDNAOption = $asciiIDNAOption;
329+
330+
return $clone;
331+
}
332+
333+
/**
334+
* Set IDNA_* options for idn_to_utf8.
335+
*
336+
* @see https://www.php.net/manual/en/intl.constants.php
337+
*
338+
* @param int $unicodeIDNAOption
339+
*
340+
* @return self
341+
*/
342+
public function withUnicodeIDNAOption(int $unicodeIDNAOption): self
343+
{
344+
if ($unicodeIDNAOption === $this->unicodeIDNAOption) {
345+
return $this;
346+
}
347+
348+
$clone = clone $this;
329349
$clone->unicodeIDNAOption = $unicodeIDNAOption;
330350

331351
return $clone;

src/TopLevelDomains.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,24 +311,42 @@ public function resolve($domain): Domain
311311
}
312312

313313
/**
314-
* Set IDNA_* options for functions idn_to_ascii, idn_to_utf8.
314+
* Set IDNA_* options for idn_to_ascii.
315+
*
315316
* @see https://www.php.net/manual/en/intl.constants.php
316317
*
317318
* @param int $asciiIDNAOption
318-
* @param int $unicodeIDNAOption
319319
*
320320
* @return self
321321
*/
322-
public function withIDNAOptions(int $asciiIDNAOption, int $unicodeIDNAOption): self
322+
public function withAsciiIDNAOption(int $asciiIDNAOption): self
323323
{
324-
if ($asciiIDNAOption === $this->asciiIDNAOption
325-
&& $unicodeIDNAOption === $this->unicodeIDNAOption
326-
) {
324+
if ($asciiIDNAOption === $this->asciiIDNAOption) {
327325
return $this;
328326
}
329327

330328
$clone = clone $this;
331329
$clone->asciiIDNAOption = $asciiIDNAOption;
330+
331+
return $clone;
332+
}
333+
334+
/**
335+
* Set IDNA_* options for idn_to_utf8.
336+
*
337+
* @see https://www.php.net/manual/en/intl.constants.php
338+
*
339+
* @param int $unicodeIDNAOption
340+
*
341+
* @return self
342+
*/
343+
public function withUnicodeIDNAOption(int $unicodeIDNAOption): self
344+
{
345+
if ($unicodeIDNAOption === $this->unicodeIDNAOption) {
346+
return $this;
347+
}
348+
349+
$clone = clone $this;
332350
$clone->unicodeIDNAOption = $unicodeIDNAOption;
333351

334352
return $clone;

tests/DomainTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,15 +1167,29 @@ public function transitionalProvider()
11671167
}
11681168

11691169
/**
1170-
* @covers ::withIDNAOptions
11711170
* @covers ::getAsciiIDNAOption
11721171
* @covers ::getUnicodeIDNAOption
1172+
* @covers ::withAsciiIDNAOption
1173+
* @covers ::withUnicodeIDNAOption
11731174
*/
11741175
public function testwithIDNAOptions()
11751176
{
11761177
$domain = new Domain('example.com', new PublicSuffix('com'));
1177-
self::assertSame($domain, $domain->withIDNAOptions($domain->getAsciiIDNAOption(), $domain->getUnicodeIDNAOption()));
11781178

1179-
self::assertNotEquals($domain, $domain->withIDNAOptions($domain->getAsciiIDNAOption(), 128));
1179+
self::assertSame($domain, $domain->withAsciiIDNAOption(
1180+
$domain->getAsciiIDNAOption()
1181+
));
1182+
1183+
self::assertNotEquals($domain, $domain->withAsciiIDNAOption(
1184+
128
1185+
));
1186+
1187+
self::assertSame($domain, $domain->withUnicodeIDNAOption(
1188+
$domain->getUnicodeIDNAOption()
1189+
));
1190+
1191+
self::assertNotEquals($domain, $domain->withUnicodeIDNAOption(
1192+
128
1193+
));
11801194
}
11811195
}

tests/PublicSuffixTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,29 @@ public function transitionalProvider()
353353
}
354354

355355
/**
356-
* @covers ::withIDNAOptions
357356
* @covers ::getAsciiIDNAOption
358357
* @covers ::getUnicodeIDNAOption
358+
* @covers ::withAsciiIDNAOption
359+
* @covers ::withUnicodeIDNAOption
359360
*/
360361
public function testwithIDNAOptions()
361362
{
362363
$publicSuffix = new PublicSuffix('com');
363-
self::assertSame($publicSuffix, $publicSuffix->withIDNAOptions($publicSuffix->getAsciiIDNAOption(), $publicSuffix->getUnicodeIDNAOption()));
364364

365-
self::assertNotEquals($publicSuffix, $publicSuffix->withIDNAOptions($publicSuffix->getAsciiIDNAOption(), 128));
365+
self::assertSame($publicSuffix, $publicSuffix->withAsciiIDNAOption(
366+
$publicSuffix->getAsciiIDNAOption()
367+
));
368+
369+
self::assertNotEquals($publicSuffix, $publicSuffix->withAsciiIDNAOption(
370+
128
371+
));
372+
373+
self::assertSame($publicSuffix, $publicSuffix->withUnicodeIDNAOption(
374+
$publicSuffix->getUnicodeIDNAOption()
375+
));
376+
377+
self::assertNotEquals($publicSuffix, $publicSuffix->withUnicodeIDNAOption(
378+
128
379+
));
366380
}
367381
}

tests/RulesTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Pdp\Rules;
2727
use PHPUnit\Framework\TestCase;
2828
use TypeError;
29-
use const IDNA_DEFAULT;
3029

3130
/**
3231
* @coversDefaultClass Pdp\Rules
@@ -81,12 +80,28 @@ public function testDomainInternalPhpMethod()
8180
}
8281

8382
/**
84-
* @covers ::withIDNAOptions
83+
* @covers ::getAsciiIDNAOption
84+
* @covers ::getUnicodeIDNAOption
85+
* @covers ::withAsciiIDNAOption
86+
* @covers ::withUnicodeIDNAOption
8587
*/
8688
public function testwithIDNAOptions()
8789
{
88-
self::assertSame($this->rules, $this->rules->withIDNAOptions(IDNA_DEFAULT, IDNA_DEFAULT));
89-
self::assertNotEquals($this->rules, $this->rules->withIDNAOptions(IDNA_DEFAULT, 128));
90+
self::assertSame($this->rules, $this->rules->withAsciiIDNAOption(
91+
$this->rules->getAsciiIDNAOption()
92+
));
93+
94+
self::assertNotEquals($this->rules, $this->rules->withAsciiIDNAOption(
95+
128
96+
));
97+
98+
self::assertSame($this->rules, $this->rules->withUnicodeIDNAOption(
99+
$this->rules->getUnicodeIDNAOption()
100+
));
101+
102+
self::assertNotEquals($this->rules, $this->rules->withUnicodeIDNAOption(
103+
128
104+
));
90105
}
91106

92107
/**

tests/TopLevelDomainsTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,24 @@ public function testGetterProperties()
9797
/**
9898
* @covers ::getAsciiIDNAOption
9999
* @covers ::getUnicodeIDNAOption
100-
* @covers ::withIDNAOptions
100+
* @covers ::withAsciiIDNAOption
101+
* @covers ::withUnicodeIDNAOption
101102
*/
102103
public function testwithIDNAOptions()
103104
{
104-
self::assertSame($this->collection, $this->collection->withIDNAOptions(
105-
$this->collection->getAsciiIDNAOption(),
105+
self::assertSame($this->collection, $this->collection->withAsciiIDNAOption(
106+
$this->collection->getAsciiIDNAOption()
107+
));
108+
109+
self::assertNotEquals($this->collection, $this->collection->withAsciiIDNAOption(
110+
128
111+
));
112+
113+
self::assertSame($this->collection, $this->collection->withUnicodeIDNAOption(
106114
$this->collection->getUnicodeIDNAOption()
107115
));
108116

109-
self::assertNotEquals($this->collection, $this->collection->withIDNAOptions(
110-
$this->collection->getAsciiIDNAOption(),
117+
self::assertNotEquals($this->collection, $this->collection->withUnicodeIDNAOption(
111118
128
112119
));
113120
}

0 commit comments

Comments
 (0)