Skip to content

Commit 89eb5fe

Browse files
committed
Value objects and trait
1 parent d972f37 commit 89eb5fe

File tree

5 files changed

+336
-0
lines changed

5 files changed

+336
-0
lines changed

src/Pdp/Domain.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Public Suffix List PHP: Public Suffix List based URL parsing.
6+
*
7+
* @see http://github.com/jeremykendall/publicsuffixlist-php for the canonical source repository
8+
*
9+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
10+
* @license http://github.com/jeremykendall/publicsuffixlist-php/blob/master/LICENSE MIT License
11+
*/
12+
13+
namespace Pdp;
14+
15+
interface Domain
16+
{
17+
/**
18+
* @return string|null
19+
*/
20+
public function getDomain();
21+
22+
/**
23+
* @return string|null
24+
*/
25+
public function getPublicSuffix();
26+
27+
/**
28+
* Get registrable domain.
29+
*
30+
* Algorithm #7: The registered or registrable domain is the public suffix
31+
* plus one additional label.
32+
*
33+
* This method should return null if the domain provided is a public suffix,
34+
* per the test cases provided by Mozilla.
35+
*
36+
* @see https://publicsuffix.org/list/
37+
* @see https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt
38+
*
39+
* @return string|null registrable domain
40+
*/
41+
public function getRegistrableDomain();
42+
43+
/**
44+
* Does the domain have a matching rule in the Public Suffix List?
45+
*
46+
* WARNING: "Some people use the PSL to determine what is a valid domain name
47+
* and what isn't. This is dangerous, particularly in these days where new
48+
* gTLDs are arriving at a rapid pace, if your software does not regularly
49+
* receive PSL updates, because it will erroneously think new gTLDs are not
50+
* valid. The DNS is the proper source for this information. If you must use
51+
* it for this purpose, please do not bake static copies of the PSL into your
52+
* software with no update mechanism."
53+
*
54+
* @see https://publicsuffix.org/learn/
55+
*
56+
* @return bool
57+
*/
58+
public function isValid(): bool;
59+
}

src/Pdp/LabelsTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Public Suffix List PHP: Public Suffix List based URL parsing.
7+
*
8+
* @see http://github.com/jeremykendall/publicsuffixlist-php for the canonical source repository
9+
*
10+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
11+
* @license http://github.com/jeremykendall/publicsuffixlist-php/blob/master/LICENSE MIT License
12+
*/
13+
namespace Pdp;
14+
15+
trait LabelsTrait
16+
{
17+
private function getLabels(string $input): array
18+
{
19+
return explode('.', $input);
20+
}
21+
22+
private function getLabelsReverse(string $input): array
23+
{
24+
return array_reverse($this->getLabels($input));
25+
}
26+
27+
private function hasLabels(string $input): bool
28+
{
29+
return strpos($input, '.') !== false;
30+
}
31+
32+
private function isSingleLabelDomain(string $domain): bool
33+
{
34+
return !$this->hasLabels($domain);
35+
}
36+
}

src/Pdp/MatchedDomain.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Public Suffix List PHP: Public Suffix List based URL parsing.
7+
*
8+
* @see http://github.com/jeremykendall/publicsuffixlist-php for the canonical source repository
9+
*
10+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
11+
* @license http://github.com/jeremykendall/publicsuffixlist-php/blob/master/LICENSE MIT License
12+
*/
13+
namespace Pdp;
14+
15+
final class MatchedDomain implements Domain
16+
{
17+
use LabelsTrait;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $domain;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $publicSuffix;
28+
29+
/**
30+
* @var bool
31+
*/
32+
private $isValid;
33+
34+
public function __construct(string $domain = null, string $publicSuffix = null, bool $isValid = false)
35+
{
36+
$this->domain = $domain;
37+
$this->publicSuffix = $publicSuffix;
38+
$this->isValid = $isValid;
39+
}
40+
41+
public function getDomain()
42+
{
43+
return $this->domain;
44+
}
45+
46+
public function getPublicSuffix()
47+
{
48+
return $this->publicSuffix;
49+
}
50+
51+
public function isValid(): bool
52+
{
53+
return $this->isValid;
54+
}
55+
56+
public function getRegistrableDomain()
57+
{
58+
if ($this->hasRegistrableDomain($this->publicSuffix) === false) {
59+
return null;
60+
}
61+
62+
$publicSuffixLabels = $this->getLabels($this->publicSuffix);
63+
$domainLabels = $this->getLabels($this->domain);
64+
$additionalLabel = $this->getAdditionalLabel($domainLabels, $publicSuffixLabels);
65+
66+
return implode('.', array_merge($additionalLabel, $publicSuffixLabels));
67+
}
68+
69+
private function hasRegistrableDomain($publicSuffix): bool
70+
{
71+
return !($publicSuffix === null || $this->domain === $publicSuffix || !$this->hasLabels($this->domain));
72+
}
73+
74+
private function getAdditionalLabel($domainLabels, $publicSuffixLabels): array
75+
{
76+
$additionalLabel = array_slice(
77+
$domainLabels,
78+
count($domainLabels) - count($publicSuffixLabels) - 1,
79+
1
80+
);
81+
82+
return $additionalLabel;
83+
}
84+
}

