Skip to content

Commit d5ff1a4

Browse files
committed
improve documentation
1 parent 751ff1e commit d5ff1a4

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

README.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Documentation
4949

5050
In order to resolve a domain name we must:
5151

52-
- Convert the Public Suffix List into a structure usable in PHP
52+
- Convert the Public Suffix List (PSL) into a structure usable in PHP
5353
- Resolve the domain against the given PSL rules
5454

5555
Conversion is done using the `Pdp\Converter` class.
@@ -65,19 +65,9 @@ final class Converter
6565
}
6666
~~~
6767

68-
The `Pdp\Converter::convert` method expect the raw content of a public suffix list and returns its `array` representation.
68+
The `Pdp\Converter::convert` method expects the raw content of a PSL and returns its `array` representation.
6969

70-
~~~php
71-
<?php
72-
73-
use Pdp\Converter;
74-
75-
$content = file_get_contents('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
76-
$converter = new Converter();
77-
$arr_rules = $converter->convert($content);
78-
~~~
79-
80-
Once the PSL has been converted we can feed its data to a `Pdp\Rules` object which is responsible for resolving a given domain name against the PSL rules.
70+
Once the PSL has been converted we can feed its data to a `Pdp\Rules` object which is responsable for resolving a given domain name against the PSL rules.
8171

8272
~~~php
8373
<?php
@@ -95,7 +85,7 @@ final class Rules
9585
}
9686
~~~
9787

98-
The `Rules` constructor expects a `array` representation of the Public Suffix List. This `array` representation is constructed using the `Pdp\Converter` class.
88+
The `Rules` constructor expects the result of the PSL conversion by the `Pdp\Converter` class.
9989

10090
Domain name resolution is done using the `Rules::resolve` method which returns a `Pdp\Domain` object. The method expects
10191

@@ -105,9 +95,7 @@ Domain name resolution is done using the `Rules::resolve` method which returns a
10595
- `Rules::ICANN_DOMAINS`, will validate the domain name againts the PSL ICANN section only.
10696
- `Rules::PRIVATE_DOMAINS`, will validate the domain name againts the PSL PRIVATE section only.
10797

108-
By default, the full PSL is used.
109-
110-
An exception will be thrown if an undefined section is submitted otherwise, a `Pdp\Domain` object is returned.
98+
By default, the `$type` argument equals `Rules::ALL_DOMAINS`. If an unrecognized section is submitted otherwise, a `Pdp\Exception` exception will be thrown.
11199

112100
~~~php
113101
<?php
@@ -126,9 +114,6 @@ final class Domain implements JsonSerializable
126114

127115
The `Domain` getters method always return normalized value according to the domain status against the PSL rules.
128116

129-
**Warning:** `Domain::isKnown`, `Domain::isICANN` status depends on the PSL rules used.
130-
For the same domain, depending on the rules used a domain public suffix status may be known or not, may be ICANN or not.
131-
132117
~~~php
133118
<?php
134119

@@ -162,20 +147,28 @@ echo json_encode($domain, JSON_PRETTY_PRINT);
162147
//let's resolve the same domain against the PRIVATE DOMAIN SECTION
163148

164149
$domain = $rules->resolve('www.ulb.ac.be', Rules::PRIVATE_DOMAINS);
165-
$domain->getDomain(); //returns 'www.ulb.ac.be'
166-
$domain->getPublicSuffix(); //returns 'be'
167-
$domain->getRegistrableDomain(); //returns 'ac.be'
168-
$domain->getSubDomain(); //returns 'www.ulb'
169-
$domain->isKnown(); //returns false
170-
$domain->isICANN(); //returns false
171-
$domain->isPrivate(); //returns false
150+
echo json_encode($domain, JSON_PRETTY_PRINT);
151+
// returns
152+
// {
153+
// "domain": "www.ulb.ac.be",
154+
// "registrableDomain": "ac.be",
155+
// "subDomain": "www.ulb",
156+
// "publicSuffix": "be",
157+
// "isKnown": false,
158+
// "isICANN": false,
159+
// "isPrivate": false
160+
// }
172161
~~~
173162

