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

Commit fac3777

Browse files
committed
Improve implementation
1 parent 3ba186e commit fac3777

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/Rfc3986/Uri.php

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

5152
/**
5253
* @throws InvalidUriException
@@ -65,8 +66,7 @@ public function __construct(string $uri, ?string $baseUri = null)
6566

6667
$this->rawComponents = self::addUserInfo($components);
6768
$this->rawUri = $uri;
68-
$this->normalizedUri = null;
69-
$this->normalizedComponents = self::DEFAULT_COMPONENTS;
69+
$this->isNormalized = false;
7070
}
7171

7272
/**
@@ -119,10 +119,11 @@ public static function parse(string $uri, ?string $baseUri = null): ?Uri
119119

120120
private function setNormalizedComponents(): void
121121
{
122-
if (self::DEFAULT_COMPONENTS === $this->normalizedComponents) {
122+
if (!$this->isNormalized) {
123123
$this->normalizedComponents = self::addUserInfo(UriString::parseNormalized($this->toRawString()));
124124
// We convert the host separately because the current RFC does not handle IDNA
125125
$this->normalizedComponents['host'] = Encoder::normalizeHost($this->rawComponents['host']);
126+
$this->isNormalized = true;
126127
}
127128
}
128129

@@ -389,8 +390,7 @@ public function __unserialize(array $data): void
389390

390391
$this->rawComponents = $uri->rawComponents;
391392
$this->rawUri = $uri->rawUri;
392-
$this->normalizedComponents = self::DEFAULT_COMPONENTS;
393-
$this->normalizedUri = null;
393+
$this->isNormalized = false;
394394
}
395395

396396
/**

tests/Rfc3986/UriTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public function it_will_normalize_the_uri_according_to_rfc3986(): void
118118
{
119119
$uri = new Uri("https://[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]/?foo=bar%26baz%3Dqux");
120120

121-
self::assertSame('[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]', $uri->getHost());
121+
self::assertSame('[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]', $uri->getRawHost());
122+
self::assertSame('[2001:0db8:0001:0000:0000:0ab9:c0a8:0102]', $uri->getHost());
122123
self::assertSame('foo=bar%26baz%3Dqux', $uri->getQuery());
123124
self::assertSame('foo=bar%26baz%3Dqux', $uri->getRawQuery());
124125
}
@@ -192,7 +193,7 @@ public function it_can_be_check_for_equivalent(): void
192193
$uri1 = new Uri('http://example.com#foobar');
193194
$uri2 = new Uri('http://example.com');
194195

195-
self::assertTrue($uri1->equals($uri2, true));
196+
self::assertTrue($uri1->equals($uri2));
196197
self::assertFalse($uri1->equals($uri2, false));
197198
}
198199

0 commit comments

Comments
 (0)