src/Pdp/NullDomain.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Public Suffix List PHP: Public Suffix List based URL parsing.
7+
*
8+
* @see http://github.com/jeremykendall/publicsuffixlist-php for the canonical source repository
9+
*
10+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
11+
* @license http://github.com/jeremykendall/publicsuffixlist-php/blob/master/LICENSE MIT License
12+
*/
13+
14+
namespace Pdp;
15+
16+
class NullDomain implements Domain
17+
{
18+
/**
19+
* @return string|null
20+
*/
21+
public function getDomain()
22+
{
23+
return null;
24+
}
25+
26+
/**
27+
* @return string|null
28+
*/
29+
public function getPublicSuffix()
30+
{
31+
return null;
32+
}
33+
34+
/**
35+
* Get registrable domain.
36+
*
37+
* Algorithm #7: The registered or registrable domain is the public suffix
38+
* plus one additional label.
39+
*
40+
* This method should return null if the domain provided is a public suffix,
41+
* per the test cases provided by Mozilla.
42+
*
43+
* @see https://publicsuffix.org/list/
44+
* @see https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt
45+
*
46+
* @return string|null registrable domain
47+
*/
48+
public function getRegistrableDomain()
49+
{
50+
return null;
51+
}
52+
53+
/**
54+
* Does the domain have a matching rule in the Public Suffix List?
55+
*
56+
* WARNING: "Some people use the PSL to determine what is a valid domain name
57+
* and what isn't. This is dangerous, particularly in these days where new
58+
* gTLDs are arriving at a rapid pace, if your software does not regularly
59+
* receive PSL updates, because it will erroneously think new gTLDs are not
60+
* valid. The DNS is the proper source for this information. If you must use
61+
* it for this purpose, please do not bake static copies of the PSL into your
62+
* software with no update mechanism."
63+
*
64+
* @see https://publicsuffix.org/learn/
65+
*
66+
* @return bool
67+
*/
68+
public function isValid(): bool
69+
{
70+
return false;
71+
}
72+
}

src/Pdp/UnmatchedDomain.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Public Suffix List PHP: Public Suffix List based URL parsing.
7+
*
8+
* @see http://github.com/jeremykendall/publicsuffixlist-php for the canonical source repository
9+
*
10+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
11+
* @license http://github.com/jeremykendall/publicsuffixlist-php/blob/master/LICENSE MIT License
12+
*/
13+
14+
namespace Pdp;
15+
16+
class UnmatchedDomain implements Domain
17+
{
18+
use LabelsTrait;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $domain;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $publicSuffix;
29+
30+
/**
31+
* @var bool
32+
*/
33+
private $isValid;
34+
35+
public function __construct(string $domain = null, string $publicSuffix = null, bool $isValid = false)
36+
{
37+
$this->domain = $domain;
38+
$this->publicSuffix = $publicSuffix;
39+
$this->isValid = $isValid;
40+
}
41+
42+
public function getDomain()
43+
{
44+
return $this->domain;
45+
}
46+
47+
public function getPublicSuffix()
48+
{
49+
return $this->publicSuffix;
50+
}
51+
52+
public function isValid(): bool
53+
{
54+
return $this->isValid;
55+
}
56+
57+
public function getRegistrableDomain()
58+
{
59+
if ($this->hasRegistrableDomain($this->publicSuffix) === false) {
60+
return null;
61+
}
62+
63+
$publicSuffixLabels = $this->getLabels($this->publicSuffix);
64+
$domainLabels = $this->getLabels($this->domain);
65+
$additionalLabel = $this->getAdditionalLabel($domainLabels, $publicSuffixLabels);
66+
67+
return implode('.', array_merge($additionalLabel, $publicSuffixLabels));
68+
}
69+
70+
private function hasRegistrableDomain($publicSuffix): bool
71+
{
72+
return !($publicSuffix === null || $this->domain === $publicSuffix || !$this->hasLabels($this->domain));
73+
}
74+
75+
private function getAdditionalLabel($domainLabels, $publicSuffixLabels): array
76+
{
77+
$additionalLabel = array_slice(
78+
$domainLabels,
79+
count($domainLabels) - count($publicSuffixLabels) - 1,
80+
1
81+
);
82+
83+
return $additionalLabel;
84+
}
85+
}

0 commit comments

Comments
 (0)