Skip to content

Commit 07ffb7a

Browse files
committed
move constants from PublicSuffixManager to PublicSuffixList
1 parent 5a33ba8 commit 07ffb7a

8 files changed

+125
-99
lines changed

src/LabelsTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,17 @@ private function isSingleLabelDomain(string $domain): bool
6060
{
6161
return !$this->hasLabels($domain);
6262
}
63+
64+
/**
65+
* Returns the additional label to generate the registrable domain.
66+
*
67+
* @param string[] $domainLabels
68+
* @param string[] $publicSuffixLabels
69+
*
70+
* @return string[]
71+
*/
72+
private function getAdditionalLabel($domainLabels, $publicSuffixLabels): array
73+
{
74+
return array_slice($domainLabels, count($domainLabels) - count($publicSuffixLabels) - 1, 1);
75+
}
6376
}

src/MatchedDomain.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ final class MatchedDomain implements Domain
2828
/**
2929
* New instance.
3030
*
31-
* @param string|null $domain
32-
* @param string|null $publicSuffix
31+
* @param string $domain
32+
* @param string $publicSuffix
3333
*/
34-
public function __construct(string $domain = null, string $publicSuffix = null)
34+
public function __construct(string $domain, string $publicSuffix)
3535
{
3636
$this->domain = $domain;
3737
$this->publicSuffix = $publicSuffix;
@@ -88,27 +88,6 @@ private function hasRegistrableDomain(): bool
8888
return false;
8989
}
9090

91-
if ($this->publicSuffix === null) {
92-
return false;
93-
}
94-
95-
if ($this->publicSuffix === $this->domain) {
96-
return false;
97-
}
98-
99-
return true;
100-
}
101-
102-
/**
103-
* Returns the additional label to generate the registrable domain.
104-
*
105-
* @param string[] $domainLabels
106-
* @param string[] $publicSuffixLabels
107-
*
108-
* @return string[]
109-
*/
110-
private function getAdditionalLabel($domainLabels, $publicSuffixLabels): array
111-
{
112-
return array_slice($domainLabels, count($domainLabels) - count($publicSuffixLabels) - 1, 1);
91+
return $this->publicSuffix !== $this->domain;
11392
}
11493
}

src/PublicSuffixList.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111

1212
namespace Pdp;
1313

