Skip to content

Commit b8dc76c

Browse files
committed
Adding methods to improve Rules public API
1 parent 119ed8f commit b8dc76c

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

src/Rules.php

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,74 @@ public function resolve($domain, string $section = self::ALL_DOMAINS): Domain
211211
{
212212
$section = $this->validateSection($section);
213213
try {
214-
$domain = $domain instanceof Domain
215-
? $domain
216-
: new Domain($domain, null, $this->asciiIDNAOption, $this->unicodeIDNAOption);
217-
if (!$domain->isResolvable()) {
218-
return $domain;
214+
if ('' === $section) {
215+
return $this->cookieResolve($domain);
216+
} elseif (self::ICANN_DOMAINS === $section) {
217+
return $this->icannResolve($domain);
219218
}
220219

221-
return $domain->resolve($this->findPublicSuffix($domain, $section));
222-
} catch (Exception $e) {
220+
return $this->privateResolve($domain);
221+
} catch (CouldNotResolvePublicSuffix $exception) {
222+
return new Domain($domain, null, $this->asciiIDNAOption, $this->unicodeIDNAOption);
223+
} catch (Exception $exception) {
223224
return new Domain(null, null, $this->asciiIDNAOption, $this->unicodeIDNAOption);
224225
}
225226
}
226227

228+
/**
229+
* Returns PSL info for a given domain against the PSL rules for cookie domain detection.
230+
*
231+
* @param mixed $domain the domain value
232+
*/
233+
public function cookieResolve($domain): Domain
234+
{
235+
$domain = $this->validateDomain($domain);
236+
237+
return $domain->resolve($this->findPublicSuffix($domain, ''));
238+
}
239+
240+
/**
241+
* Returns PSL info for a given domain against the PSL rules for ICANN domain detection.
242+
*
243+
* @param mixed $domain
244+
*/
245+
public function icannResolve($domain): Domain
246+
{
247+
$domain = $this->validateDomain($domain);
248+
249+
return $domain->resolve($this->findPublicSuffix($domain, self::ICANN_DOMAINS));
250+
}
251+
252+
/**
253+
* Returns PSL info for a given domain against the PSL rules for private domain detection.
254+
*
255+
* @param mixed $domain
256+
*/
257+
public function privateResolve($domain): Domain
258+
{
259+
$domain = $this->validateDomain($domain);
260+
261+
return $domain->resolve($this->findPublicSuffix($domain, self::PRIVATE_DOMAINS));
262+
}
263+
264+
/**
265+
* Returns PSL info for a given domain.
266+
*
267+
* @param mixed $domain
268+
*/
269+
private function validateDomain($domain): Domain
270+
{
271+
if (!($domain instanceof Domain)) {
272+
$domain = new Domain($domain, null, $this->asciiIDNAOption, $this->unicodeIDNAOption);
273+
}
274+
275+
if (!$domain->isResolvable()) {
276+
throw new CouldNotResolvePublicSuffix(sprintf('The domain `%s` can not contain a public suffix', $domain->getContent()));
277+
}
278+
279+
return $domain;
280+
}
281+
227282
/**
228283
* Assert the section status.
229284
*

tests/RulesTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public function testwithIDNAOptions(): void
114114

115115
/**
116116
* @covers ::resolve
117+
* @covers ::cookieResolve
118+
* @covers ::icannResolve
119+
* @covers ::privateResolve
120+
* @covers ::validateDomain
117121
* @covers ::validateSection
118122
* @covers \Pdp\Domain::isResolvable
119123
* @covers \Pdp\PublicSuffix::setSection
@@ -191,6 +195,8 @@ public function testIsSuffixValidTrue(): void
191195

192196
/**
193197
* @covers ::resolve
198+
* @covers ::cookieResolve
199+
* @covers ::validateDomain
194200
* @covers ::validateSection
195201
* @covers \Pdp\Domain::isResolvable
196202
* @covers ::findPublicSuffix
@@ -215,6 +221,8 @@ public function testIsSuffixValidFalseWithPunycoded(): void
215221

216222
/**
217223
* @covers ::resolve
224+
* @covers ::icannResolve
225+
* @covers ::validateDomain
218226
* @covers ::validateSection
219227
* @covers \Pdp\Domain::isResolvable
220228
* @covers ::findPublicSuffix
@@ -239,6 +247,8 @@ public function testSubDomainIsNull(): void
239247

240248
/**
241249
* @covers ::resolve
250+
* @covers ::cookieResolve
251+
* @covers ::validateDomain
242252
* @covers ::validateSection
243253
* @covers \Pdp\IDNAConverterTrait::parse
244254
*/
@@ -281,6 +291,8 @@ public function testWithAbsoluteHostInvalid(): void
281291

282292
/**
283293
* @covers ::resolve
294+
* @covers ::privateResolve
295+
* @covers ::validateDomain
284296
* @covers ::validateSection
285297
* @covers ::findPublicSuffix
286298
* @covers ::findPublicSuffixFromSection
@@ -299,6 +311,8 @@ public function testWithPrivateDomainInvalid(): void
299311

300312
/**
301313
* @covers ::resolve
314+
* @covers ::privateResolve
315+
* @covers ::validateDomain
302316
* @covers ::validateSection
303317
* @covers ::findPublicSuffix
304318
* @covers ::findPublicSuffixFromSection
@@ -367,6 +381,8 @@ public function testWithDomainInterfaceObject(): void
367381

368382
/**
369383
* @covers ::resolve
384+
* @covers ::icannResolve
385+
* @covers ::validateDomain
370386
* @covers \Pdp\Domain::setRegistrableDomain
371387
* @covers \Pdp\Domain::getRegistrableDomain
372388
* @dataProvider parseDataProvider
@@ -383,6 +399,8 @@ public function testGetRegistrableDomain($publicSuffix, $registrableDomain, $dom
383399

384400
/**
385401
* @covers ::resolve
402+
* @covers ::icannResolve
403+
* @covers ::validateDomain
386404
* @covers \Pdp\IDNAConverterTrait::parse
387405
* @covers \Pdp\Domain::setPublicSuffix
388406
* @covers \Pdp\Domain::getPublicSuffix

0 commit comments

Comments
 (0)