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 d790f87 commit de986d9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
64 changes: 38 additions & 26 deletions 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 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());
}
}

0 comments on commit de986d9

Please sign in to comment.