Skip to content
This repository was archived by the owner on Sep 22, 2025. It is now read-only.

Commit 81460e2

Browse files
committed
Improve normalization
1 parent 3a8aaab commit 81460e2

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/Rfc3986/Uri.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ final class Uri
4545
private readonly array $rawComponents;
4646
private readonly string $rawUri;
4747
/** @var Components */
48-
private array $normalizedComponents = self::DEFAULT_COMPONENTS;
49-
private ?string $normalizedUri = null;
48+
private array $normalizedComponents;
49+
private ?string $normalizedUri;
5050

5151
/**
5252
* @throws InvalidUriException
@@ -65,6 +65,8 @@ public function __construct(string $uri, ?string $baseUri = null)
6565

6666
$this->rawComponents = self::addUserInfo($components);
6767
$this->rawUri = $uri;
68+
$this->normalizedUri = null;
69+
$this->normalizedComponents = self::DEFAULT_COMPONENTS;
6870
}
6971

7072
/**
@@ -119,6 +121,7 @@ private function setNormalizedComponents(): void
119121
{
120122
if (self::DEFAULT_COMPONENTS === $this->normalizedComponents) {
121123
$this->normalizedComponents = self::addUserInfo(UriString::parseNormalized($this->toRawString()));
124+
// We convert the host separately because the current RFC does not handle IDNA
122125
$this->normalizedComponents['host'] = Encoder::normalizeHost($this->rawComponents['host']);
123126
}
124127
}
@@ -128,17 +131,11 @@ private function setNormalizedComponents(): void
128131
*/
129132
private function getComponent(string $name, string $type): ?string
130133
{
131-
if (self::TYPE_RAW === $type) {
132-
$value = $this->rawComponents[$name];
133-
if (null === $value) {
134-
return null;
135-
}
136-
137-
return (string) $value;
134+
if (self::TYPE_NORMALIZED === $type) {
135+
$this->setNormalizedComponents();
138136
}
139137

140-
$this->setNormalizedComponents();
141-
$value = $this->normalizedComponents[$name];
138+
$value = self::TYPE_NORMALIZED === $type ? $this->normalizedComponents[$name] : $this->rawComponents[$name];
142139
if (null === $value) {
143140
return null;
144141
}

0 commit comments

Comments
 (0)