Skip to content

Commit fb2f224

Browse files
committed
change TopLevelDomains::lastUpdate to TopLevelDomains::getModifiedDate
1 parent 895fb44 commit fb2f224

11 files changed

+225
-195
lines changed

data/pdp-RZD_FULL_f18a70477d29d525b9220612e2115345.cache

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

src/Converter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Pdp;
1717

18+
use Pdp\Exception\CouldNotLoadRules;
1819
use SplTempFileObject;
1920
use function array_pop;
2021
use function explode;
@@ -102,15 +103,22 @@ private function getSection(string $section, string $line): string
102103
* becomes the array representation of a Public Suffix List section
103104
* @param array $rule_parts One line (rule) from the Public Suffix List
104105
* exploded on '.', or the remaining portion of that array during recursion
106+
*
107+
* @throws Exception if The domain name is invalid
105108
*/
106109
private function addRule(array $list, array $rule_parts): array
107110
{
108111
// Adheres to canonicalization rule from the "Formal Algorithm" section
109112
// of https://publicsuffix.org/list/
110113
// "The domain and all rules must be canonicalized in the normal way
111114
// for hostnames - lower-case, Punycode (RFC 3492)."
115+
try {
116+
$tld = array_pop($rule_parts);
117+
$rule = $this->idnToAscii($tld);
118+
} catch (Exception $e) {
119+
throw new CouldNotLoadRules($e->getMessage(), $e->getCode(), $e);
120+
}
112121

113-
$rule = $this->idnToAscii(array_pop($rule_parts));
114122
$isDomain = true;
115123
if (0 === strpos($rule, '!')) {
116124
$rule = substr($rule, 1);

src/CurlHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function __construct(array $options = [])
4747
CURLOPT_FAILONERROR => true,
4848
CURLOPT_FOLLOWLOCATION => true,
4949
CURLOPT_RETURNTRANSFER => true,
50+
CURLOPT_SSL_VERIFYHOST => 0,
5051
CURLOPT_SSL_VERIFYPEER => false,
51-
CURLOPT_SSL_VERIFYHOST => false,
5252
CURLOPT_HTTPGET => true,
5353
];
5454

src/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ public function getTLDs(string $url = self::RZD_URL, $ttl = null): TopLevelDomai
143143
throw new CouldNotLoadTLDs('The root zone database cache is corrupted: '.json_last_error_msg(), json_last_error());
144144
}
145145

146-
if (!isset($data['records'], $data['version'], $data['update'])) {
146+
if (!isset($data['records'], $data['version'], $data['modifiedDate'])) {
147147
throw new CouldNotLoadTLDs(sprintf('The root zone database cache content is corrupted'));
148148
}
149149

150150
return new TopLevelDomains(
151151
$data['records'],
152152
$data['version'],
153-
DateTimeImmutable::createFromFormat(DATE_ATOM, $data['update'])
153+
DateTimeImmutable::createFromFormat(DATE_ATOM, $data['modifiedDate'])
154154
);
155155
}
156156

src/TLDConverter.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Pdp;
1717

1818
use DateTimeImmutable;
19+
use Pdp\Exception\CouldNotLoadTLDs;
1920
use SplTempFileObject;
2021
use const DATE_ATOM;
2122
use function preg_match;
@@ -40,7 +41,7 @@ final class TLDConverter
4041
/**
4142
* Converts the IANA Root Zone Database into a TopLevelDomains associative array.
4243
*
43-
* @throws Exception if the content is invalid or can not be correctly parsed and converted
44+
* @throws CouldNotLoadTLDs if the content is invalid or can not be correctly parsed and converted
4445
*/
4546
public function convert(string $content): array
4647
{
@@ -61,44 +62,49 @@ public function convert(string $content): array
6162
continue;
6263
}
6364

64-
throw new Exception(sprintf('Invalid line content: %s', $line));
65+
throw new CouldNotLoadTLDs(sprintf('Invalid line content: %s', $line));
6566
}
6667

