Skip to content

Commit 457f548

Browse files
committed
Update UriTemplate API to keep te method consistent
1 parent 531c8b7 commit 457f548

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

phpunit.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
colors="true"
7+
processIsolation="false"
8+
stopOnFailure="true"
9+
cacheDirectory=".phpunit.cache"
10+
backupStaticProperties="false"
11+
>
312
<coverage>
413
<report>
514
<clover outputFile="build/clover.xml"/>

polyfill/Polyfill/UrlTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,24 @@ public function it_will_update_the_port_and_the_scheme(): void
347347
self::assertTrue($resScheme->equals($resSchemeDot));
348348
self::assertTrue($resSchemeDot->equals($resSchemeDotSlashes));
349349
}
350+
351+
public function test_it_will_return_the_host_correct_format(): void
352+
{
353+
$uri = new Url('https://bébé.be');
354+
355+
self::assertSame('https://xn--bb-bjab.be/', $uri->toAsciiString());
356+
self::assertSame('https://bébé.be/', $uri->toUnicodeString());
357+
358+
self::assertSame('xn--bb-bjab.be', $uri->getAsciiHost());
359+
self::assertSame('bébé.be', $uri->getUnicodeHost());
360+
361+
$uri = new Url('https://xn--bb-bjab.be');
362+
363+
self::assertSame('https://xn--bb-bjab.be/', $uri->toAsciiString());
364+
self::assertSame('https://bébé.be/', $uri->toUnicodeString());
365+
366+
self::assertSame('xn--bb-bjab.be', $uri->getAsciiHost());
367+
self::assertSame('bébé.be', $uri->getUnicodeHost());
368+
}
369+
350370
}

polyfill/lib/WhatWg/Url.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Uri\UriComparisonMode;
2828

2929
use function in_array;
30+
use function preg_match;
3031
use function substr;
3132

3233
use const PHP_VERSION_ID;
@@ -44,12 +45,18 @@ final class Url
4445
{
4546
private const PORT_RANGE_MIN = 0;
4647
private const PORT_RANGE_MAX = 65535;
48+
private const REGEXP_IDNA_PATTERN = '/[^\x20-\x7f]/';
4749

4850
private WhatWgURL $url;
51+
4952
private ?string $unicodeHost = null;
5053
private bool $unicodeHostInitialized = false;
5154
private ?string $urlUnicodeString = null;
5255

56+
private ?string $asciiHost = null;
57+
private bool $asciiHostInitialized = false;
58+
private ?string $urlAsciiString = null;
59+
5360
/**
5461
* @param list<UrlValidationError> $errors
5562
*/
@@ -167,7 +174,14 @@ public function withPassword(#[SensitiveParameter] ?string $password): self
167174

168175
public function getAsciiHost(): ?string
169176
{
170-
return $this->url->hostname;
177+
if ($this->asciiHostInitialized) {
178+
return $this->asciiHost;
179+
}
180+
181+
$this->asciiHost = $this->setAsciiHost();
182+
$this->asciiHostInitialized = true;
183+
184+
return $this->asciiHost;
171185
}
172186

173187
public function getUnicodeHost(): ?string
@@ -210,6 +224,29 @@ private function setUnicodeHost(): ?string
210224
return $result->getDomain();
211225
}
212226

227+
private function setAsciiHost(): ?string
228+
{
229+
$host = $this->url->hostname;
230+
if ('' === $host || null === $host || 1 !== preg_match(self::REGEXP_IDNA_PATTERN, $host)) {
231+
return $host;
232+
}
233+
234+
$result = Idna::toAscii($host, [
235+
'CheckHyphens' => false,
236+
'CheckBidi' => true,
237+
'CheckJoiners' => true,
238+
'UseSTD3ASCIIRules' => false,
239+
'Transitional_Processing' => false,
240+
'IgnoreInvalidPunycode' => false,
241+
]);
242+
243+
if ($result->hasErrors()) {
244+
return $host;
245+
}
246+
247+
return $result->getDomain();
248+
}
249+
213250
/**
214251
* @throws InvalidUrlException
215252
*/
@@ -355,7 +392,22 @@ private static function urlRecord(self $url): URLRecord
355392

356393
public function toAsciiString(): string
357394
{
358-
return $this->url->href;
395+
if (null !== $this->urlAsciiString) {
396+
return $this->urlAsciiString;
397+
}
398+
399+
$asciiHost = $this->getAsciiHost();
400+
if (null === $asciiHost || $this->getUnicodeHost() === $asciiHost) {
401+
$this->urlAsciiString = $this->url->href;
402+
403+
return $this->urlAsciiString;
404+
}
405+
406+
$urlRecord = self::urlRecord($this);
407+
$urlRecord->host = new StringHost($asciiHost);
408+
$this->urlAsciiString = $urlRecord->serializeURL();
409+
410+
return $this->urlAsciiString;
359411
}
360412

361413
public function toUnicodeString(): string

0 commit comments

Comments
 (0)