Skip to content

Commit

Permalink
Improve components public API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jun 26, 2023
1 parent 80c4be2 commit e9148fd
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 154 deletions.
30 changes: 11 additions & 19 deletions Components/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ final class Domain extends Component implements DomainHostInterface
/** @var string[] */
private readonly array $labels;

private function __construct(Stringable|int|string $host)
private function __construct(Stringable|string|null $host)
{
$host = match (true) {
$host instanceof HostInterface => $host,
$host instanceof UriComponentInterface => Host::new($host->value()),
default => Host::new((string) $host),
default => Host::new($host),
};

if (!$host->isDomain()) {
Expand All @@ -63,15 +63,15 @@ private function __construct(Stringable|int|string $host)
*/
public static function new(Stringable|string|null $value = null): self
{
return self::fromHost(Host::new($value));
return new self($value);
}

/**
* Returns a new instance from an iterable structure.
*
* @throws TypeError If a label is the null value
*/
public static function fromLabels(iterable $labels): self
public static function fromLabels(Stringable|string ...$labels): self
{
$hostLabels = [];
foreach ($labels as $label) {
Expand All @@ -90,23 +90,15 @@ public static function fromLabels(iterable $labels): self
*/
public static function fromUri(Stringable|string $uri): self
{
return self::fromHost(Host::fromUri($uri));
return self::new(Host::fromUri($uri));
}

/**
* Create a new instance from an Authority object.
*/
public static function fromAuthority(Stringable|string $authority): self
{
return self::fromHost(Host::fromAuthority($authority));
}

/**
* Returns a new instance from an iterable structure.
*/
public static function fromHost(HostInterface $host): self
{
return new self($host);
return self::new(Host::fromAuthority($authority));
}

public function value(): ?string
Expand Down Expand Up @@ -233,7 +225,7 @@ public function withoutRootLabel(): DomainHostInterface
$labels = $this->labels;
array_shift($labels);

return self::fromLabels($labels);
return self::fromLabels(...$labels);
}

/**
Expand Down Expand Up @@ -294,7 +286,7 @@ public function withoutLabel(int ...$keys): DomainHostInterface
$deleted_keys = array_keys(array_count_values($keys));
$filter = static fn ($key): bool => !in_array($key, $deleted_keys, true);

return self::fromLabels(array_filter($this->labels, $filter, ARRAY_FILTER_USE_KEY));
return self::fromLabels(...array_filter($this->labels, $filter, ARRAY_FILTER_USE_KEY));
}

public function clear(): self
Expand Down Expand Up @@ -346,7 +338,7 @@ public static function createFromString(Stringable|string $host): self
*/
public static function createFromLabels(iterable $labels): self
{
return self::fromLabels($labels);
return self::fromLabels(...$labels);
}

/**
Expand Down Expand Up @@ -383,14 +375,14 @@ public static function createFromAuthority(AuthorityInterface|Stringable|string
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Domain::fromHost()
* @see Domain::new()
*
* @codeCoverageIgnore
*
* Returns a new instance from an iterable structure.
*/
public static function createFromHost(HostInterface $host): self
{
return self::fromHost($host);
return self::new($host);
}
}
29 changes: 7 additions & 22 deletions Components/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use League\Uri\Uri;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use TypeError;
use function date_create;

/**
* @group host
Expand All @@ -32,7 +30,7 @@ final class DomainTest extends TestCase
public function testItCanBeInstantiatedWithAHostInterfaceImplementingObject(): void
{
$host = Host::new('uri.thephpleague.com');
$domain = Domain::fromHost($host);
$domain = Domain::new($host);

self::assertSame('uri.thephpleague.com', $domain->value());
}
Expand All @@ -41,14 +39,13 @@ public function testItFailsIfTheHostInterfaceImplementingObjectIsNotADomain(): v
{
$this->expectException(UriException::class);

Domain::fromHost(Host::fromIp('127.0.0.1'));
Domain::new(Host::fromIp('127.0.0.1'));
}

public function testItFailsIfTheHostIsNotADomain(): void
{
$this->expectException(UriException::class);

Domain::fromHost(Domain::new('127.0.0.1'));
Domain::new('127.0.0.1');
}

public function testIterator(): void
Expand Down Expand Up @@ -120,7 +117,7 @@ public function testInvalidDomain(?string $invalid): void
{
$this->expectException(SyntaxError::class);

Domain::fromHost(Host::new($invalid));
Domain::new($invalid);
}

public static function invalidDomainProvider(): array
Expand Down Expand Up @@ -235,7 +232,7 @@ public static function countableProvider(): array
*/
public function testCreateFromLabels(iterable $input, string $expected): void
{
self::assertSame($expected, (string) Domain::fromLabels($input));
self::assertSame($expected, (string) Domain::fromLabels(...$input));
}

public static function createFromLabelsValid(): array
Expand All @@ -248,28 +245,16 @@ public static function createFromLabelsValid(): array
];
}

public function testCreateFromLabelsFailedWithInvalidArrayInput(): void
{
$this->expectException(TypeError::class);
Domain::fromLabels([date_create()]);
}

public function testCreateFromLabelsFailedWithNullLabel(): void
{
$this->expectException(TypeError::class);
Domain::fromLabels([null]);
}

public function testCreateFromLabelsFailedWithEmptyStringLabel(): void
{
$this->expectException(SyntaxError::class);
Domain::fromLabels(['']);
Domain::fromLabels('');
}

