Skip to content

Commit cd738ca

Browse files
committed
deprecate the PublicSuffixListSection::ALL_DOMAINS constant
1 parent 5790a80 commit cd738ca

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ All Notable changes to `PHP Domain Parser` **5.x** series will be documented in
2727
### Deprecated
2828

2929
- `Pdp\Domain::getDomain` use instead `Pdp\Domain::getContent`
30+
- `Pdp\PublicSuffixListSection::ALL_DOMAINS` use the empty string instead
3031

3132
### Removed
3233

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function Domain::getContent(): ?string
9494
public function Domain::getPublicSuffix(): ?string
9595
public function Domain::getRegistrableDomain(): ?string
9696
public function Domain::getSubDomain(); ?string
97+
public function Domain::isResolvable(): bool;
9798
public function Domain::isKnown(): bool;
9899
public function Domain::isICANN(): bool;
99100
public function Domain::isPrivate(): bool;
@@ -157,11 +158,11 @@ The `Pdp\Rules` object is responsible for public suffix resolution for a given d
157158

158159
- `$domain` a domain name as a string
159160
- `$section` a string which specifies which section of the PSL you want to validate the given domain against. The possible values are:
160-
- `Rules::ALL_DOMAINS`, to validate against the full PSL.
161161
- `Rules::ICANN_DOMAINS`, to validate against the PSL ICANN DOMAINS section only.
162162
- `Rules::PRIVATE_DOMAINS`, to validate against the PSL PRIVATE DOMAINS section only.
163+
- the empty string to validate against all the PSL sections.
163164

164-
By default, the `$section` argument is equal to `Rules::ALL_DOMAINS`. If an unsupported section is submitted a `Pdp\Exception` exception will be thrown.
165+
By default, the `$section` argument is equal to the empty string. If an unsupported section is submitted a `Pdp\Exception` exception will be thrown.
165166

166167
While the `Pdp\Rules::resolve` returns a `Pdp\Domain` object, the `Pdp\Rules::getPublicSuffix` returns a `Pdp\PublicSuffix` object.
167168

@@ -176,7 +177,7 @@ use Pdp\Converter;
176177
$pdp_url = 'https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat';
177178
$rules = Rules::createFromPath($pdp_url);
178179

179-
$domain = $rules->resolve('www.Ulb.AC.be'); //using Rules::ALL_DOMAINS
180+
$domain = $rules->resolve('www.Ulb.AC.be'); // resolution is done against all the sections available
180181
echo json_encode($domain, JSON_PRETTY_PRINT);
181182
// returns
182183
// {

src/PublicSuffix.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,22 @@ public function __construct($publicSuffix = null, string $section = '')
9090
*
9191
* @param string $section
9292
*
93+
* @throws Exception if the submitted section is not supported
94+
*
9395
* @return string
9496
*/
9597
private function setSection(string $section): string
9698
{
97-
if ('' === $this->publicSuffix || null === $this->publicSuffix) {
99+
if (in_array($this->publicSuffix, ['', null], true) || self::ALL_DOMAINS === $section) {
98100
return '';
99101
}
100102

101-
return $section;
103+
static $section_list = [self::PRIVATE_DOMAINS, self::ICANN_DOMAINS, ''];
104+
if (in_array($section, $section_list, true)) {
105+
return $section;
106+
}
107+
108+
throw new Exception(sprintf('`%s` is an unknown Public Suffix List section', $section));
102109
}
103110

104111
/**

src/PublicSuffixListSection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
*/
1919
interface PublicSuffixListSection
2020
{
21+
/**
22+
* @deprecated 5.3
23+
*/
2124
const ALL_DOMAINS = 'ALL_DOMAINS';
25+
2226
const ICANN_DOMAINS = 'ICANN_DOMAINS';
27+
2328
const PRIVATE_DOMAINS = 'PRIVATE_DOMAINS';
2429
}

src/Rules.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __construct(array $rules)
100100
*/
101101
public function getPublicSuffix($domain = null, string $section = self::ALL_DOMAINS): PublicSuffix
102102
{
103-
$this->validateSection($section);
103+
$section = $this->validateSection($section);
104104
$domain = $domain instanceof Domain ? $domain : new Domain($domain);
105105
if (!$domain->isResolvable()) {
106106
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $domain->getContent()));
@@ -119,7 +119,7 @@ public function getPublicSuffix($domain = null, string $section = self::ALL_DOMA
119119
*/
120120
public function resolve($domain = null, string $section = self::ALL_DOMAINS): Domain
121121
{
122-
$this->validateSection($section);
122+
$section = $this->validateSection($section);
123123
try {
124124
$domain = $domain instanceof Domain ? $domain : new Domain($domain);
125125
if (!$domain->isResolvable()) {
@@ -138,16 +138,18 @@ public function resolve($domain = null, string $section = self::ALL_DOMAINS): Do
138138
* @param string $section
139139
*
140140
* @throws Exception if the submitted section is not supported
141+
*
142+
* @return string
141143
*/
142-
private function validateSection(string $section)
144+
private function validateSection(string $section): string
143145
{
144-
if (self::ALL_DOMAINS === $section) {
145-
return;
146+
if (self::ALL_DOMAINS === $section || '' === $section) {
147+
return '';
146148
}
147149

148150
$rules = $this->rules[$section] ?? null;
149151
if (is_array($rules)) {
150-
return;
152+
return $section;
151153
}
152154

153155
throw new Exception(sprintf('%s is an unknown Public Suffix List section', $section));
@@ -174,11 +176,11 @@ private function findPublicSuffix(DomainInterface $domain, string $section): Pub
174176
return $private;
175177
}
176178

177-
if (self::ALL_DOMAINS === $section) {
178-
return $icann;
179+
if (self::PRIVATE_DOMAINS === $section) {
180+
return new PublicSuffix($domain->getLabel(0));
179181
}
180182

181-
return new PublicSuffix($domain->getLabel(0));
183+
return $icann;
182184
}
183185

184186
/**

tests/PublicSuffixTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public function testPSToAsciiThrowsException()
5757
new PublicSuffix('a⒈com');
5858
}
5959

60+
/**
61+
* @covers ::__construct
62+
* @covers ::setSection
63+
*/
64+
public function testSetSectionThrowsException()
65+
{
66+
$this->expectException(Exception::class);
67+
new PublicSuffix('ac.be', 'foobar');
68+
}
69+
6070
/**
6171
* @covers ::toUnicode
6272
* @covers ::idnToUnicode

0 commit comments

Comments
 (0)