Skip to content

Commit 030f314

Browse files
committed
improve documentation
1 parent 7fc2857 commit 030f314

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

README.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ final class Rules
8181
const PRIVATE_DOMAINS = 'PRIVATE_DOMAINS';
8282

8383
public function __construct(array $rules)
84-
public function resolve(string $domain = null, string $type = self::ALL_DOMAINS): Domain
84+
public function resolve(string $domain = null, string $section = self::ALL_DOMAINS): Domain
8585
}
8686
~~~
8787

8888
Domain name resolution is done using the `Pdp\Rules::resolve` method which expects at most two parameters:
8989

9090
- `$domain` a domain name as a string
91-
- `$type` a string which specifies which section of the PSL you want to validate the given domain against. The possible values are:
91+
- `$section` a string which specifies which section of the PSL you want to validate the given domain against. The possible values are:
9292
- `Rules::ALL_DOMAINS`, to validate against the full PSL.
9393
- `Rules::ICANN_DOMAINS`, to validate against the PSL ICANN DOMAINS section only.
9494
- `Rules::PRIVATE_DOMAINS`, to validate against the PSL PRIVATE DOMAINS section only.
9595

96-
By default, the `$type` argument is equal to `Rules::ALL_DOMAINS`. If an unsupported section is submitted otherwise, a `Pdp\Exception` exception will be thrown.
96+
By default, the `$section` argument is equal to `Rules::ALL_DOMAINS`. If an unsupported section is submitted a `Pdp\Exception` exception will be thrown.
9797

9898

9999
The `Pdp\Rules::resolve` returns a `Pdp\Domain` object.
@@ -128,7 +128,7 @@ If the domain name or some of its part are seriously malformed or unrecognized,
128128
- `Pdp\Domain::isICANN` returns `true` if the public suffix is found in a selected PSL which includes the ICANN DOMAINS section;
129129
- `Pdp\Domain::isPrivate` returns `true` if the public suffix is found in a selected PSL which includes the PRIVATE DOMAINS section;
130130

131-
**THIS EXAMPLE ILLUSTRATES HOW EACH OBJECT IS USED BUT SHOULD BE AVOID IN A PRODUCTIVE ENVIRONMENT**
131+
**THIS EXAMPLE ILLUSTRATES HOW EACH OBJECT IS USED BUT SHOULD BE AVOID IN A PRODUCTON**
132132

133133
~~~php
134134
<?php
@@ -178,13 +178,13 @@ echo json_encode($domain, JSON_PRETTY_PRINT);
178178

179179
**WARNING:**
180180

181-
**As already stated, you should never user the library this way in a productive application, without at least a caching mechanism to load the PSL.**
181+
**You should never use the library this way in production, without, at least, a caching mechanism to reduce PSL downloads.**
182182

183183
**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.**
184184

185185
### Public Suffix List Maintenance
186186

187-
Since **directly fetching the PSL without caching the result is not recommend**, 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.
187+
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.
188188

189189
~~~php
190190
<?php
@@ -202,14 +202,15 @@ final class Manager
202202
}
203203
~~~
204204

205-
#### Creating a new manager
205+
#### Instantiate `Pdp\Manager`
206206

207-
To work as intended, the `Manager` constructor requires:
207+
To work as intended, the `Pdp\Manager` constructor requires:
208208

