Skip to content

Commit fca2a87

Browse files
committed
Adding Rules named contructors
1 parent 525c2d3 commit fca2a87

File tree

4 files changed

+12351
-1
lines changed

4 files changed

+12351
-1
lines changed

CHANGELOG.md

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

33
All Notable changes to `PHP Domain Parser` will be documented in this file
44

5+
## Next - TBD
6+
7+
### Added
8+
9+
- `Pdp\Rules::createFromPath` named constructor to returns a new instance from a path
10+
- `Pdp\Rules::createFromString` named constructor to returns a new instance from a string
11+
12+
### Fixed
13+
14+
- None
15+
16+
### Deprecated
17+
18+
- None
19+
20+
### Removed
21+
22+
- None
23+
524
## 5.0.0 - 2017-12-13
625

726
### Added

src/Rules.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,37 @@ final class Rules
3030
*/
3131
private $rules;
3232

33+
/**
34+
* Returns a new instance from a file path.
35+
*
36+
* @param string $path
37+
*
38+
* @return self
39+
*/
40+
public static function createFromPath(string $path): self
41+
{
42+
if (!($resource = @fopen($path, 'r'))) {
43+
throw new Exception(sprintf('`%s`: failed to open stream: No such file or directory', $path));
44+
}
45+
46+
$content = stream_get_contents($resource);
47+
fclose($resource);
48+
49+
return self::createFromString($content);
50+
}
51+
52+
/**
53+
* Returns a new instance from a string.
54+
*
55+
* @param string $content
56+
*
57+
* @return self
58+
*/
59+
public static function createFromString(string $content): self
60+
{
61+
return new self((new Converter())->convert($content));
62+
}
63+
3364
/**
3465
* new instance.
3566
*
@@ -51,7 +82,7 @@ public function __construct(array $rules)
5182
public function resolve(string $domain = null, string $section = self::ALL_DOMAINS): Domain
5283
{
5384
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));
85+
throw new Exception(sprintf('%s is an unknown Public Suffix List section', $section));
5586
}
5687

5788
if (!$this->isMatchable($domain)) {

tests/RulesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ public function setUp()
2525
$this->rules = $manager->getRules();
2626
}
2727

28+
public function testCreateFromPath()
29+
{
30+
$rules = Rules::createFromPath(__DIR__.'/data/public_suffix_list.dat');
31+
$this->assertInstanceOf(Rules::class, $rules);
32+
}
33+
34+
public function testCreateFromPathThrowsException()
35+
{
36+
$this->expectException(Exception::class);
37+
Rules::createFromPath('/foo/bar.dat');
38+
}
39+
2840
public function testNullWillReturnNullDomain()
2941
{
3042
$domain = $this->rules->resolve('COM');

0 commit comments

Comments
 (0)