@@ -36,10 +36,10 @@ final class Converter
36
36
public function convert (string $ content ): array
37
37
{
38
38
$ rules = [Rules::ICANN_DOMAINS => [], Rules::PRIVATE_DOMAINS => []];
39
+ $ section = '' ;
39
40
$ file = new SplTempFileObject ();
40
41
$ file ->fwrite ($ content );
41
42
$ file ->setFlags (SplTempFileObject::DROP_NEW_LINE | SplTempFileObject::READ_AHEAD | SplTempFileObject::SKIP_EMPTY );
42
- $ section = '' ;
43
43
foreach ($ file as $ line ) {
44
44
$ section = $ this ->getSection ($ section , $ line );
45
45
if ('' !== $ section && false === strpos ($ line , '// ' )) {
@@ -60,20 +60,13 @@ public function convert(string $content): array
60
60
*/
61
61
private function getSection (string $ section , string $ line ): string
62
62
{
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 ' ]];
77
70
}
78
71
79
72
return $ section ;
@@ -97,26 +90,21 @@ private function getSection(string $section, string $line): string
97
90
*/
98
91
private function addRule (array $ list , array $ rule_parts ): array
99
92
{
100
- $ part = array_pop ($ rule_parts );
101
-
102
93
// Adheres to canonicalization rule from the "Formal Algorithm" section
103
94
// of https://publicsuffix.org/list/
104
95
// "The domain and all rules must be canonicalized in the normal way
105
96
// for hostnames - lower-case, Punycode (RFC 3492)."
106
97
107
- $ part = $ this ->idnToAscii ($ part );
98
+ $ rule = $ this ->idnToAscii (array_pop ( $ rule_parts ) );
108
99
$ isDomain = true ;
109
- if (0 === strpos ($ part , '! ' )) {
110
- $ part = substr ($ part , 1 );
100
+ if (0 === strpos ($ rule , '! ' )) {
101
+ $ rule = substr ($ rule , 1 );
111
102
$ isDomain = false ;
112
103
}
113
104
114
- if (!isset ($ list [$ part ])) {
115
- $ list [$ part ] = $ isDomain ? [] : ['! ' => '' ];
116
- }
117
-
105
+ $ list [$ rule ] = $ list [$ rule ] ?? ($ isDomain ? [] : ['! ' => '' ]);
118
106
if ($ isDomain && !empty ($ rule_parts )) {
119
- $ list [$ part ] = $ this ->addRule ($ list [$ part ], $ rule_parts );
107
+ $ list [$ rule ] = $ this ->addRule ($ list [$ rule ], $ rule_parts );
120
108
}
121
109
122
110
return $ list ;
0 commit comments