Skip to content

Commit 0d9a875

Browse files
committed
Revert Uri::getPath behaviour as Uri is more generic than Http
1 parent e785e0d commit 0d9a875

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

uri/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ All Notable changes to `League\Uri` will be documented in this file
4646
- `Uri` host encoding compliance to RFC3986 is improved by supporting RFC3986 encoded URI properly
4747
- `Uri` parsing with strings started or ended with empty string are no longer allowed
4848
- `Uri` space are rawurlencoded.
49+
- `Uri::getPath` no longer trim the leading slashes (the `Http` class which is a PSR-7 compliant class still do!)
4950

5051
### Deprecated
5152

uri/Http.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Uri\WhatWg\Url as WhatWgUrl;
2727

2828
use function is_bool;
29+
use function ltrim;
2930

3031
/**
3132
* @phpstan-import-type InputComponentMap from UriString
@@ -185,7 +186,12 @@ public function getPort(): ?int
185186

186187
public function getPath(): string
187188
{
188-
return $this->uri->getPath();
189+
$path = $this->uri->getPath();
190+
191+
return match (true) {
192+
str_starts_with($path, '//') => '/'.ltrim($path, '/'),
193+
default => $path,
194+
};
189195
}
190196

191197
public function getQuery(): string

uri/Uri.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
use function is_array;
6464
use function is_bool;
6565
use function is_string;
66-
use function ltrim;
6766
use function preg_match;
6867
use function preg_replace_callback;
6968
use function rawurldecode;
@@ -1463,10 +1462,7 @@ public function getPort(): ?int
14631462

14641463
public function getPath(): string
14651464
{
1466-
return match (true) {
1467-
str_starts_with($this->path, '//') => '/'.ltrim($this->path, '/'),
1468-
default => $this->path,
1469-
};
1465+
return $this->path;
14701466
}
14711467

14721468
public function getQuery(): ?string

uri/UriTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,12 @@ public function testItStripMultipleLeadingSlashOnGetPath(): void
437437
$uri = Uri::new('https://example.com///miscillaneous.tld');
438438

439439
self::assertSame('https://example.com///miscillaneous.tld', (string) $uri);
440-
self::assertSame('/miscillaneous.tld', $uri->getPath());
440+
self::assertSame('///miscillaneous.tld', $uri->getPath());
441441

442442
$modifiedUri = $uri->withPath('///foobar');
443443

444444
self::assertSame('https://example.com///foobar', (string) $modifiedUri);
445-
self::assertSame('/foobar', $modifiedUri->getPath());
445+
self::assertSame('///foobar', $modifiedUri->getPath());
446446
self::assertSame('//example.com///foobar', (string) $modifiedUri->withScheme(null));
447447

448448
$this->expectException(SyntaxError::class);
@@ -453,15 +453,15 @@ public function testItPreservesMultipleLeadingSlashesOnMutation(): void
453453
{
454454
$uri = Uri::new('https://www.example.com///google.com');
455455
self::assertSame('https://www.example.com///google.com', (string) $uri);
456-
self::assertSame('/google.com', $uri->getPath());
456+
self::assertSame('///google.com', $uri->getPath());
457457

458458
$modifiedUri = $uri->withPath('/google.com');
459459
self::assertSame('https://www.example.com/google.com', (string) $modifiedUri);
460460
self::assertSame('/google.com', $modifiedUri->getPath());
461461

462462
$modifiedUri2 = $uri->withPath('///google.com');
463463
self::assertSame('https://www.example.com///google.com', (string) $modifiedUri2);
464-
self::assertSame('/google.com', $modifiedUri2->getPath());
464+
self::assertSame('///google.com', $modifiedUri2->getPath());
465465
}
466466

467467
public function testItCanBeUpdatedWithAnUriComponent(): void

0 commit comments

Comments
 (0)