174-
**Warning:** Some people use the PSL to determine what is a valid domain name and what isn't. This is dangerous, particularly in these days where new gTLDs are arriving at a rapid pace, if your software does not regularly receive PSL updates, it may erroneously think new gTLDs are not known. The DNS is the proper source for this information. If you must use it for this purpose, please do not bake static copies of the PSL into your software with no update mechanism.
163+
**Warning: While the above example works it is highly discouraged to use the library in a productive application this way, without a caching mechanism to load the PSL.**
164+
165+
**Warning: `Domain::isKnown`, `Domain::isICANN` status depends on the PSL rules used. For the same domain, depending on the rules used a domain public suffix status may be known or not, may be ICANN or not.**
166+
167+
**Warning: Some people use the PSL to determine what is a valid domain name and what isn't. This is dangerous, particularly in these days where new gTLDs are arriving at a rapid pace, if your software does not regularly receive PSL updates, it may erroneously think new gTLDs are not known. The DNS is the proper source for this information. If you must use it for this purpose, please do not bake static copies of the PSL into your software with no update mechanism.**
175168

176169
### Public Suffix List Maintenance
177170

178-
**Directly fetching the Public Suffix List without caching the result is not recommend**. For that reason, the package comes bundle with a `Pdp\Manager` class which retrieves, converts and caches the Public Suffix List for you as well as create a `Pdp\Rules` object on demand.
171+
**Directly fetching the PSL without caching the result is not recommend**. For that reason, the library comes bundle with a `Pdp\Manager` class. This class is a service which enables resolving domain name without the constant network overhead of continously downloading the PSL. The class retrieves, converts and caches the PSL as well as creates the corresponding `Pdp\Rules` object on demand. It uses internally a `Pdp\Converter` object to convert the fetched PSL into its `array` representation when required.
179172

180173
~~~php
181174
<?php
@@ -202,8 +195,6 @@ To work as intended, the `Manager` constructor requires:
202195
- a `HttpClient` interface which exposes the `HttpClient::getContent` method which expects a string URL representation has its sole argument and returns the body from the given URL resource as a string.
203196
If an error occurs while retrieving such body a `HttpClientException` is thrown.
204197

205-
**Of note:** the class also uses internally a `Pdp\Converter` object to convert the fetched PSL into its `array` representation when required.
206-
207198
~~~php
208199
<?php
209200

@@ -246,6 +237,8 @@ The method takes an optional `$source_url` argument which specifies the PSL sour
246237

247238
On error, the method throws an `Pdp\Exception`.
248239

240+
**THIS IS THE RECOMMENDED WAY OF USING THE LIBRARY**
241+
249242
~~~php
250243
<?php
251244

@@ -254,13 +247,24 @@ use Pdp\CurlHttpClient;
254247
use Pdp\Manager;
255248

256249
$manager = new Manager(new Cache(), new CurlHttpClient());
257-
$rules = $manager->getRules('https://publicsuffix.org/list/public_suffix_list.dat');
258-
$domain = $rules->resolve('www.bébé.be');
250+
$rules = $manager->getRules('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
251+
$domain = $rules->resolve('www.ulb.ac.be');
252+
echo json_encode($domain, JSON_PRETTY_PRINT);
253+
// returns
254+
// {
255+
// "domain": "www.ulb.ac.be",
256+
// "registrableDomain": "ulb.ac.be",
257+
// "subDomain": "www",
258+
// "publicSuffix": "ac.be",
259+
// "isKnown": true,
260+
// "isICANN": true,
261+
// "isPrivate": false
262+
// }
259263
~~~
260264

261265
#### Refreshing the cached rules
262266

263-
This 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.
267+
The `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.
264268
The method returns a boolean value which is `true` on success.
265269

266270
~~~php
@@ -271,12 +275,17 @@ use Pdp\CurlHttpClient;
271275
use Pdp\Manager;
272276

273277
$manager = new Manager(new Cache(), new CurlHttpClient());
274-
$manager->refreshRules('https://publicsuffix.org/list/public_suffix_list.dat');
278+
$res = $manager->refreshRules('https://publicsuffix.org/list/public_suffix_list.dat');
279+
if ($res === true) {
280+
//the local cache has been updated
281+
} else {
282+
//the local cache has not been updated
283+
}
275284
~~~
276285

277286
## Automatic Updates
278287

279-
It is important to always have an up to date PSL ICANN Section. In order to do so the library comes bundle with an auto-update script located in the `bin` directory.
288+
It is important to always have an up to date PSL. In order to do so the library comes bundle with an auto-update script located in the `bin` directory.
280289

281290
~~~bash
282291
$ php ./bin/update-psl
@@ -293,7 +302,7 @@ If you prefer using your own implementations you should:
293302
1. Copy the `Pdp\Installer` class
294303
2. Adapt its code to reflect your requirements.
295304

296-
In any cases your are required to update regularly your PSL information with your chosen script to keep your data up to date.
305+
In any case, your are required to update regularly your PSL information with your chosen script to keep your data up to date.
297306

298307
For example, below I'm using the `Manager` with
299308

0 commit comments

Comments
 (0)