Skip to content

Commit c79cf7f

Browse files
committed
Simplify Converter
1 parent ac9c3fa commit c79cf7f

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

src/Converter.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ final class Converter
3636
public function convert(string $content): array
3737
{
3838
$rules = [Rules::ICANN_DOMAINS => [], Rules::PRIVATE_DOMAINS => []];
39+
$section = '';
3940
$file = new SplTempFileObject();
4041
$file->fwrite($content);
4142
$file->setFlags(SplTempFileObject::DROP_NEW_LINE | SplTempFileObject::READ_AHEAD | SplTempFileObject::SKIP_EMPTY);
42-
$section = '';
4343
foreach ($file as $line) {
4444
$section = $this->getSection($section, $line);
4545
if ('' !== $section && false === strpos($line, '//')) {
@@ -60,20 +60,13 @@ public function convert(string $content): array
6060
*/
6161
private function getSection(string $section, string $line): string
6262
{
63-
if ('' === $section && 0 === strpos($line, '// ===BEGIN ICANN DOMAINS===')) {
64-
return Rules::ICANN_DOMAINS;
65-
}
66-
67-
if (Rules::ICANN_DOMAINS === $section && 0 === strpos($line, '// ===END ICANN DOMAINS===')) {
68-
return '';
69-
}
70-
71-
if ('' === $section && 0 === strpos($line, '// ===BEGIN PRIVATE DOMAINS===')) {
72-
return Rules::PRIVATE_DOMAINS;
73-
}
74-
75-
if (Rules::PRIVATE_DOMAINS === $section && 0 === strpos($line, '// ===END PRIVATE DOMAINS===')) {
76-
return '';
63+
static $section_list = [
64+
'ICANN' => ['BEGIN' => Rules::ICANN_DOMAINS, 'END' => ''],
65+
'PRIVATE' => ['BEGIN' => Rules::PRIVATE_DOMAINS, 'END' => ''],
66+
];
67+
static $pattern = ',^// ===(?<point>BEGIN|END) (?<type>ICANN|PRIVATE) DOMAINS===,';
68+
if (preg_match($pattern, $line, $matches)) {
69+
return $section_list[$matches['type']][$matches['point']];
7770
}
7871

7972
return $section;
@@ -97,26 +90,21 @@ private function getSection(string $section, string $line): string
9790
*/
9891
private function addRule(array $list, array $rule_parts): array
9992
{
100-
$part = array_pop($rule_parts);
101-
10293
// Adheres to canonicalization rule from the "Formal Algorithm" section
10394
// of https://publicsuffix.org/list/
10495
// "The domain and all rules must be canonicalized in the normal way
10596
// for hostnames - lower-case, Punycode (RFC 3492)."
10697

107-
$part = $this->idnToAscii($part);
98+
$rule = $this->idnToAscii(array_pop($rule_parts));
10899
$isDomain = true;
109-
if (0 === strpos($part, '!')) {
110-
$part = substr($part, 1);
100+
if (0 === strpos($rule, '!')) {
101+
$rule = substr($rule, 1);
111102
$isDomain = false;
112103
}
113104

114-
if (!isset($list[$part])) {
115-
$list[$part] = $isDomain ? [] : ['!' => ''];
116-
}
117-
105+
$list[$rule] = $list[$rule] ?? ($isDomain ? [] : ['!' => '']);
118106
if ($isDomain && !empty($rule_parts)) {
119-
$list[$part] = $this->addRule($list[$part], $rule_parts);
107+
$list[$rule] = $this->addRule($list[$rule], $rule_parts);
120108
}
121109

122110
return $list;

0 commit comments

Comments
 (0)