public function testCreateFromLabelsFailedWithEmptyLabel(): void
{
$this->expectException(SyntaxError::class);
Domain::fromLabels([]);
Domain::fromLabels();
}

public function testGet(): void
Expand Down
69 changes: 31 additions & 38 deletions Components/HierarchicalPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use League\Uri\Exceptions\SyntaxError;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
use Traversable;
use TypeError;
use function array_count_values;
use function array_filter;
Expand Down Expand Up @@ -74,30 +73,42 @@ private function __construct(Stringable|string|int $path)
*/
public static function new(Stringable|string $value = ''): self
{
return self::fromPath(Path::new($value));
return new self($value);
}

public static function fromPath(PathInterface $path): self
/**
* Create a new instance from a URI object.
*/
public static function fromUri(Stringable|string $uri): self
{
return new self($path);
return new self(Path::fromUri($uri));
}

/**
* Returns a new instance from an iterable structure.
*
* @throws TypeError If the segments are malformed
*/
public static function fromRelativeSegments(iterable $segments): self
public static function fromRelative(string ...$segments): self
{
return self::fromSegments(self::IS_RELATIVE, $segments);
}

private static function fromSegments(int $pathType, iterable $segments): self
/**
* Returns a new instance from an iterable structure.
*
* @throws TypeError If the segments are malformed
*/
public static function fromAbsolute(string ...$segments): self
{
if ($segments instanceof Traversable) {
$segments = iterator_to_array($segments, false);
}
return self::fromSegments(self::IS_ABSOLUTE, $segments);
}

/**
* @param array<string> $segments
*/
private static function fromSegments(int $pathType, array $segments): self
{
$pathSegments = array_map(fn (Stringable|string $segment): string => (string) $segment, $segments);
$path = implode(self::SEPARATOR, $pathSegments);
if (self::IS_RELATIVE === $pathType) {
Expand All @@ -111,24 +122,6 @@ private static function fromSegments(int $pathType, iterable $segments): self
return new self($path);
}

/**
* Returns a new instance from an iterable structure.
*
* @throws TypeError If the segments are malformed
*/
public static function fromAbsoluteSegments(iterable $segments): self
{
return self::fromSegments(self::IS_ABSOLUTE, $segments);
}

/**
* Create a new instance from a URI object.
*/
public static function fromUri(Stringable|string $uri): self
{
return new self(Path::fromUri($uri));
}

public function count(): int
{
return count($this->segments);
Expand Down Expand Up @@ -465,7 +458,7 @@ private function buildBasename(string $extension, string $ext, string $param = n
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see HierarchicalPath::fromPath()
* @see HierarchicalPath::new()
*
* @codeCoverageIgnore
*
Expand All @@ -480,47 +473,47 @@ public static function createFromString(Stringable|string $path): self
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see HierarchicalPath::fromPath()
* @see HierarchicalPath::new()
*
* @codeCoverageIgnore
*/
public static function createFromPath(PathInterface $path): self
{
return self::fromPath($path);
return self::new($path);
}

/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see HierarchicalPath::fromRelativeSegments()
* @throws TypeError If the segments are malformed
*@see HierarchicalPath::fromRelative()
*
* @codeCoverageIgnore
*
* Returns a new instance from an iterable structure.
*
* @throws TypeError If the segments are malformed
* @deprecated Since version 7.0.0
*/
public static function createRelativeFromSegments(iterable $segments): self
{
return self::fromRelativeSegments($segments);
return self::fromRelative(...$segments);
}

/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see HierarchicalPath::fromAbsoluteSegments()
* @throws TypeError If the segments are malformed
*@see HierarchicalPath::fromAbsolute()
*
* @codeCoverageIgnore
*
* Returns a new instance from an iterable structure.
*
* @throws TypeError If the segments are malformed
* @deprecated Since version 7.0.0
*/
public static function createAbsoluteFromSegments(iterable $segments): self
{
return self::fromAbsoluteSegments($segments);
return self::fromAbsolute(...$segments);
}

/**
Expand Down
18 changes: 2 additions & 16 deletions Components/HierarchicalPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use League\Uri\Uri;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use TypeError;
use function date_create;
use function iterator_to_array;

/**
Expand Down Expand Up @@ -177,7 +175,7 @@ public static function withoutLeadingSlashProvider(): array
*/
public function testCreateRelativeFromSegments(iterable $input, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::fromRelativeSegments($input));
self::assertSame($expected, (string) HierarchicalPath::fromRelative(...$input));
}

public static function createFromRelativeSegmentsValid(): array
Expand All @@ -196,7 +194,7 @@ public static function createFromRelativeSegmentsValid(): array
*/
public function testCreateAbsoluteFromSegments(iterable $input, string $expected): void
{
self::assertSame($expected, (string) HierarchicalPath::fromAbsoluteSegments($input));
self::assertSame($expected, (string) HierarchicalPath::fromAbsolute(...$input));
}

public static function createFromAbsoluteSegmentsValid(): array
Expand All @@ -214,18 +212,6 @@ public static function createFromAbsoluteSegmentsValid(): array
];
}

public function testCreateRelativeFromSegmentsFailed(): void
{
$this->expectException(TypeError::class);
HierarchicalPath::fromRelativeSegments([date_create()]);
}

public function testCreateAbsoluteFromSegmentsFailed(): void
{
$this->expectException(TypeError::class);
HierarchicalPath::fromAbsoluteSegments([date_create()]);
}

/**
* @dataProvider prependData
*/
Expand Down
Loading

0 comments on commit e9148fd

Please sign in to comment.