14+
use OutOfRangeException;
15+
1416
final class PublicSuffixList
1517
{
1618
use LabelsTrait;
1719

20+
const ALL_DOMAINS = 'ALL';
21+
const ICANN_DOMAINS = 'ICANN';
22+
const PRIVATE_DOMAINS = 'PRIVATE';
23+
1824
/**
1925
* @var array
2026
*/
@@ -23,21 +29,52 @@ final class PublicSuffixList
2329
/**
2430
* PublicSuffixList constructor.
2531
*
26-
* @param array $rules
32+
* @param string $type Tje PSL type 'ALL, ICANN or PRIVATE'
33+
* @param array $rules
2734
*/
28-
public function __construct(array $rules)
35+
public function __construct(string $type, array $rules)
2936
{
37+
static $type_lists = [
38+
self::ALL_DOMAINS => 1,
39+
self::ICANN_DOMAINS => 1,
40+
self::PRIVATE_DOMAINS => 1,
41+
];
42+
43+
if (!isset($type_lists[$type])) {
44+
throw new OutOfRangeException(sprintf('Unknown PSL type %s', $type));
45+
}
46+
$this->type = $type;
3047
$this->rules = $rules;
3148
}
3249

3350
/**
34-
* Returns PSL rules.
51+
* Tell whether the PSL resolves ICANN domains only.
3552
*
36-
* @return array
53+
* @return bool
54+
*/
55+
public function isICANN()
56+
{
57+
return $this->type === self::ICANN_DOMAINS;
58+
}
59+
60+
/**
61+
* Tell whether the PSL resolves private domains only.
62+
*
63+
* @return bool
64+
*/
65+
public function isPrivate()
66+
{
67+
return $this->type === self::PRIVATE_DOMAINS;
68+
}
69+
70+
/**
71+
* Tell whether the PSL resolves all domains.
72+
*
73+
* @return bool
3774
*/
38-
public function getRules(): array
75+
public function isAll()
3976
{
40-
return $this->rules;
77+
return $this->type === self::ALL_DOMAINS;
4178
}
4279

4380
/**

src/PublicSuffixListManager.php

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
class PublicSuffixListManager
2525
{
2626
const PUBLIC_SUFFIX_LIST_URL = 'https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat';
27-
const ALL_DOMAINS = 'ALL';
28-
const ICANN_DOMAINS = 'ICANN';
29-
const PRIVATE_DOMAINS = 'PRIVATE';
3027

3128
/**
3229
* @var string Public Suffix List Source URL
@@ -67,22 +64,22 @@ public function __construct(
6764
*
6865
* @return PublicSuffixList
6966
*/
70-
public function getList($type = self::ALL_DOMAINS): PublicSuffixList
67+
public function getList($type = PublicSuffixList::ALL_DOMAINS): PublicSuffixList
7168
{
7269
static $type_lists = [
73-
self::ALL_DOMAINS => self::ALL_DOMAINS,
74-
self::ICANN_DOMAINS => self::ICANN_DOMAINS,
75-
self::PRIVATE_DOMAINS => self::PRIVATE_DOMAINS,
70+
PublicSuffixList::ALL_DOMAINS => PublicSuffixList::ALL_DOMAINS,
71+
PublicSuffixList::ICANN_DOMAINS => PublicSuffixList::ICANN_DOMAINS,
72+
PublicSuffixList::PRIVATE_DOMAINS => PublicSuffixList::PRIVATE_DOMAINS,
7673
];
7774

78-
$type = $type_lists[$type] ?? self::ALL_DOMAINS;
75+
$type = $type_lists[$type] ?? PublicSuffixList::ALL_DOMAINS;
7976
$list = $this->cacheAdapter->get($type);
8077
if ($list === null) {
8178
$this->refreshPublicSuffixList();
8279
$list = $this->cacheAdapter->get($type);
8380
}
8481

85-
return new PublicSuffixList(json_decode($list, true));
82+
return new PublicSuffixList($type, json_decode($list, true));
8683
}
8784

8885
/**
@@ -112,14 +109,14 @@ public function refreshPublicSuffixList(): bool
112109
private function convertListToArray(string $publicSuffixList): array
113110
{
114111
$addDomain = [
115-
self::ICANN_DOMAINS => false,
116-
self::PRIVATE_DOMAINS => false,
112+
PublicSuffixList::ICANN_DOMAINS => false,
113+
PublicSuffixList::PRIVATE_DOMAINS => false,
117114
];
118115

119116
$publicSuffixListTypes = [
120-
self::ALL_DOMAINS => [],
121-
self::ICANN_DOMAINS => [],
122-
self::PRIVATE_DOMAINS => [],
117+
PublicSuffixList::ALL_DOMAINS => [],
118+
PublicSuffixList::ICANN_DOMAINS => [],
119+
PublicSuffixList::PRIVATE_DOMAINS => [],
123120
];
124121

125122
$data = new SplTempFileObject();
@@ -178,21 +175,21 @@ private function isValidSection(bool $previousStatus, string $line, string $sect
178175
/**
179176
* Convert a line from the Public Suffix list.
180177
*
181-
* @param string $textLine Public Suffix List text line
178+
* @param string $rule Public Suffix List text line
182179
* @param array $publicSuffixListTypes Associative, multidimensional array representation of the
183180
* public suffx list
184-
* @param array $addDomain Tell which section should be converted
181+
* @param array $validTypes Tell which section should be converted
185182
*
186183
* @return array Associative, multidimensional array representation of the
187184
* public suffx list
188185
*/
189-
private function convertLineToArray(string $textLine, array $publicSuffixListTypes, array $addDomain): array
186+
private function convertLineToArray(string $line, array $publicSuffixListTypes, array $validTypes): array
190187
{
191-
$ruleParts = explode('.', $textLine);
192-
$this->buildArray($publicSuffixListTypes[self::ALL_DOMAINS], $ruleParts);
193-
$domainNames = array_keys(array_filter($addDomain));
194-
foreach ($domainNames as $domainName) {
195-
$this->buildArray($publicSuffixListTypes[$domainName], $ruleParts);
188+
$ruleParts = explode('.', $line);
189+
$validTypes = array_keys(array_filter($validTypes));
190+
$validTypes[] = PublicSuffixList::ALL_DOMAINS;
191+
foreach ($validTypes as $type) {
192+
$publicSuffixListTypes[$type] = $this->buildArray($publicSuffixListTypes[$type], $ruleParts);
196193
}
197194

198195
return $publicSuffixListTypes;
@@ -212,7 +209,7 @@ private function convertLineToArray(string $textLine, array $publicSuffixListTyp
212209
* @param array $ruleParts One line (rule) from the Public Suffix List
213210
* exploded on '.', or the remaining portion of that array during recursion
214211
*/
215-
private function buildArray(array &$publicSuffixList, array $ruleParts)
212+
private function buildArray(array $publicSuffixList, array $ruleParts): array
216213
{
217214
$isDomain = true;
218215

@@ -229,12 +226,12 @@ private function buildArray(array &$publicSuffixList, array $ruleParts)
229226
$isDomain = false;
230227
}
231228

232-
if (!array_key_exists($part, $publicSuffixList)) {
233-
$publicSuffixList[$part] = $isDomain ? [] : ['!' => ''];
234-
}
229+
$publicSuffixList[$part] = $publicSuffixList[$part] ?? ($isDomain ? [] : ['!' => '']);
235230

236231
if ($isDomain && !empty($ruleParts)) {
237-
$this->buildArray($publicSuffixList[$part], $ruleParts);
232+
$publicSuffixList[$part] = $this->buildArray($publicSuffixList[$part], $ruleParts);
238233
}
234+
235+
return $publicSuffixList;
239236
}
240237
}

src/UnmatchedDomain.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class UnmatchedDomain implements Domain
3131
* @param string|null $domain
3232
* @param string|null $publicSuffix
3333
*/
34-
public function __construct(string $domain = null, string $publicSuffix = null)
34+
public function __construct(string $domain, string $publicSuffix)
3535
{
3636
$this->domain = $domain;
3737
$this->publicSuffix = $publicSuffix;
@@ -88,27 +88,6 @@ private function hasRegistrableDomain(): bool
8888
return false;
8989
}
9090

91-
if ($this->publicSuffix === null) {
92-
return false;
93-
}
94-
95-
if ($this->publicSuffix === $this->domain) {
96-
return false;
97-
}
98-
99-
return true;
100-
}
101-
102-
/**
103-
* Returns the additional label to generate the registrable domain.
104-
*
105-
* @param string[] $domainLabels
106-
* @param string[] $publicSuffixLabels
107-
*
108-
* @return string[]
109-
*/
110-
private function getAdditionalLabel($domainLabels, $publicSuffixLabels): array
111-
{
112-
return array_slice($domainLabels, count($domainLabels) - count($publicSuffixLabels) - 1, 1);
91+
return $this->publicSuffix !== $this->domain;
11392
}
11493
}