67-
if (isset($data['version'], $data['update'], $data['records'])) {
68+
if (isset($data['version'], $data['modifiedDate'], $data['records'])) {
6869
return $data;
6970
}
7071

71-
throw new Exception(sprintf('Invalid content: TLD conversion failed'));
72+
throw new CouldNotLoadTLDs(sprintf('Invalid content: TLD conversion failed'));
7273
}
7374

7475
/**
7576
* Extract IANA Root Zone Database header info.
7677
*
77-
* @throws Exception if the Header line is invalid
78+
* @throws CouldNotLoadTLDs if the Header line is invalid
7879
*/
7980
private function extractHeader(string $content): array
8081
{
81-
if (!preg_match('/^\# Version (?<version>\d+), Last Updated (?<update>.*?)$/', $content, $matches)) {
82-
throw new Exception(sprintf('Invalid Version line: %s', $content));
82+
if (!preg_match('/^\# Version (?<version>\d+), Last Updated (?<date>.*?)$/', $content, $matches)) {
83+
throw new CouldNotLoadTLDs(sprintf('Invalid Version line: %s', $content));
8384
}
8485

8586
return [
8687
'version' => $matches['version'],
87-
'update' => DateTimeImmutable::createFromFormat(self::IANA_DATE_FORMAT, $matches['update'])
88+
'modifiedDate' => DateTimeImmutable::createFromFormat(self::IANA_DATE_FORMAT, $matches['date'])
8889
->format(DATE_ATOM),
8990
];
9091
}
9192

9293
/**
9394
* Extract IANA Root Zone.
9495
*
95-
* @throws Exception If the Root Zone is invalid
96+
* @throws CouldNotLoadTLDs If the Root Zone is invalid
9697
*/
9798
private function extractRootZone(string $content): string
9899
{
99-
$tld = (new PublicSuffix($content))->toAscii();
100+
try {
101+
$tld = (new PublicSuffix($content))->toAscii();
102+
} catch (Exception $e) {
103+
throw new CouldNotLoadTLDs(sprintf('Invalid Root zone: %s', $content), 0, $e);
104+
}
105+
100106
if (1 !== $tld->count() || '' === $tld->getContent()) {
101-
throw new Exception(sprintf('Invalid Root zone: %s', $content));
107+
throw new CouldNotLoadTLDs(sprintf('Invalid Root zone: %s', $content));
102108
}
103109

104110
return (string) $tld;

src/TopLevelDomains.php

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class TopLevelDomains implements Countable, IteratorAggregate
3333
/**
3434
* @var DateTimeImmutable
3535
*/
36-
private $update;
36+
private $modifiedDate;
3737

3838
/**
3939
* @var string
@@ -83,7 +83,7 @@ public static function createFromString(string $content): self
8383
return new self(
8484
$data['records'],
8585
$data['version'],
86-
DateTimeImmutable::createFromFormat(DATE_ATOM, $data['update'])
86+
DateTimeImmutable::createFromFormat(DATE_ATOM, $data['modifiedDate'])
8787
);
8888
}
8989

@@ -92,17 +92,17 @@ public static function createFromString(string $content): self
9292
*/
9393
public static function __set_state(array $properties): self
9494
{
95-
return new self($properties['records'], $properties['version'], $properties['update']);
95+
return new self($properties['records'], $properties['version'], $properties['modifiedDate']);
9696
}
9797

9898
/**
9999
* New instance.
100100
*/
101-
public function __construct(array $records, string $version, DateTimeInterface $update)
101+
public function __construct(array $records, string $version, DateTimeInterface $modifiedDate)
102102
{
103103
$this->records = $records;
104104
$this->version = $version;
105-
$this->update = $update instanceof DateTime ? DateTimeImmutable::createFromMutable($update) : $update;
105+
$this->modifiedDate = $modifiedDate instanceof DateTime ? DateTimeImmutable::createFromMutable($modifiedDate) : $modifiedDate;
106106
}
107107

108108
/**
@@ -114,11 +114,11 @@ public function getVersion(): string
114114
}
115115

116116
/**
117-
* Returns the List Last Update Info.
117+
* Returns the List Last Modified Date.
118118
*/
119-
public function getLastUpdate(): DateTimeImmutable
119+
public function getModifiedDate(): DateTimeImmutable
120120
{
121-
return $this->update;
121+
return $this->modifiedDate;
122122
}
123123

124124
/**
@@ -155,7 +155,7 @@ public function toArray(): array
155155
return [
156156
'version' => $this->version,
157157
'records' => $this->records,
158-
'update' => $this->update->format(DATE_ATOM),
158+
'modifiedDate' => $this->modifiedDate->format(DATE_ATOM),
159159
];
160160
}
161161

@@ -166,20 +166,22 @@ public function contains($tld): bool
166166
{
167167
try {
168168
$tld = $tld instanceof Domain ? $tld : new Domain($tld);
169-
if (1 !== count($tld)) {
170-
return false;
171-
}
172-
$label = $tld->toAscii()->getLabel(0);
173-
foreach ($this as $tld) {
174-
if ($tld->getContent() === $label) {
175-
return true;
176-
}
177-
}
178-
179-
return false;
180169
} catch (Exception $e) {
181170
return false;
182171
}
172+
173+
if (1 !== count($tld)) {
174+
return false;
175+
}
176+
177+
$label = $tld->toAscii()->getLabel(0);
178+
foreach ($this as $tld) {
179+
if ($tld->getContent() === $label) {
180+
return true;
181+
}
182+
}
183+
184+
return false;
183185
}
184186

185187
/**
@@ -189,20 +191,21 @@ public function resolve($domain): Domain
189191
{
190192
try {
191193
$domain = $domain instanceof Domain ? $domain : new Domain($domain);
192-
if (!$domain->isResolvable()) {
193-
return $domain;
194-
}
195-
196-
$label = $domain->toAscii()->getLabel(0);
197-
foreach ($this as $tld) {
198-
if ($tld->getContent() === $label) {
199-
return $domain->resolve($tld);
200-
}
201-
}
202-
203-
return $domain->withPublicSuffix(new PublicSuffix());
204194
} catch (Exception $e) {
205195
return new Domain();
206196
}
197+
198+
if (!$domain->isResolvable()) {
199+
return $domain;
200+
}
201+
202+
$label = $domain->toAscii()->getLabel(0);
203+
foreach ($this as $tld) {
204+
if ($tld->getContent() === $label) {
205+
return $domain->resolve($tld);
206+
}
207+
}
208+
209+
return $domain->resolve(null);
207210
}
208211
}

tests/ConverterTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* PHP Domain Parser: Public Suffix List based URL parsing.
5+
*
6+
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7+
*
8+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Pdp\Tests;
17+
18+
use Pdp\Converter;
19+
use Pdp\Exception\CouldNotLoadRules;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @coversDefaultClass Pdp\Converter
24+
*/
25+
class ConverterTest extends TestCase
26+
{
27+
public function testConverter()
28+
{
29+
$string = file_get_contents(__DIR__.'/data/public_suffix_list.dat');
30+
$retval = (new Converter())->convert($string);
31+
self::assertNotEmpty($retval[Converter::ICANN_DOMAINS]);
32+
self::assertNotEmpty($retval[Converter::PRIVATE_DOMAINS]);
33+
}
34+
35+
public function testConvertThrowsExceptionWithInvalidContent()
36+
{
37+
self::expectException(CouldNotLoadRules::class);
38+
$content = file_get_contents(__DIR__.'/data/invalid_suffix_list_content.dat');
39+
(new Converter())->convert($content);
40+
}
41+
42+
public function testConvertWithEmptyString()
43+
{
44+
$retval = (new Converter())->convert('');
45+
self::assertEquals([Converter::ICANN_DOMAINS => [], Converter::PRIVATE_DOMAINS => []], $retval);
46+
}
47+
48+
public function testConvertWithInvalidString()
49+
{
50+
$retval = (new Converter())->convert('foobar');
51+
self::assertEquals([Converter::ICANN_DOMAINS => [], Converter::PRIVATE_DOMAINS => []], $retval);
52+
}
53+
}

0 commit comments

Comments
 (0)