Skip to content

Commit 31462d1

Browse files
committed
Improve internal codebase
1 parent ab6baae commit 31462d1

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

src/Domain.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ final class Domain implements DomainName
4646

4747
private function __construct(private string $type, DomainNameProvider|Host|Stringable|string|int|null $domain)
4848
{
49-
$this->type = $type;
5049
$this->domain = $this->parseDomain($domain);
5150
$this->labels = null === $this->domain ? [] : array_reverse(explode('.', $this->domain));
5251
}
@@ -113,45 +112,38 @@ private function parseValue(Stringable|string|int|null $domain): ?string
113112
}
114113

115114
$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+
};
131123
}
132124

133125
private function domainToAscii(string $domain): string
134126
{
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();
138131
}
139132

140133
private function domainToUnicode(string $domain): string
141134
{
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();
145139
}
146140

147141
/**
148142
* @return Iterator<string>
149143
*/
150144
public function getIterator(): Iterator
151145
{
152-
foreach ($this->labels as $label) {
153-
yield $label;
154-
}
146+
yield from $this->labels;
155147
}
156148

157149
public function isAscii(): bool

src/Idna.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static function toAscii(string $domain, int $options): IdnaInfo
118118
*/
119119
public static function toUnicode(string $domain, int $options): IdnaInfo
120120
{
121-
if (false === strpos($domain, 'xn--')) {
121+
if (!str_contains($domain, 'xn--')) {
122122
return IdnaInfo::fromIntl(['result' => $domain, 'isTransitionalDifferent' => false, 'errors' => 0]);
123123
}
124124

0 commit comments

Comments
 (0)