Skip to content

Commit 6e326c5

Browse files
authored
Merge pull request #231 from jeremykendall/develop
merging 5.4.0
2 parents 6133fe0 + e3bafec commit 6e326c5

30 files changed

+3221
-320
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ docs
66
*~
77
build
88
.idea
9+
.php_cs.cache

.php_cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ return PhpCsFixer\Config::create()
3434
'no_leading_import_slash' => true,
3535
'no_trailing_comma_in_singleline_array' => true,
3636
'no_unused_imports' => true,
37-
'ordered_imports' => ['importsOrder' => null, 'sortAlgorithm' => 'alpha'],
37+
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
3838
'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
3939
'phpdoc_align' => true,
4040
'phpdoc_no_empty_return' => true,
4141
'phpdoc_order' => true,
4242
'phpdoc_scalar' => true,
4343
'phpdoc_to_comment' => true,
4444
'phpdoc_summary' => true,
45-
'psr0' => true,
4645
'psr4' => true,
4746
'return_type_declaration' => ['space_before' => 'none'],
4847
'single_blank_line_before_namespace' => true,

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ matrix:
1010
env: VALIDATE_CODING_STYLE=false RUN_PHPSTAN=false IGNORE_PLATFORMS=false
1111
- php: 7.2
1212
env: VALIDATE_CODING_STYLE=true RUN_PHPSTAN=true IGNORE_PLATFORMS=false
13+
- php: 7.3
14+
env: VALIDATE_CODING_STYLE=false RUN_PHPSTAN=false IGNORE_PLATFORMS=true
1315
- php: nightly
1416
env: VALIDATE_CODING_STYLE=false RUN_PHPSTAN=false IGNORE_PLATFORMS=true
1517
allow_failures:

CHANGELOG.md

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

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

