Skip to content

Commit f94da6b

Browse files
committed
Improve added features
1 parent 082a198 commit f94da6b

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All Notable changes to `PHP Domain Parser` **5.x** series will be documented in
99
- `Pdp\DomainInterface` interface implemented by `Pdp\Domain` and `Pdp\PublicSuffix`
1010
- `Pdp\Domain::getContent` returns the Domain name value replaces `Pdp\Domain::getDomain`
1111
- `Pdp\Domain` implements the `Countable` interface.
12+
- `Pdp\Domain::withPublicSuffix` returns a new Domain object with a different Public Suffix
1213

1314
### Fixed
1415

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ The `Pdp\Domain` implements the `Pdp\DomainInterface`
9090
interface DomainInterface extends Countable, IteratorAggregate
9191
{
9292
public function getContent(): ?string
93-
public function getLabel(int $offset): ?string
93+
public function getLabel(int $key): ?string
9494
public function keys(string $label): int[]
9595
public function toUnicode(): static;
9696
public function toAscii(): static;
@@ -110,6 +110,7 @@ final class Domain implements DomainInterface, JsonSerializable
110110
public function isKnown(): bool;
111111
public function isICANN(): bool;
112112
public function isPrivate(): bool;
113+
public function withPublicSuffix(PublicSuffix $publicSuffix): self;
113114
}
114115
~~~
115116

src/Domain.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ public function getDomain()
220220
/**
221221
* {@inheritdoc}
222222
*/
223-
public function getLabel(int $offset)
223+
public function getLabel(int $key)
224224
{
225-
if ($offset < 0) {
226-
$offset += count($this->labels);
225+
if ($key < 0) {
226+
$key += count($this->labels);
227227
}
228228

229-
return $this->labels[$offset] ?? null;
229+
return $this->labels[$key] ?? null;
230230
}
231231

232232
/**
@@ -309,7 +309,7 @@ public function isPrivate(): bool
309309
/**
310310
* {@inheritdoc}
311311
*/
312-
public function toAscii(): self
312+
public function toAscii()
313313
{
314314
static $pattern = '/[^\x20-\x7f]/';
315315
if (null === $this->domain || !preg_match($pattern, $this->domain)) {
@@ -329,7 +329,7 @@ public function toAscii(): self
329329
/**
330330
* {@inheritdoc}
331331
*/
332-
public function toUnicode(): self
332+
public function toUnicode()
333333
{
334334
if (null === $this->domain || false === strpos($this->domain, 'xn--')) {
335335
return $this;
@@ -345,6 +345,16 @@ public function toUnicode(): self
345345
return $clone;
346346
}
347347

348+
/**
349+
* Returns a new domain name with a different public suffix.
350+
*
351+
* @param PublicSuffix $publicSuffix
352+
*
353+
* @throws Exception if the domain can not contain a public suffix
354+
* @throws Exception if the public suffix can not be assign to the domain name
355+
*
356+
* @return self
357+
*/
348358
public function withPublicSuffix(PublicSuffix $publicSuffix): self
349359
{
350360
if ($this->publicSuffix == $publicSuffix) {
@@ -360,21 +370,18 @@ public function withPublicSuffix(PublicSuffix $publicSuffix): self
360370
return $clone;
361371
}
362372

373+
if (null === $this->domain || false === strpos($this->domain, '.')) {
374+
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
375+
}
376+
363377
static $pattern = '/[^\x20-\x7f]/';
364378
if (preg_match($pattern, $this->domain)) {
365379
$publicSuffix = $publicSuffix->toUnicode();
366380
}
367381

368-
if (null === $this->domain || false === strpos($this->domain, '.') || $this->domain === $publicSuffix->getContent()) {
369-
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
370-
}
371-
372-
if ($publicSuffix->getContent() !== substr($this->domain, - strlen($publicSuffix->getContent()))) {
373-
throw new Exception(sprintf(
374-
'the public suffix `%s` can not be assign to the domain name `%s`',
375-
$publicSuffix->getContent(),
376-
$this->domain
377-
));
382+
$publicSuffixContent = $publicSuffix->getContent();
383+
if ($this->domain === $publicSuffixContent || $publicSuffixContent !== substr($this->domain, - strlen($publicSuffixContent))) {
384+
throw new Exception(sprintf('the public suffix `%s` can not be assign to the domain name `%s`', $publicSuffixContent, $this->domain));
378385
}
379386

380387
$clone = clone $this;

src/PublicSuffix.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ public function getContent()
131131
/**
132132
* {@inheritdoc}
133133
*/
134-
public function getLabel(int $offset)
134+
public function getLabel(int $key)
135135
{
136-
if ($offset < 0) {
137-
$offset += count($this->labels);
136+
if ($key < 0) {
137+
$key += count($this->labels);
138138
}
139139

140-
return $this->labels[$offset] ?? null;
140+
return $this->labels[$key] ?? null;
141141
}
142142

143143
/**

tests/DomainTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ public function withPublicSuffixWorksProvider()
366366
'public suffix' => new PublicSuffix('be', Rules::ICANN_DOMAINS),
367367
'expected' => 'be',
368368
],
369+
'idn domain name' => [
370+
'domain' => new Domain('Яндекс.РФ', new PublicSuffix('рф', Rules::ICANN_DOMAINS)),
371+
'public suffix' => new PublicSuffix('рф', Rules::ICANN_DOMAINS),
372+
'expected' => 'рф',
373+
],
374+
'idn domain name with ascii public suffix' => [
375+
'domain' => new Domain('Яндекс.РФ', new PublicSuffix('рф', Rules::ICANN_DOMAINS)),
376+
'public suffix' => new PublicSuffix('xn--p1ai', Rules::ICANN_DOMAINS),
377+
'expected' => 'рф',
378+
],
369379
];
370380
}
371381

@@ -402,4 +412,15 @@ public function withPublicSuffixFailsProvider()
402412
],
403413
];
404414
}
415+
416+
/**
417+
* @covers ::withPublicSuffix
418+
*/
419+
public function testWithPublicSuffixReturnsInstance()
420+
{
421+
$publicSuffix = new PublicSuffix('ac.be', Rules::ICANN_DOMAINS);
422+
$domain = new Domain('ulb.ac.be', $publicSuffix);
423+
$this->assertSame($domain, $domain->withPublicSuffix($publicSuffix));
424+
$this->assertNotSame($domain, $domain->withPublicSuffix(new PublicSuffix('ac.be', Rules::PRIVATE_DOMAINS)));
425+
}
405426
}

0 commit comments

Comments
 (0)