Skip to content

Commit

Permalink
Improve and simplify codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 10, 2023
1 parent b7241ea commit 2228a07
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 223 deletions.
64 changes: 38 additions & 26 deletions components/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
use League\Uri\Contracts\UriAccess;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\IPv4\Converter;
use League\Uri\Idna\Converter as IdnConverter;
use League\Uri\IPv4\Converter as IPv4Converter;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
Expand Down Expand Up @@ -195,25 +196,31 @@ public function appendLabel(Stringable|string|null $label): static
*/
public function hostToAscii(): static
{
return new static($this->uri->withHost(
static::normalizeComponent(
Host::fromUri($this->uri)->value(),
$this->uri
)
));
$currentHost = $this->uri->getHost();
$host = IdnConverter::toAsciiOrFail((string) $currentHost);

return match (true) {
null === $currentHost,
'' === $currentHost,
$host === $currentHost => $this,
default => new static($this->uri->withHost($host)),
};
}

/**
* Convert the URI host part to its unicode value.
*/
public function hostToUnicode(): static
{
return new static($this->uri->withHost(
static::normalizeComponent(
Host::fromUri($this->uri)->toUnicode(),
$this->uri
)
));
$currentHost = $this->uri->getHost();
$host = IdnConverter::toUnicode((string) $currentHost)->domain();

return match (true) {
null === $currentHost,
'' === $currentHost,
$host === $currentHost => $this,
default => new static($this->uri->withHost($host)),
};
}

/**
Expand Down Expand Up @@ -312,13 +319,13 @@ public function removeLabels(int ...$keys): static
*/
public function removeRootLabel(): static
{
$currentHost = $this->uri->getHost();
$host = $this->uri->getHost();

return match (true) {
null === $currentHost,
'' === $currentHost,
!str_ends_with($currentHost, '.') => $this,
default => new static($this->uri->withHost(substr($currentHost, 0, -1))),
null === $host,
'' === $host,
!str_ends_with($host, '.') => $this,
default => new static($this->uri->withHost(substr($host, 0, -1))),
};
}

Expand All @@ -342,12 +349,17 @@ public function sliceLabels(int $offset, int $length = null): static
*/
public function removeZoneId(): static
{
return new static($this->uri->withHost(
static::normalizeComponent(
Host::fromUri($this->uri)->withoutZoneIdentifier()->value(),
$this->uri
)
));
$host = Host::fromUri($this->uri);

return match (true) {
$host->hasZoneIdentifier() => new static($this->uri->withHost(
static::normalizeComponent(
Host::fromUri($this->uri)->withoutZoneIdentifier()->value(),
$this->uri
)
)),
default => $this,
};
}

/**
Expand Down Expand Up @@ -584,11 +596,11 @@ final protected static function normalizeComponent(?string $component, Psr7UriIn
};
}

final protected static function ipv4Converter(): Converter
final protected static function ipv4Converter(): IPv4Converter
{
static $converter;

$converter = $converter ?? Converter::fromEnvironment();
$converter = $converter ?? IPv4Converter::fromEnvironment();

return $converter;
}
Expand Down
14 changes: 14 additions & 0 deletions components/ModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ public static function validHostProvider(): array
];
}

public function testItCanSliceHostLabels(): void
{
$uri = 'http://www.localhost.co.uk/path/to/the/sky/';

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

public function testAppendLabelWithIpv4Host(): void
{
$uri = Http::new('http://127.0.0.1/foo/bar');
Expand Down Expand Up @@ -708,4 +715,11 @@ public static function providesInvalidMethodNames(): iterable
yield 'unknown method' => ['method' => 'unknownMethod'];
yield 'case sensitive method' => ['method' => 'rePLAceExtenSIOn'];
}

public function testItCanSlicePathSegments(): void
{
$uri = 'http://www.localhost.com/path/to/the/sky/';

self::assertSame('http://www.localhost.com/the/sky/', Modifier::from($uri)->sliceSegments(2, 2)->getUriString());
}
}
8 changes: 4 additions & 4 deletions interfaces/IPv4/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ final class Converter
'/^(?<number>\d+)$/' => 10,
];

private readonly mixed $maxIpv4Number;
private readonly mixed $maxIPv4Number;

public function __construct(
private readonly Calculator $calculator
) {
$this->maxIpv4Number = $calculator->sub($calculator->pow(2, 32), 1);
$this->maxIPv4Number = $calculator->sub($calculator->pow(2, 32), 1);
}

/**
Expand Down Expand Up @@ -170,7 +170,7 @@ public function toDecimal(Stringable|string|null $host): ?string
*
* @see https://url.spec.whatwg.org/#ipv4-number-parser
*
* @return mixed Returns null if it can not correctly convert the label
* @return mixed returns null if it can not correctly convert the label
*/
private function labelToNumber(string $label): mixed
{
Expand All @@ -185,7 +185,7 @@ private function labelToNumber(string $label): mixed
}

$number = $this->calculator->baseConvert($number, $base);
if (0 <= $this->calculator->compare($number, 0) && 0 >= $this->calculator->compare($number, $this->maxIpv4Number)) {
if (0 <= $this->calculator->compare($number, 0) && 0 >= $this->calculator->compare($number, $this->maxIPv4Number)) {
return $number;
}
}
Expand Down
2 changes: 1 addition & 1 deletion interfaces/UriString.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
* @author Ignace Nyamagana Butera <[email protected]>
* @since 6.0.0
*
* @phpstan-type AuthorityMap array{user:?string, pass:?string, host:?string, port:?int}
* @phpstan-type ComponentMap array{scheme:?string, user:?string, pass:?string, host:?string, port:?int, path:string, query:?string, fragment:?string}
* @phpstan-type InputComponentMap array{scheme? : ?string, user? : ?string, pass? : ?string, host? : ?string, port? : ?int, path? : ?string, query? : ?string, fragment? : ?string}
* @phpstan-type AuthorityMap array{user:?string, pass:?string, host:?string, port:?int}
*/
final class UriString
{
Expand Down
Loading

0 comments on commit 2228a07

Please sign in to comment.