Skip to content

Commit

Permalink
Adding UriAccess::getInternationalizedUriString method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 28, 2024
1 parent 06c0bc3 commit ad9f037
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All Notable changes to `League\Uri\Components` will be documented in this file
### Added

- `UrlSearchParams::uniqueKeyCount`
- `Modifier::getInternationalizedUriString`

### Fixed

Expand Down
18 changes: 18 additions & 0 deletions Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ public function getUri(): Psr7UriInterface|UriInterface
return $this->uri;
}

public function getInternationalizedUriString(): string
{
$currentHost = $this->uri->getHost();
if (null === $currentHost || '' === $currentHost) {
return $this->getUriString();
}

$host = IdnConverter::toUnicode($currentHost)->domain();
if ($host === $currentHost) {
return $this->getUriString();
}

$components = $this->uri instanceof UriInterface ? $this->uri->getComponents() : UriString::parse($this->uri);
$components['host'] = $host;

return UriString::build($components);
}

public function getUriString(): string
{
return $this->uri->__toString();
Expand Down
32 changes: 31 additions & 1 deletion ModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
use League\Uri\Components\DataPath;
use League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

use const PHP_QUERY_RFC3986;

#[CoversClass(UriModifier::class)] /** @phpstan-ignore-line */
#[CoversClass(Modifier::class)]
#[Group('host')]
#[Group('resolution')]
final class ModifierTest extends TestCase
Expand Down Expand Up @@ -829,4 +830,33 @@ public function testItCanSlicePathSegments(): void

self::assertSame('http://www.localhost.com/the/sky/', Modifier::from($uri)->sliceSegments(2, 2)->getUriString());
}

#[DataProvider('idnUriProvider')]
public function testItReturnsTheCorrectUriString(string $expected, string $input): void
{
self::assertSame($expected, Modifier::from($input)->getInternationalizedUriString());
}

public static function idnUriProvider(): iterable
{
yield 'basic uri stays the same' => [
'expected' => 'http://example.com/foo/bar',
'input' => 'http://example.com/foo/bar',
];

yield 'idn host are changed' => [
'expected' => "http://bébé.be",
'input' => "http://xn--bb-bjab.be",
];

yield 'idn host are the same' => [
'expected' => "http://bébé.be",
'input' => "http://bébé.be",
];

yield 'the rest of the URI is not affected and uses RFC3986 rules' => [
'expected' => "http://bébé.be?q=toto%20le%20h%C3%A9ros",
'input' => "http://bébé.be:80?q=toto le héros",
];
}
}

0 comments on commit ad9f037

Please sign in to comment.