@@ -46,7 +46,6 @@ final class Domain implements DomainName
46
46
47
47
private function __construct (private string $ type , DomainNameProvider |Host |Stringable |string |int |null $ domain )
48
48
{
49
- $ this ->type = $ type ;
50
49
$ this ->domain = $ this ->parseDomain ($ domain );
51
50
$ this ->labels = null === $ this ->domain ? [] : array_reverse (explode ('. ' , $ this ->domain ));
52
51
}
@@ -113,45 +112,38 @@ private function parseValue(Stringable|string|int|null $domain): ?string
113
112
}
114
113
115
114
$ formattedDomain = rawurldecode ($ domain );
116
- if (1 === preg_match (self ::REGEXP_REGISTERED_NAME , $ formattedDomain )) {
117
- return strtolower ($ formattedDomain );
118
- }
119
-
120
- // a domain name can not contains URI delimiters or space
121
- if (1 === preg_match (self ::REGEXP_URI_DELIMITERS , $ formattedDomain )) {
122
- throw SyntaxError::dueToInvalidCharacters ($ domain );
123
- }
124
-
125
- // if the domain name does not contains UTF-8 chars then it is malformed
126
- if (1 !== preg_match (self ::REGEXP_IDN_PATTERN , $ formattedDomain )) {
127
- throw SyntaxError::dueToMalformedValue ($ domain );
128
- }
129
-
130
- return $ this ->domainToUnicode ($ this ->domainToAscii ($ formattedDomain ));
115
+ return match (true ) {
116
+ 1 === preg_match (self ::REGEXP_REGISTERED_NAME , $ formattedDomain ) => strtolower ($ formattedDomain ),
117
+ // a domain name can not contain URI delimiters or space
118
+ 1 === preg_match (self ::REGEXP_URI_DELIMITERS , $ formattedDomain ) => throw SyntaxError::dueToInvalidCharacters ($ domain ),
119
+ // if the domain name does not contain UTF-8 chars then it is malformed
120
+ 1 !== preg_match (self ::REGEXP_IDN_PATTERN , $ formattedDomain ) => throw SyntaxError::dueToMalformedValue ($ domain ),
121
+ default => $ this ->domainToUnicode ($ this ->domainToAscii ($ formattedDomain )),
122
+ };
131
123
}
132
124
133
125
private function domainToAscii (string $ domain ): string
134
126
{
135
- $ option = self ::IDNA_2003 === $ this ->type ? Idna::IDNA2003_ASCII : Idna::IDNA2008_ASCII ;
136
-
137
- return Idna::toAscii ($ domain , $ option )->result ();
127
+ return Idna::toAscii (
128
+ $ domain ,
129
+ self ::IDNA_2003 === $ this ->type ? Idna::IDNA2003_ASCII : Idna::IDNA2008_ASCII
130
+ )->result ();
138
131
}
139
132
140
133
private function domainToUnicode (string $ domain ): string
141
134
{
142
- $ option = self ::IDNA_2003 === $ this ->type ? Idna::IDNA2003_UNICODE : Idna::IDNA2008_UNICODE ;
143
-
144
- return Idna::toUnicode ($ domain , $ option )->result ();
135
+ return Idna::toUnicode (
136
+ $ domain ,
137
+ self ::IDNA_2003 === $ this ->type ? Idna::IDNA2003_UNICODE : Idna::IDNA2008_UNICODE
138
+ )->result ();
145
139
}
146
140
147
141
/**
148
142
* @return Iterator<string>
149
143
*/
150
144
public function getIterator (): Iterator
151
145
{
152
- foreach ($ this ->labels as $ label ) {
153
- yield $ label ;
154
- }
146
+ yield from $ this ->labels ;
155
147
}
156
148
157
149
public function isAscii (): bool
0 commit comments