209-
- a [PSR-16](http://www.php-fig.org/psr/psr-16/) Cache object to store the retrieved rules using a basic HTTP client.
209+
- a [PSR-16](http://www.php-fig.org/psr/psr-16/) Cache object to store the rules locally.
210210

211-
- 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.
212-
If an error occurs while retrieving such body a `HttpClientException` is thrown.
211+
- a `Pdp\HttpClient` object to retrieve the PSL.
212+
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.
213+
If an error occurs while retrieving such body a `HttpClientException` exception is thrown.
213214

214215
~~~php
215216
<?php
@@ -231,11 +232,32 @@ interface HttpClient
231232
}
232233
~~~
233234

234-
By default and out of the box, the package uses:
235+
The package comes bundle with:
235236

236237
- 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**.
237238
- a HTTP client based on the cURL extension.
238239

240+
#### Refreshing the cached PSL
241+
242+
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.
243+
The method returns a boolean value which is `true` on success.
244+
245+
~~~php
246+
<?php
247+
248+
use Pdp\Cache;
249+
use Pdp\CurlHttpClient;
250+
use Pdp\Manager;
251+
252+
$manager = new Manager(new Cache(), new CurlHttpClient());
253+
$retval = $manager->refreshRules('https://publicsuffix.org/list/public_suffix_list.dat');
254+
if ($retval) {
255+
//the local cache has been updated
256+
} else {
257+
//the local cache has not been updated
258+
}
259+
~~~
260+
239261
#### Returning a `Pdp\Rules` object
240262

241263
~~~php
@@ -278,27 +300,6 @@ echo json_encode($domain, JSON_PRETTY_PRINT);
278300
// }
279301
~~~
280302

281-
#### Refreshing the cached PSL
282-
283-
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.
284-
The method returns a boolean value which is `true` on success.
285-
286-
~~~php
287-
<?php
288-
289-
use Pdp\Cache;
290-
use Pdp\CurlHttpClient;
291-
use Pdp\Manager;
292-
293-
$manager = new Manager(new Cache(), new CurlHttpClient());
294-
$retval = $manager->refreshRules('https://publicsuffix.org/list/public_suffix_list.dat');
295-
if ($retval) {
296-
//the local cache has been updated
297-
} else {
298-
//the local cache has not been updated
299-
}
300-
~~~
301-
302303
### Automatic Updates
303304

304305
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.
@@ -310,8 +311,8 @@ $ php ./bin/update-psl
310311
This script requires:
311312

312313
- The PHP `curl` extension
313-
- The `Pdp\Installer` class which comes bundle with this package
314-
- The use of the Cache and HTTP Client implementations bundle with this package.
314+
- The `Pdp\Installer` class which organizes how to update the cache.
315+
- The `Pdp\Cache` and `Pdp\CurlHttpClient` classes to retrieve and cache the PSL
315316

316317
If you prefer using your own implementations you should:
317318

src/Rules.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ public function __construct(array $rules)
4444
* Returns PSL ICANN public info for a given domain.
4545
*
4646
* @param string|null $domain
47-
* @param string $type
47+
* @param string $section
4848
*
4949
* @return Domain
5050
*/
51-
public function resolve(string $domain = null, string $type = self::ALL_DOMAINS): Domain
51+
public function resolve(string $domain = null, string $section = self::ALL_DOMAINS): Domain
5252
{
53-
if (!in_array($type, [self::PRIVATE_DOMAINS, self::ICANN_DOMAINS, self::ALL_DOMAINS], true)) {
54-
throw new Exception(sprintf('%s is an unknown Domain type', $type));
53+
if (!in_array($section, [self::PRIVATE_DOMAINS, self::ICANN_DOMAINS, self::ALL_DOMAINS], true)) {
54+
throw new Exception(sprintf('%s is an unknown Domain section', $section));
5555
}
5656

5757
if (!$this->isMatchable($domain)) {
5858
return new Domain();
5959
}
6060

61-
$publicSuffix = $this->findPublicSuffix($domain, $type);
61+
$publicSuffix = $this->findPublicSuffix($domain, $section);
6262
if (null === $publicSuffix->getContent()) {
6363
return new Domain($domain, $this->handleNoMatches($domain));
6464
}
@@ -110,16 +110,16 @@ private function normalize(string $domain): string
110110
* Returns the matched public suffix.
111111
*
112112
* @param string $domain
113-
* @param string $type
113+
* @param string $section
114114
*
115115
* @return PublicSuffix
116116
*/
117-
private function findPublicSuffix(string $domain, string $type): PublicSuffix
117+
private function findPublicSuffix(string $domain, string $section): PublicSuffix
118118
{
119119
$normalizedDomain = $this->normalize($domain);
120120
$reverseLabels = array_reverse(explode('.', $normalizedDomain));
121121
$resultIcann = $this->findPublicSuffixFromSection($reverseLabels, self::ICANN_DOMAINS);
122-
if (self::ICANN_DOMAINS === $type) {
122+
if (self::ICANN_DOMAINS === $section) {
123123
return $resultIcann;
124124
}
125125

@@ -128,7 +128,7 @@ private function findPublicSuffix(string $domain, string $type): PublicSuffix
128128
return $resultPrivate;
129129
}
130130

131-
if (self::ALL_DOMAINS === $type) {
131+
if (self::ALL_DOMAINS === $section) {
132132
return $resultIcann;
133133
}
134134

@@ -139,13 +139,13 @@ private function findPublicSuffix(string $domain, string $type): PublicSuffix
139139
* Returns the public suffix matched against a given PSL section.
140140
*
141141
* @param array $labels
142-
* @param string $type
142+
* @param string $section
143143
*
144144
* @return PublicSuffix
145145
*/
146-
private function findPublicSuffixFromSection(array $labels, string $type): PublicSuffix
146+
private function findPublicSuffixFromSection(array $labels, string $section): PublicSuffix
147147
{
148-
$rules = $this->rules[$type] ?? null;
148+
$rules = $this->rules[$section] ?? null;
149149
$matches = [];
150150
foreach ($labels as $label) {
151151
//match exception rule
@@ -173,7 +173,7 @@ private function findPublicSuffixFromSection(array $labels, string $type): Publi
173173
return new PublicSuffix();
174174
}
175175

176-
return new PublicSuffix(implode('.', $foundLabels), $type);
176+
return new PublicSuffix(implode('.', $foundLabels), $section);
177177
}
178178

179179
/**

0 commit comments

Comments
 (0)