Skip to content

Commit 99d71f7

Browse files
committed
Improve package
- Improve PublicSuffix validation - Improve Domain modification methods validation
1 parent 192f793 commit 99d71f7

22 files changed

+261
-126
lines changed

.php_cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<?php
22

3+
$header = <<<EOF
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+
EOF;
13+
314
$finder = PhpCsFixer\Finder::create()
415
->in(__DIR__.'/src')
516
->in(__DIR__.'/tests')
@@ -10,6 +21,12 @@ return PhpCsFixer\Config::create()
1021
'@PSR2' => true,
1122
'array_syntax' => ['syntax' => 'short'],
1223
'concat_space' => ['spacing' => 'none'],
24+
'header_comment' => [
25+
'commentType' => 'PHPDoc',
26+
'header' => $header,
27+
'location' => 'after_open',
28+
'separate' => 'both',
29+
],
1330
'new_with_braces' => true,
1431
'no_blank_lines_after_phpdoc' => true,
1532
'no_empty_phpdoc' => true,

src/Cache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;

src/CacheException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;

src/Converter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;

src/CurlHttpClient.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;
1317

14-
/**
15-
* Simple cURL Http client.
16-
*
17-
* Lifted pretty much completely from William Durand's excellent Geocoder
18-
* project
19-
*
20-
* @see https://github.com/willdurand/Geocoder Geocoder on GitHub
21-
*
22-
* @author William Durand <[email protected]>
23-
* @author Jeremy Kendall <[email protected]>
24-
* @author Ignace Nyamagana Butera <[email protected]>
25-
*/
2618
final class CurlHttpClient implements HttpClient
2719
{
2820
/**
29-
* Additionnal cURL options.
30-
*
3121
* @var array
3222
*/
3323
private $options;

src/Domain.php

Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;
@@ -23,9 +27,6 @@
2327
* valid. The DNS is the proper source for this innormalizeion. If you must use
2428
* it for this purpose, please do not bake static copies of the PSL into your
2529
* software with no update mechanism."
26-
*
27-
* @author Jeremy Kendall <[email protected]>
28-
* @author Ignace Nyamagana Butera <[email protected]>
2930
*/
3031
final class Domain implements DomainInterface, JsonSerializable
3132
{
@@ -122,7 +123,7 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
122123
*
123124
* @return PublicSuffix
124125
*/
125-
private function normalize(PublicSuffix $subject): PublicSuffix
126+
private function normalize($subject)
126127
{
127128
if (null === $this->domain || null === $subject->getContent()) {
128129
return $subject;
@@ -370,14 +371,7 @@ public function toAscii()
370371
return $this;
371372
}
372373

373-
$clone = clone $this;
374-
$clone->domain = $domain;
375-
$clone->labels = array_reverse(explode('.', $clone->domain));
376-
$clone->publicSuffix = $this->publicSuffix->toAscii();
377-
$clone->registrableDomain = $clone->setRegistrableDomain();
378-
$clone->subDomain = $clone->setSubDomain();
379-
380-
return $clone;
374+
return new self($domain, $this->publicSuffix);
381375
}
382376

383377
/**
@@ -389,14 +383,7 @@ public function toUnicode()
389383
return $this;
390384
}
391385

392-
$clone = clone $this;
393-
$clone->domain = $this->idnToUnicode($this->domain);
394-
$clone->labels = array_reverse(explode('.', $clone->domain));
395-
$clone->publicSuffix = $this->publicSuffix->toUnicode();
396-
$clone->registrableDomain = $clone->setRegistrableDomain();
397-
$clone->subDomain = $clone->setSubDomain();
398-
399-
return $clone;
386+
return new self($this->idnToUnicode($this->domain), $this->publicSuffix);
400387
}
401388

402389
/**
@@ -425,12 +412,7 @@ public function resolve($publicSuffix): self
425412
return $this;
426413
}
427414

428-
$clone = clone $this;
429-
$clone->publicSuffix = $clone->setPublicSuffix($publicSuffix);
430-
$clone->registrableDomain = $clone->setRegistrableDomain();
431-
$clone->subDomain = $clone->setSubDomain();
432-
433-
return $clone;
415+
return new self($this->domain, $publicSuffix);
434416
}
435417

436418
/**
@@ -460,12 +442,9 @@ public function withSubDomain($subDomain): self
460442
return $this;
461443
}
462444

463-
$clone = clone $this;
464-
$clone->labels = array_merge(array_slice($this->labels, 0, count($this->publicSuffix) + 1), iterator_to_array($subDomain));
465-
$clone->domain = implode('.', array_reverse($clone->labels));
466-
$clone->subDomain = $subDomain->getContent();
445+
$labels = array_merge(array_slice($this->labels, 0, count($this->publicSuffix) + 1), iterator_to_array($subDomain));
467446

468-
return $clone;
447+
return new self(implode('.', array_reverse(array_values($labels))), $this->publicSuffix);
469448
}
470449

471450
/**
@@ -495,13 +474,9 @@ public function withPublicSuffix($publicSuffix): self
495474
return $this;
496475
}
497476

498-
$clone = clone $this;
499-
$clone->labels = array_merge(iterator_to_array($publicSuffix), array_slice($this->labels, count($this->publicSuffix)));
500-
$clone->domain = implode('.', array_reverse($clone->labels));
501-
$clone->publicSuffix = $publicSuffix;
502-
$clone->registrableDomain = $this->labels[count($this->publicSuffix)].'.'.$publicSuffix->getContent();
477+
$labels = array_merge(iterator_to_array($publicSuffix), array_slice($this->labels, count($this->publicSuffix)));
503478

504-
return $clone;
479+
return new self(implode('.', array_reverse(array_values($labels))), $publicSuffix);
505480
}
506481

507482
/**
@@ -523,15 +498,14 @@ public function withPublicSuffix($publicSuffix): self
523498
*/
524499
public function withLabel(int $key, $label): self
525500
{
526-
if (!$label instanceof PublicSuffix) {
527-
$label = $this->normalize(new PublicSuffix($label));
501+
if (!$label instanceof Domain) {
502+
$label = new Domain($label);
528503
}
529504

530505
if (1 != count($label)) {
531506
throw new Exception(sprintf('The label `%s` is invalid', (string) $label));
532507
}
533508

534-
$label = (string) $label;
535509
$nb_labels = count($this->labels);
536510
$offset = filter_var($key, FILTER_VALIDATE_INT, ['options' => ['min_range' => - $nb_labels - 1, 'max_range' => $nb_labels]]);
537511
if (false === $offset) {
@@ -542,29 +516,23 @@ public function withLabel(int $key, $label): self
542516
$offset = $nb_labels + $offset;
543517
}
544518

545-
if ($label === ($this->labels[$offset] ?? null)) {
519+
if (($this->labels[$offset] ?? null) === (string) $label) {
546520
return $this;
547521
}
548522

549-
$labels = $this->labels;
550-
$labels[$offset] = $label;
551-
ksort($labels);
552-
553-
$clone = clone $this;
554-
$clone->labels = array_values($labels);
555-
$clone->domain = implode('.', array_reverse($clone->labels));
556-
if (null !== $this->publicSuffix->getLabel($offset)) {
557-
$clone->publicSuffix = new PublicSuffix();
558-
$clone->registrableDomain = null;
559-
$clone->subDomain = null;
560-
561-
return $clone;
523+
if (null !== $this->domain) {
524+
static $pattern = '/[^\x20-\x7f]/';
525+
$label = !preg_match($pattern, $this->domain) ? $label->toAscii() : $label->toUnicode();
562526
}
563527

564-
$clone->registrableDomain = $clone->setRegistrableDomain();
565-
$clone->subDomain = $clone->setSubDomain();
528+
$labels = $this->labels;
529+
$labels[$offset] = (string) $label;
530+
ksort($labels);
566531

567-
return $clone;
532+
return new self(
533+
implode('.', array_reverse(array_values($labels))),
534+
null === $this->publicSuffix->getLabel($offset) ? $this->publicSuffix : null
535+
);
568536
}
569537

570538
/**
@@ -594,20 +562,12 @@ public function withoutLabel(int $key): self
594562
$offset = $nb_labels + $offset;
595563
}
596564

597-
$clone = clone $this;
598-
unset($clone->labels[$offset]);
599-
$clone->domain = implode('.', array_reverse($clone->labels));
600-
if (null !== $this->publicSuffix->getLabel($offset)) {
601-
$clone->publicSuffix = new PublicSuffix();
602-
$clone->registrableDomain = null;
603-
$clone->subDomain = null;
604-
605-
return $clone;
606-
}
607-
608-
$clone->registrableDomain = $clone->setRegistrableDomain();
609-
$clone->subDomain = $clone->setSubDomain();
565+
$labels = $this->labels;
566+
unset($labels[$offset]);
610567

611-
return $clone;
568+
return new self(
569+
implode('.', array_reverse(array_values($labels))),
570+
null === $this->publicSuffix->getLabel($offset) ? $this->publicSuffix : null
571+
);
612572
}
613573
}

src/DomainInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;
@@ -20,8 +24,6 @@
2024
* @see https://tools.ietf.org/html/rfc1034#section-3.5
2125
* @see https://tools.ietf.org/html/rfc1123#section-2.1
2226
* @see https://tools.ietf.org/html/rfc5890
23-
*
24-
* @author Ignace Nyamagana Butera <[email protected]>
2527
*/
2628
interface DomainInterface extends Countable, IteratorAggregate
2729
{

src/Exception.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* PHP Domain Parser: Public Suffix List based URL parsing.
45
*
56
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
67
*
78
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8-
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
912
*/
13+
1014
declare(strict_types=1);
1115

1216
namespace Pdp;

src/HttpClient.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<?php
2+
23
/**
3-
* League.Uri (http://uri.thephpleague.com).
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
47
*
5-
* @package League\Uri
6-
* @subpackage Pdp
7-
* @author Ignace Nyamagana Butera <[email protected]>
8-
* @license https://github.com/thephpleague/uri-hostname-parser/blob/master/LICENSE (MIT License)
9-
* @version 1.0.4
10-
* @link https://github.com/thephpleague/uri-hostname-parser
8+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
119
*
1210
* For the full copyright and license information, please view the LICENSE
1311
* file that was distributed with this source code.

0 commit comments

Comments
 (0)