Skip to content

Commit bbd49b8

Browse files
committed
Improve Domain Value Object
1 parent 758dfa2 commit bbd49b8

File tree

5 files changed

+107
-38
lines changed

5 files changed

+107
-38
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
All Notable changes to `PHP Domain Parser` **5.x** series will be documented in this file
44

5+
## Next - TBD
6+
7+
### Added
8+
9+
- `Pdp\Domain::getContent` returns the Domain name value replaces `Pdp\Domain::getDomain`
10+
- `Pdp\Domain` implements the `Countable` interface.
11+
12+
### Fixed
13+
14+
- `Pdp\Domain` domain part computation (public suffix, registrable domain and sub domain)
15+
16+
### Deprecated
17+
18+
- `Pdp\Domain::getDomain` use instead `Pdp\Domain::getContent`
19+
20+
### Removed
21+
22+
- None
23+
524
## 5.2.0 - 2018-02-23
625

726
### Added

data/pdp-PSL_FULL_5a3cc7f81795bb2e48e848af42d287b4.cache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/Domain.php

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Pdp;
1313

14+
use Countable;
1415
use JsonSerializable;
1516

1617
/**
@@ -27,7 +28,7 @@
2728
* @author Jeremy Kendall <[email protected]>
2829
* @author Ignace Nyamagana Butera <[email protected]>
2930
*/
30-
final class Domain implements JsonSerializable
31+
final class Domain implements Countable, JsonSerializable
3132
{
3233
use IDNAConverterTrait;
3334

@@ -67,56 +68,72 @@ public static function __set_state(array $properties): self
6768
*/
6869
public function __construct($domain = null, PublicSuffix $publicSuffix = null)
6970
{
70-
if (false !== strpos((string) $domain, '%')) {
71-
$domain = rawurldecode($domain);
71+
$this->domain = $this->setDomain($domain);
72+
$this->publicSuffix = $this->setPublicSuffix($publicSuffix);
73+
$this->registrableDomain = $this->setRegistrableDomain();
74+
$this->subDomain = $this->setSubDomain();
75+
}
76+
77+
/**
78+
* Normalize the given domain.
79+
*
80+
* @param string|null $domain
81+
*
82+
* @return string|null
83+
*/
84+
private function setDomain(string $domain = null)
85+
{
86+
if (null === $domain) {
87+
return null;
7288
}
7389

74-
if (null !== $domain) {
75-
$domain = strtolower($domain);
90+
if (false !== strpos($domain, '%')) {
91+
$domain = rawurldecode($domain);
7692
}
7793

78-
$this->domain = $domain;
79-
$this->publicSuffix = $this->setPublicSuffix($publicSuffix);
80-
$this->registrableDomain = $this->setRegistrableDomain();
81-
$this->subDomain = $this->setSubDomain();
94+
return strtolower($domain);
8295
}
8396

8497
/**
85-
* Filter the PublicSuffix
98+
* Sets the public suffix domain part.
8699
*
87100
* @param PublicSuffix|null $publicSuffix
88101
*
89102
* @return PublicSuffix
90103
*/
91104
private function setPublicSuffix(PublicSuffix $publicSuffix = null): PublicSuffix
92105
{
93-
if (null === $publicSuffix || null === $this->domain) {
106+
$publicSuffix = $publicSuffix ?? new PublicSuffix();
107+
if (null === $publicSuffix->getContent()) {
108+
return $publicSuffix;
109+
}
110+
111+
if (null === $this->domain || false === strpos($this->domain, '.')) {
94112
return new PublicSuffix();
95113
}
96114

97115
return $publicSuffix;
98116
}
99117

100118
/**
101-
* Compute the registrable domain part.
119+
* Computes the registrable domain part.
102120
*
103121
* @return string|null
104122
*/
105123
private function setRegistrableDomain()
106124
{
107-
if (false === strpos((string) $this->domain, '.')) {
125+
if (null === $this->publicSuffix->getContent()) {
108126
return null;
109127
}
110128

111-
if (in_array($this->publicSuffix->getContent(), [null, $this->domain], true)) {
129+
$labels = explode('.', $this->domain);
130+
$countLabels = count($labels);
131+
$countPublicSuffixLabels = count($this->publicSuffix);
132+
if ($countLabels === $countPublicSuffixLabels) {
112133
return null;
113134
}
114135

115-
$nbLabelsToRemove = count($this->publicSuffix) + 1;
116-
$domainLabels = explode('.', $this->domain);
117-
$registrableDomain = implode('.', array_slice($domainLabels, count($domainLabels) - $nbLabelsToRemove));
118-
119-
return $registrableDomain;
136+
return implode('.', array_slice($labels, $countLabels - $countPublicSuffixLabels - 1));
120137
}
121138

122139
/**
@@ -130,16 +147,14 @@ private function setSubDomain()
130147
return null;
131148
}
132149

133-
$nbLabelsToRemove = count($this->publicSuffix) + 1;
134-
$domainLabels = explode('.', $this->domain);
135-
$countLabels = count($domainLabels);
136-
if ($countLabels === $nbLabelsToRemove) {
150+
$labels = explode('.', $this->domain);
151+
$countLabels = count($labels);
152+
$countLabelsToRemove = count(explode('.', $this->registrableDomain));
153+
if ($countLabels === $countLabelsToRemove) {
137154
return null;
138155
}
139156

140-
$subDomain = implode('.', array_slice($domainLabels, 0, $countLabels - $nbLabelsToRemove));
141-
142-
return $subDomain;
157+
return implode('.', array_slice($labels, 0, $countLabels - $countLabelsToRemove));
143158
}
144159

145160
/**
@@ -162,16 +177,45 @@ public function __debugInfo()
162177
return $this->jsonSerialize();
163178
}
164179

180+
/**
181+
* {@inheritdoc}
182+
*/
183+
public function count()
184+
{
185+
if (null === $this->domain) {
186+
return 0;
187+
}
188+
189+
return count(explode('.', $this->domain));
190+
}
191+
192+
/**
193+
* Returns the domain content.
194+
*
195+
* This method should return null on seriously malformed domain name
196+
*
197+
* @return string|null
198+
*/
199+
public function getContent()
200+
{
201+
return $this->domain;
202+
}
203+
165204
/**
166205
* Returns the full domain name.
167206
*
207+
* DEPRECATION WARNING! This method will be removed in the next major point release
208+
*
209+
* @deprecated deprecated since version 5.3
210+
* @see Domain::getContent
211+
*
168212
* This method should return null on seriously malformed domain name
169213
*
170214
* @return string|null
171215
*/
172216
public function getDomain()
173217
{
174-
return $this->domain;
218+
return $this->getContent();
175219
}
176220

177221
/**

src/PublicSuffix.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ public function __debugInfo()
9090
return $this->jsonSerialize();
9191
}
9292

93-
/**
94-
* Returns the public suffix content.
95-
*
96-
* @return string|null
97-
*/
98-
public function getContent()
99-
{
100-
return $this->publicSuffix;
101-
}
102-
10393
/**
10494
* {@inheritdoc}
10595
*/
@@ -112,6 +102,16 @@ public function count()
112102
return count(explode('.', $this->publicSuffix));
113103
}
114104

105+
/**
106+
* Returns the public suffix content.
107+
*
108+
* @return string|null
109+
*/
110+
public function getContent()
111+
{
112+
return $this->publicSuffix;
113+
}
114+
115115
/**
116116
* Tells whether the public suffix has a matching rule in a Public Suffix List.
117117
*

tests/DomainTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function testDomainInternalPhpMethod()
5252
$generateDomain = eval('return '.var_export($domain, true).';');
5353
$this->assertInternalType('array', $domain->__debugInfo());
5454
$this->assertEquals($domain, $generateDomain);
55+
$this->assertCount(4, $domain);
56+
}
57+
58+
public function testDomainWithNullLabels()
59+
{
60+
$this->assertCount(0, new Domain());
5561
}
5662

5763
public function testPublicSuffixnternalPhpMethod()

0 commit comments

Comments
 (0)