@@ -81,11 +81,25 @@ final class Rules
81
81
const ICANN_DOMAINS = 'ICANN_DOMAINS';
82
82
const PRIVATE_DOMAINS = 'PRIVATE_DOMAINS';
83
83
84
+ public static function createFromPath(string $path, $context = null): self
85
+ public static function createFromString(string $content): self
84
86
public function __construct(array $rules)
85
87
public function resolve(string $domain = null, string $section = self::ALL_DOMAINS): Domain
86
88
}
87
89
~~~
88
90
91
+ Starting with version 5.1.0, the following named constructors arre added to ease ` Rules ` instantiation.
92
+
93
+ - ` Rules::createFromString ` expects a string content which follows [ the PSL format] ( https://publicsuffix.org/list/#list-format ) ;
94
+
95
+ - ` Rules::createFromPath ` expects a valid path to a readable PSL. You can optionnally submit a context resource as defined in PHP's ` fopen ` function;
96
+
97
+ Both named constructors:
98
+
99
+ - uses internally a ` Pdp\Converter ` object to convert the raw content into a suitable array to instantiate a valid ` Pdp\Rules ` ;
100
+ - do not have any cache functionnality;
101
+
102
+
89
103
Domain name resolution is done using the ` Pdp\Rules::resolve ` method which expects at most two parameters:
90
104
91
105
- ` $domain ` a domain name as a string
@@ -135,12 +149,9 @@ If the domain name or some of its part are seriously malformed or unrecognized,
135
149
~~~ php
136
150
<?php
137
151
138
- use Pdp\Converter;
139
152
use Pdp\Rules;
140
153
141
- $content = file_get_contents('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
142
- $arr_rules = (new Converter())->convert($content);
143
- $rules = new Rules($arr_rules);
154
+ $rules = Rules::createFromPath('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
144
155
145
156
$domain = $rules->resolve('www.ulb.ac.be'); //using Rules::ALL_DOMAINS
146
157
$domain->getDomain(); //returns 'www.ulb.ac.be'
0 commit comments