5+
# 5.4.0 - 2018-11-22
6+
7+
### Added
8+
9+
- `Pdp\TopLevelDomains` to allow resolving domain againts IANA Root zone database
10+
- `Pdp\TLDConverter` converts the IANA Root Zones database into an associative array
11+
- `Pdp\Manager::getTLDs` a service to return a cache version of the IANA Root zone database
12+
- `Pdp\Manager::refreshTLDs` a service to refresh the cache version of the IANA Root zone database
13+
- added a new `$ttl` parameter to improve PSR-16 supports to
14+
- `Pdp\Manager::__construct`
15+
- `Pdp\Manager::getRules`
16+
- `Pdp\Manager::refreshRules`
17+
- `Pdp\Exception\CouldNotLoadTLDs` exception
18+
19+
### Fixed
20+
21+
- `Pdp\IDNAConverterTrait::setLabels` improve IDN domain handling
22+
- `Pdp\IDNAConverterTrait` throws a `UnexpectedValueException` if the Intl extension is misconfigured see [#230](https://github.com/jeremykendall/php-domain-parser/issues/230)
23+
24+
### Deprecated
25+
26+
- None
27+
28+
### Removed
29+
30+
- None
31+
532
## 5.3.0 - 2018-05-22
633

734
### Added

README.md

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
**PHP Domain Parser** is a [Public Suffix List](http://publicsuffix.org/) based
44
domain parser implemented in PHP.
55

6-
[![Build Status](https://img.shields.io/travis/jeremykendall/php-domain-parser/master.svg?style=flat-square)](https://travis-ci.org/jeremykendall/php-domain-parser)
7-
[![Total Downloads](https://img.shields.io/packagist/dt/jeremykendall/php-domain-parser.svg?style=flat-square)](https://packagist.org/packages/jeremykendall/php-domain-parser)
8-
[![Latest Stable Version](https://img.shields.io/github/release/jeremykendall/php-domain-parser.svg?style=flat-square)](https://github.com/jeremykendall/php-domain-parser/releases)
9-
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE)
6+
[![Build Status][ico-travis]][link-travis]
7+
[![Total Downloads][ico-packagist]][link-packagist]
8+
[![Latest Stable Version][ico-release]][link-release]
9+
[![Software License][ico-license]][link-license]
1010

1111

1212
Motivation
@@ -20,7 +20,7 @@ Consider the domain www.pref.okinawa.jp. In this domain, the
2020
*public suffix* portion is **okinawa.jp**, the *registrable domain* is
2121
**pref.okinawa.jp**, and the *subdomain* is **www**. You can't regex that.
2222

23-
PHP Domain Parser is built around accurate Public Suffix List based parsing. For URL parsing, building or manipulation please refer to [libraries](https://packagist.org/packages/sabre/uri?q=uri%20rfc3986&p=0) focused on those area of development.
23+
PHP Domain Parser is built around accurate Public Suffix List based parsing. For URL parsing, building or manipulation please refer to [libraries][link-parse-library] focused on those area of development.
2424

2525
System Requirements
2626
-------
@@ -66,7 +66,6 @@ $domain->isKnown(); // returns true
6666
$domain->isICANN(); // returns true
6767
$domain->isPrivate(); // returns false
6868

69-
7069
$publicSuffix = $rules->getPublicSuffix('mydomain.github.io', Rules::PRIVATE_DOMAINS); //$publicSuffix is a Pdp\PublicSuffix object
7170
echo $publicSuffix->getContent(); // 'github.io'
7271
$publicSuffix->isKnown(); // returns true
@@ -78,6 +77,14 @@ echo $altSuffix->getContent(); // 'io'
7877
$altSuffix->isKnown(); // returns true
7978
$altSuffix->isICANN(); // returns true
8079
$altSuffix->isPrivate(); // returns false
80+
81+
$tldList = $manager->getTLDs(); //$tldList is a Pdp\TopLevelDomains object
82+
$domain = $tldList->resolve('www.ulb.ac.be'); //$domain is a Pdp\Domain object
83+
$tldList->contains('be'); //returns true
84+
$tldList->contains('localhost'); //return false
85+
foreach($tldList as $tld) {
86+
//$tld is a Pdp\PublisSuffix object
87+
}
8188
~~~
8289

8390
Using the above code you have parse, validate and resolve a domain name and its public suffix status against the Public Suffix list.
@@ -286,7 +293,50 @@ echo json_encode($domain, JSON_PRETTY_PRINT);
286293

287294
**Using the PSL to determine what is a valid domain name and what isn't is dangerous, particularly in these days where new gTLDs are arriving at a rapid pace. The DNS is the proper source for this information. If you must use this library for this purpose, please consider integrating a PSL update mechanism into your software.**
288295

289-
### Managing the Public Suffix List
296+
### Top Level Domains resolutions
297+
298+
**since version 5.4**
299+
300+
~~~php
301+
<?php
302+
303+
namespace Pdp;
304+
305+
final class TopLevelDomains implements Countable, IteratorAggregate
306+
{
307+
public static function createFromPath(string $path, $context = null): Rules
308+
public static function createFromString(string $content): Rules
309+
public function __construct(array $records, string $version, DateTimeInterface $modifiedDate)
310+
public function resolve($domain): Domain
311+
public function contains($domain): bool
312+
public function isEmpty(): bool
313+
}
314+
~~~
315+
316+
The `Pdp\TopLevelDomains` object is responsible for top level domain resolution for a given domain. The resolution is done using a resource which should follow [IANA resource file](https://data.iana.org/TLD/tlds-alpha-by-domain.txt). The class is a collection which contains the list of all top levels domain as registered on the IANA servers.
317+
318+
**THIS EXAMPLE ILLUSTRATES HOW THE OBJECT WORK BUT SHOULD BE AVOIDED IN PRODUCTON**
319+
320+
~~~php
321+
$pdp_url = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';
322+
$tldCollection = Pdp\TopLevelDomains::createFromPath($pdp_url);
323+
324+
$result = $tldCollection->contains('be'); // resolution is done against the retrieves list
325+
326+
$domain = $tldCollection->resolve('www.Ulb.Ac.BE');
327+
// returns
328+
// {
329+
// "domain": "www.ulb.ac.be",
330+
// "registrableDomain": "ac.be",
331+
// "subDomain": "www.ulb",
332+
// "publicSuffix": "be",
333+
// "isKnown": true,
334+
// "isICANN": true,
335+
// "isPrivate": false
336+
// }
337+
~~~
338+
339+
### Managing the package lists
290340

291341
The library comes bundle with a service which enables resolving domain name without the constant network overhead of continously downloading the PSL. The `Pdp\Manager` class retrieves, converts and caches the PSL as well as creates the corresponding `Pdp\Rules` object on demand. It internally uses a `Pdp\Converter` object to convert the fetched PSL into its `array` representation when required.
292342

@@ -300,9 +350,13 @@ use Psr\SimpleCache\CacheInterface;
300350
final class Manager
301351
{
302352
const PSL_URL = 'https://publicsuffix.org/list/public_suffix_list.dat';
303-
public function __construct(CacheInterface $cache, HttpClient $http)
304-
public function getRules(string $source_url = self::PSL_URL): Rules
305-
public function refreshRules(string $source_url = self::PSL_URL): bool
353+
const RZD_URL = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';
354+
355+
public function __construct(CacheInterface $cache, HttpClient $http, $ttl = null)
356+
public function getRules(string $source_url = self::PSL_URL, $ttl = null): Rules
357+
public function refreshRules(string $source_url = self::PSL_URL, $ttl = null): bool
358+
public function getTLDs(string $source_url = self::RZD_URL, $ttl = null): Rules
359+
public function refreshTLDs(string $source_url = self::RZD_URL, $ttl = null): bool
306360
}
307361
~~~
308362

@@ -311,8 +365,17 @@ final class Manager
311365
To work as intended, the `Pdp\Manager` constructor requires:
312366

313367
- a [PSR-16](http://www.php-fig.org/psr/psr-16/) Cache object to store the rules locally.
314-
315368
- a `Pdp\HttpClient` object to retrieve the PSL.
369+
- a `$ttl` argument if you need to set the default $ttl; **since 5.4**
370+
371+
The `$ttl` argument can be:
372+
373+
- an `int` representing time in second (see PSR-16);
374+
- a `DateInterval` object (see PSR-16);
375+
- a `DateTimeInterface` object representing the date and time when the item should expire;
376+
377+
**the `$ttl` argument is added to improve PSR-16 interoperability**
378+
316379

317380
The `Pdp\HttpClient` is a simple interface which exposes the `HttpClient::getContent` method. This method expects a string URL representation has its sole argument and returns the body from the given URL resource as a string.
318381
If an error occurs while retrieving such body a `HttpClientException` exception is thrown.
@@ -342,15 +405,16 @@ The package comes bundle with:
342405
- a file cache PSR-16 implementation based on the excellent [FileCache](https://github.com/kodus/file-cache) which **caches the local copy for a maximum of 7 days**.
343406
- a HTTP client based on the cURL extension.
344407

345-
#### Refreshing the cached PSL
408+
#### Refreshing the cached PSL and TLD data
346409

347410
~~~php
348411
<?php
349412

350-
public Manager::refreshRules(string $source_url = self::PSL_URL): bool
413+
public Manager::refreshRules(string $source_url = self::PSL_URL, $ttl = null): bool
414+
public Manager::refreshTLD(string $source_url = self::RZD_URL, $ttl = null): bool
351415
~~~
352416

353-
The `Pdp\Manager::refreshRules` method enables refreshing your local copy of the PSL stored with your [PSR-16](http://www.php-fig.org/psr/psr-16/) Cache and retrieved using the Http Client. By default the method will use the `Manager::PSL_URL` as the source URL but you are free to substitute this URL with your own.
417+
The both methods method enables refreshing your local copy of the stored resources with your [PSR-16](http://www.php-fig.org/psr/psr-16/) Cache and retrieved using the Http Client. By default the method will use the resource default source URL but you are free to substitute this URL with your own.
354418
The method returns a boolean value which is `true` on success.
355419

356420
~~~php
@@ -363,32 +427,33 @@ if ($retval) {
363427
}
364428
~~~
365429

366-
#### Returning a `Pdp\Rules` object
430+
#### Returning `Pdp\Rules` and `Pdp\TopLevelDomains` objects
367431

368432
~~~php
369433
<?php
370434

371-
public Manager::getRules(string $source_url = self::PSL_URL): Rules
435+
public Manager::getRules(string $source_url = self::PSL_URL, $ttl = null): Rules
436+
public Manager::getTLDs(string $source_url = self::RZD_URL, $ttl = null): TopLevelDomains
372437
~~~
373438

374-
This method returns a `Rules` object which is instantiated with the PSL rules.
439+
These methods returns a `Pdp\Rules` or `Pdp\TopLevelDomains` objects seeded with their corresponding data fetch from the cache or from the external resources depending on the submitted `$ttl` argument.
375440

376-
The method takes an optional `$source_url` argument which specifies the PSL source URL. If no local cache exists for the submitted source URL, the method will:
441+
These methods take an optional `$source_url` argument which specifies the PSL source URL. If no local cache exists for the submitted source URL, the method will:
377442

378-
1. call `Manager::refreshRules` with the given URL to update its local cache
379-
2. instantiate the `Rules` object with the newly cached data.
443+
1. call `Manager::refreshRules` with the given URL and `$ttl` argument to update its local cache
444+
2. instantiate the `Rules` or the `TopLevelDomains` objects with the newly cached data.
380445

381-
On error, the method throws an `Pdp\Exception`.
446+
On error, theses methods will throw an `Pdp\Exception`.
382447

383448
**THIS IS THE RECOMMENDED WAY OF USING THE LIBRARY**
384449

385450
~~~php
386451
<?php
387452

388453
$manager = new Pdp\Manager(new Pdp\Cache(), new Pdp\CurlHttpClient());
389-
$rules = $manager->getRules('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
390-
$domain = $rules->resolve('www.ulb.ac.be');
391-
echo $domain->getPublicSuffix(); // print 'ac.be'
454+
$tldCollection = $manager->getTLDs(self::RZD_URL);
455+
$domain = $tldCollection->resolve('www.ulb.ac.be');
456+
echo $domain->getPublicSuffix(); // print 'be'
392457
~~~
393458

394459
### Automatic Updates
@@ -555,3 +620,15 @@ Portions of the `Pdp\Converter` and `Pdp\Rules` are derivative works of the PHP
555620
[registered-domain-libs](https://github.com/usrflo/registered-domain-libs).
556621
Those parts of this codebase are heavily commented, and I've included a copy of
557622
the Apache Software Foundation License 2.0 in this project.
623+
624+
[ico-travis]: https://img.shields.io/travis/jeremykendall/php-domain-parser/master.svg?style=flat-square
625+
[ico-packagist]: https://img.shields.io/packagist/dt/jeremykendall/php-domain-parser.svg?style=flat-square
626+
[ico-release]: https://img.shields.io/github/release/jeremykendall/php-domain-parser.svg?style=flat-square
627+
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
628+
629+
[link-travis]: https://travis-ci.org/jeremykendall/php-domain-parser
630+
[link-packagist]: https://packagist.org/packages/jeremykendall/php-domain-parser
631+
[link-release]: https://github.com/jeremykendall/php-domain-parser/releases
632+
[link-license]: https://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE
633+
634+
[link-parse-library]: https://packagist.org/explore/?query=rfc3986

data/pdp-PSL_FULL_5a3cc7f81795bb2e48e848af42d287b4.cache

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

0 commit comments

Comments
 (0)