tests/DomainTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function invalidRegistrableDomainProvider()
3434
{
3535
return [
3636
'domain and suffix are the same' => ['co.uk', 'co.uk'],
37-
'publicSuffix is null' => ['example.faketld', null],
3837
'domain has no labels' => ['faketld', 'faketld'],
3938
];
4039
}

tests/PublicSuffixListManagerTest.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use org\bovigo\vfs\vfsStream;
88
use Pdp\Cache\FileCacheAdapter;
99
use Pdp\Http\CurlHttpAdapter;
10+
use Pdp\PublicSuffixList;
1011
use Pdp\PublicSuffixListManager;
1112
use PHPUnit\Framework\TestCase;
1213

@@ -41,12 +42,6 @@ public function tearDown()
4142
$this->root = null;
4243
}
4344

44-
public function testGetProvidedListFromDefaultCacheDir()
45-
{
46-
$publicSuffixList = $this->manager->getList();
47-
$this->assertGreaterThanOrEqual(300, count($publicSuffixList->getRules()));
48-
}
49-
5045
public function testGetDifferentPublicList()
5146
{
5247
$publicSuffixList = $this->manager->getList();
@@ -63,17 +58,17 @@ public function testRefreshList()
6358

6459
public function testGetListRebuildListFromLocalCache()
6560
{
66-
$previous = $this->manager->getList(PublicSuffixListManager::ICANN_DOMAINS);
67-
$this->cachePool->delete(PublicSuffixListManager::ICANN_DOMAINS); //delete local copy of ICAN DOMAINS RULES
68-
$list = $this->manager->getList(PublicSuffixListManager::ICANN_DOMAINS);
69-
$this->assertEquals($previous, $this->manager->getList(PublicSuffixListManager::ICANN_DOMAINS));
61+
$previous = $this->manager->getList(PublicSuffixList::ICANN_DOMAINS);
62+
$this->cachePool->delete(PublicSuffixList::ICANN_DOMAINS); //delete local copy of ICAN DOMAINS RULES
63+
$list = $this->manager->getList(PublicSuffixList::ICANN_DOMAINS);
64+
$this->assertEquals($previous, $this->manager->getList(PublicSuffixList::ICANN_DOMAINS));
7065
}
7166

7267
public function testGetListRebuildListFromRemoveSource()
7368
{
74-
$previous = $this->manager->getList(PublicSuffixListManager::ICANN_DOMAINS);
69+
$previous = $this->manager->getList(PublicSuffixList::ICANN_DOMAINS);
7570
$this->cachePool->clear(); //delete all local cache
76-
$list = $this->manager->getList(PublicSuffixListManager::ICANN_DOMAINS);
77-
$this->assertEquals($previous, $this->manager->getList(PublicSuffixListManager::ICANN_DOMAINS));
71+
$list = $this->manager->getList(PublicSuffixList::ICANN_DOMAINS);
72+
$this->assertEquals($previous, $this->manager->getList(PublicSuffixList::ICANN_DOMAINS));
7873
}
7974
}

