Skip to content

Commit c270b9d

Browse files
committed
improve dockblock
1 parent ed676c2 commit c270b9d

File tree

8 files changed

+133
-140
lines changed

8 files changed

+133
-140
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All Notable changes to `PHP Domain Parser` will be documented in this file
44

5-
## 4.0.0 - TBD
5+
## 5.0.0 - TBD
66

77
### Added
88

@@ -16,8 +16,9 @@ All Notable changes to `PHP Domain Parser` will be documented in this file
1616

1717
### Fixed
1818

19-
- Domain class with invalid domain names improved supported
19+
- invalid domain names improved supported
2020
- idn_* conversion error better handled
21+
- domain name with RFC3986 encoded string improved supported
2122

2223
### Deprecated
2324

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ The `Rules` class resolves the submitted domain against the parsed rules from th
256256
~~~php
257257
<?php
258258

259-
final class Domain
259+
final class Domain implements JsonSerializable
260260
{
261261
public function getDomain(): ?string
262262
public function getPublicSuffix(): ?string
@@ -292,6 +292,17 @@ $domain->getSubDomain(); //returns 'www'
292292
$domain->isKnown(); //returns true
293293
$domain->isICANN(); //returns true
294294
$domain->isPrivate(); //returns false
295+
echo json_encode($domain, JSON_PRETTY_PRINT);
296+
// returns
297+
// {
298+
// "domain": "www.ulb.ac.be",
299+
// "registrableDomain": "ulb.ac.be",
300+
// "subDomain": "www",
301+
// "publicSuffix": "ac.be",
302+
// "isKnown": true,
303+
// "isICANN": true,
304+
// "isPrivate": false
305+
// }
295306

296307
//let's resolve the same domain against the PRIVATE DOMAIN SECTION
297308

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"extra": {
6868
"branch-alias": {
69-
"dev-master": "4.x-dev"
69+
"dev-master": "5.x-dev"
7070
}
7171
},
7272
"config": {

src/Domain.php

Lines changed: 86 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@
1111

1212
namespace Pdp;
1313

14+
use JsonSerializable;
15+
1416
/**
1517
* Domain Value Object
1618
*
17-
* Lifted pretty much completely from Jeremy Kendall PDP
18-
* project
19+
* WARNING: "Some people use the PSL to determine what is a valid domain name
20+
* and what isn't. This is dangerous, particularly in these days where new
21+
* gTLDs are arriving at a rapid pace, if your software does not regularly
22+
* receive PSL updates, it will erroneously think new gTLDs are not
23+
* valid. The DNS is the proper source for this innormalizeion. If you must use
24+
* it for this purpose, please do not bake static copies of the PSL into your
25+
* software with no update mechanism."
1926
*
2027
* @author Jeremy Kendall <[email protected]>
2128
* @author Ignace Nyamagana Butera <[email protected]>
2229
*/
23-
final class Domain
30+
final class Domain implements JsonSerializable
2431
{
2532
/**
2633
* @var string|null
@@ -63,8 +70,12 @@ public function __construct($domain = null, PublicSuffix $publicSuffix)
6370
*/
6471
private function setRegistrableDomain()
6572
{
66-
if (!$this->hasRegistrableDomain()) {
67-
return;
73+
if (strpos((string) $this->domain, '.') === false) {
74+
return null;
75+
}
76+
77+
if (in_array($this->publicSuffix->getContent(), [null, $this->domain], true)) {
78+
return null;
6879
}
6980

7081
$nbLabelsToRemove = count($this->publicSuffix) + 1;
@@ -75,18 +86,7 @@ private function setRegistrableDomain()
7586
}
7687

7788
/**
78-
* Tells whether the domain has a registrable domain part.
79-
*
80-
* @return bool
81-
*/
82-
private function hasRegistrableDomain(): bool
83-
{
84-
return strpos((string) $this->domain, '.') > 0
85-
&& !in_array($this->publicSuffix->getContent(), [null, $this->domain], true);
86-
}
87-
88-
/**
89-
* Normalize the domain according to its representation.
89+
* Normalizes the domain according to its representation.
9090
*
9191
* @param string $domain
9292
*
@@ -108,13 +108,13 @@ private function normalize(string $domain)
108108
}
109109

110110
/**
111-
* Compute the sub domain part.
111+
* Computes the sub domain part.
112112
*
113113
* @return string|null
114114
*/
115115
private function setSubDomain()
116116
{
117-
if (!$this->hasRegistrableDomain()) {
117+
if (null === $this->registrableDomain) {
118118
return null;
119119
}
120120

@@ -125,95 +125,61 @@ private function setSubDomain()
125125
return null;
126126
}
127127

128-
$domain = implode('.', array_slice($domainLabels, 0, $countLabels - $nbLabelsToRemove));
128+
$subDomain = implode('.', array_slice($domainLabels, 0, $countLabels - $nbLabelsToRemove));
129129

130-
return $this->normalize($domain);
130+
return $this->normalize($subDomain);
131131
}
132132

133133
/**
134-
* @return string|null
135-
*/
136-
public function getDomain()
137-
{
138-
return $this->domain;
139-
}
140-
141-
/**
142-
* @return string|null
134+
* {@inheritdoc}
143135
*/
144-
public function getPublicSuffix()
136+
public function jsonSerialize()
145137
{
146-
return $this->publicSuffix->getContent();
138+
return [
139+
'domain' => $this->domain,
140+
'registrableDomain' => $this->registrableDomain,
141+
'subDomain' => $this->subDomain,
142+
'publicSuffix' => $this->publicSuffix->getContent(),
143+
'isKnown' => $this->publicSuffix->isKnown(),
144+
'isICANN' => $this->publicSuffix->isICANN(),
145+
'isPrivate' => $this->publicSuffix->isPrivate(),
146+
];
147147
}
148148

149149
/**
150-
* Does the domain have a matching rule in the Public Suffix List?
151-
*
152-
* WARNING: "Some people use the PSL to determine what is a valid domain name
153-
* and what isn't. This is dangerous, particularly in these days where new
154-
* gTLDs are arriving at a rapid pace, if your software does not regularly
155-
* receive PSL updates, because it will erroneously think new gTLDs are not
156-
* valid. The DNS is the proper source for this innormalizeion. If you must use
157-
* it for this purpose, please do not bake static copies of the PSL into your
158-
* software with no update mechanism."
159-
*
160-
* @see https://publicsuffix.org/learn/
161-
*
162-
* @return bool
150+
* {@inheritdoc}
163151
*/
164-
public function isKnown(): bool
152+
public function __debugInfo()
165153
{
166-
return $this->publicSuffix->isKnown();
154+
return $this->jsonSerialize();
167155
}
168156

169157
/**
170-
* Does the domain have a matching rule in the Public Suffix List ICANN section
171-
*
172-
* WARNING: "Some people use the PSL to determine what is a valid domain name
173-
* and what isn't. This is dangerous, particularly in these days where new
174-
* gTLDs are arriving at a rapid pace, if your software does not regularly
175-
* receive PSL updates, because it will erroneously think new gTLDs are not
176-
* valid. The DNS is the proper source for this innormalizeion. If you must use
177-
* it for this purpose, please do not bake static copies of the PSL into your
178-
* software with no update mechanism."
179-
*
180-
* @see https://publicsuffix.org/learn/
181-
*
182-
* @return bool
158+
* {@inheritdoc}
183159
*/
184-
public function isICANN(): bool
160+
public static function __set_state(array $properties)
185161
{
186-
return $this->publicSuffix->isICANN();
162+
return new self($properties['domain'], $properties['publicSuffix']);
187163
}
188164

189165
/**
190-
* Does the domain have a matching rule in the Public Suffix List Private section
191-
*
192-
* WARNING: "Some people use the PSL to determine what is a valid domain name
193-
* and what isn't. This is dangerous, particularly in these days where new
194-
* gTLDs are arriving at a rapid pace, if your software does not regularly
195-
* receive PSL updates, because it will erroneously think new gTLDs are not
196-
* valid. The DNS is the proper source for this innormalizeion. If you must use
197-
* it for this purpose, please do not bake static copies of the PSL into your
198-
* software with no update mechanism."
166+
* Returns the full domain name.
199167
*
200-
* @see https://publicsuffix.org/learn/
168+
* This method should return null on seriously malformed domain name
201169
*
202-
* @return bool
170+
* @return string|null
203171
*/
204-
public function isPrivate(): bool
172+
public function getDomain()
205173
{
206-
return $this->publicSuffix->isPrivate();
174+
return $this->domain;
207175
}
208176

209177
/**
210-
* Get registrable domain.
178+
* Returns the registrable domain.
211179
*
212-
* Algorithm #7: The registered or registrable domain is the public suffix
213-
* plus one additional label.
180+
* The registered or registrable domain is the public suffix plus one additional label.
214181
*
215-
* This method should return null if the domain provided is a public suffix,
216-
* per the test cases provided by Mozilla.
182+
* This method should return null if the registrable domain is the same as the public suffix.
217183
*
218184
* @see https://publicsuffix.org/list/
219185
* @see https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt
@@ -226,12 +192,12 @@ public function getRegistrableDomain()
226192
}
227193

228194
/**
229-
* Get the sub domain.
195+
* Returns the sub domain.
230196
*
231-
* This method should return null if
197+
* The sub domain represents the remaining labels without the registrable domain.
232198
*
233-
* - the registrable domain is null
234-
* - the registrable domain is the same as the public suffix
199+
* This method should return null if the registrable domain is null
200+
* This method should return null if the registrable domain is the same as the public suffix
235201
*
236202
* @return string|null registrable domain
237203
*/
@@ -241,26 +207,48 @@ public function getSubDomain()
241207
}
242208

243209
/**
244-
* {@inheritdoc}
210+
* Returns the public suffix.
211+
*
212+
* @return string|null
245213
*/
246-
public function __debugInfo()
214+
public function getPublicSuffix()
247215
{
248-
return [
249-
'domain' => $this->domain,
250-
'publicSuffix' => $this->publicSuffix->getContent(),
251-
'registrableDomain' => $this->registrableDomain,
252-
'subDomain' => $this->subDomain,
253-
'isKnown' => $this->isKnown(),
254-
'isICANN' => $this->isICANN(),
255-
'isPrivate' => $this->isPrivate(),
256-
];
216+
return $this->publicSuffix->getContent();
257217
}
258218

259219
/**
260-
* {@inheritdoc}
220+
* Tells whether the public suffix has a matching rule in a Public Suffix List.
221+
*
222+
* @see https://publicsuffix.org/learn/
223+
*
224+
* @return bool
261225
*/
262-
public static function __set_state(array $properties)
226+
public function isKnown(): bool
263227
{
264-
return new self($properties['domain'], $properties['publicSuffix']);
228+
return $this->publicSuffix->isKnown();
229+
}
230+
231+
/**
232+
* Tells whether the public suffix has a matching rule in a Public Suffix List ICANN Section.
233+
*
234+
* @see https://publicsuffix.org/learn/
235+
*
236+
* @return bool
237+
*/
238+
public function isICANN(): bool
239+
{
240+
return $this->publicSuffix->isICANN();
241+
}
242+
243+
/**
244+
* Tells whether the public suffix has a matching rule in a Public Suffix List Private Section.
245+
*
246+
* @see https://publicsuffix.org/learn/
247+
*
248+
* @return bool
249+
*/
250+
public function isPrivate(): bool
251+
{
252+
return $this->publicSuffix->isPrivate();
265253
}
266254
}

src/Installer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
/**
1717
* A class to manage PSL ICANN Section rules updates
18+
*
19+
* @author Ignace Nyamagana Butera <[email protected]>
1820
*/
1921
final class Installer
2022
{

src/Manager.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(CacheInterface $cache, HttpClient $http)
4646
}
4747

4848
/**
49-
* Gets ICANN Public Suffix List Rules.
49+
* Gets the Public Suffix List Rules.
5050
*
5151
* @param string $source_url the Public Suffix List URL
5252
*
@@ -66,7 +66,6 @@ public function getRules(string $source_url = self::PSL_URL): Rules
6666

6767
$rules = $this->cache->get($cacheKey);
6868

69-
7069
return new Rules(json_decode($rules, true));
7170
}
7271

@@ -85,10 +84,11 @@ private function getCacheKey(string $str): string
8584
}
8685

8786
/**
88-
* Downloads Public Suffix List and writes text cache and PHP cache. If these files
89-
* already exist, they will be overwritten.
87+
* Downloads, converts and cache the Public Suffix.
88+
*
89+
* If a local cache already exists, it will be overwritten.
9090
*
91-
* Returns true if all list are correctly refreshed
91+
* Returns true if the refresh was successful
9292
*
9393
* @param string $source_url the Public Suffix List URL
9494
*

0 commit comments

Comments
 (0)