tests/PublicSuffixListTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
namespace Pdp\Tests;
1414

15+
use OutOfRangeException;
1516
use Pdp\Cache\FileCacheAdapter;
1617
use Pdp\Http\CurlHttpAdapter;
1718
use Pdp\MatchedDomain;
@@ -28,15 +29,41 @@ class PublicSuffixListTest extends TestCase
2829
*/
2930
private $list;
3031

32+
private $manager;
33+
3134
public function setUp()
3235
{
33-
$manager = new PublicSuffixListManager(new FileCacheAdapter(), new CurlHttpAdapter());
34-
$this->list = $manager->getList();
36+
$this->manager = new PublicSuffixListManager(new FileCacheAdapter(), new CurlHttpAdapter());
37+
$this->list = $this->manager->getList();
38+
}
39+
40+
public function testConstructorThrowsExceptionOnUnsupportedType()
41+
{
42+
$this->expectException(OutOfRangeException::class);
43+
new PublicSuffixList('foo', []);
44+
}
45+
46+
public function testFullDomainList()
47+
{
48+
$this->assertTrue($this->list->isAll());
49+
$this->assertFalse($this->list->isICANN());
50+
$this->assertFalse($this->list->isPrivate());
51+
}
52+
53+
public function testICANNDomainList()
54+
{
55+
$list = $this->manager->getList(PublicSuffixList::ICANN_DOMAINS);
56+
$this->assertFalse($list->isAll());
57+
$this->assertTrue($list->isICANN());
58+
$this->assertFalse($list->isPrivate());
3559
}
3660

37-
public function testGetRules()
61+
public function testPrivateDomainList()
3862
{
39-
$this->assertInternalType('array', $this->list->getRules());
63+
$list = $this->manager->getList(PublicSuffixList::PRIVATE_DOMAINS);
64+
$this->assertFalse($list->isAll());
65+
$this->assertFalse($list->isICANN());
66+
$this->assertTrue($list->isPrivate());
4067
}
4168

4269
public function testNullWillReturnNullDomain()

0 commit comments

Comments
 (0)