From 6370d7c5d8e7060fc061e1f4f538b58355724cef Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Thu, 26 Dec 2024 19:00:37 +0100 Subject: [PATCH] Adding new URIrenderer methods --- CHANGELOG.md | 1 + Contracts/{UriEncoder.php => UriRenderer.php} | 51 +++++++++++++++++-- IPv6/Converter.php | 8 +++ IPv6/ConverterTest.php | 19 +++++++ 4 files changed, 74 insertions(+), 5 deletions(-) rename Contracts/{UriEncoder.php => UriRenderer.php} (53%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bc4396..f17069d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All Notable changes to `League\Uri\Interfaces` will be documented in this file - `UriInterface::equals` - `UriInterface::toNormalizedString` - `UriInterface::getUser` +- `League\Uri\IPv6\Converter::isIpv6` ### Fixed diff --git a/Contracts/UriEncoder.php b/Contracts/UriRenderer.php similarity index 53% rename from Contracts/UriEncoder.php rename to Contracts/UriRenderer.php index b37286a..96fb20a 100644 --- a/Contracts/UriEncoder.php +++ b/Contracts/UriRenderer.php @@ -16,11 +16,15 @@ use DOMException; use JsonSerializable; use League\Uri\UriString; +use RuntimeException; +use SplFileInfo; +use SplFileObject; +use Stringable; /** * @phpstan-import-type ComponentMap from UriString */ -interface UriEncoder extends JsonSerializable +interface UriRenderer extends JsonSerializable { /** * Returns the string representation as a URI reference. @@ -51,19 +55,37 @@ public function toDisplayString(): ?string; */ public function jsonSerialize(): string; + /** + * Returns the markdown string representation of the anchor tag with the current instance as its href attribute. + */ + public function toMarkdown(?string $linkTextTemplate = null): string; + /** * Returns the HTML string representation of the anchor tag with the current instance as its href attribute. * - * @param list|string|null $class + * @param iterable $attributes an ordered map of key value. you must quote the value if needed * * @throws DOMException */ - public function toAnchorTag(?string $linkText = null, array|string|null $class = null, ?string $target = null): string; + public function toAnchorTag(?string $linkTextTemplate = null, iterable $attributes = []): string; /** - * Returns the markdown string representation of the anchor tag with the current instance as its href attribute. + * Returns the Link tag content for the current instance. + * + * @param iterable $attributes an ordered map of key value. you must quote the value if needed + * + * @throws DOMException */ - public function toMarkdown(?string $linkText = null): string; + public function toLinkTag(iterable $attributes = []): string; + + /** + * Returns the Link header content for a single item. + * + * @param iterable $parameters an ordered map of key value. you must quote the value if needed + * + * @see https://www.rfc-editor.org/rfc/rfc7230.html#section-3.2.6 + */ + public function toLinkFieldValue(iterable $parameters = []): string; /** * Returns the Unix filesystem path. The method returns null for any other scheme except the file scheme. @@ -81,4 +103,23 @@ public function toWindowsPath(): ?string; * @return ComponentMap */ public function toComponents(): array; + + /** + * Returns a string representation of a File URI according to RFC8089. + * + * The method will return null if the URI scheme is not the `file` scheme + * + * @see https://datatracker.ietf.org/doc/html/rfc8089 + */ + public function toRfc8089(): ?string; + + /** + * Save the data to a specific file. The method returns null for any other scheme except the data scheme. + * + * @param SplFileInfo|SplFileObject|resource|Stringable|string $destination + * @param ?resource $context + * + * @throws RuntimeException if the content can not be saved. + */ + public function toFileContents(mixed $destination, $context = null): ?int; } diff --git a/IPv6/Converter.php b/IPv6/Converter.php index f645c1d..43fc4dc 100644 --- a/IPv6/Converter.php +++ b/IPv6/Converter.php @@ -134,4 +134,12 @@ private static function parse(Stringable|string|null $host): array default => ['ipAddress' => null, 'zoneIdentifier' => null], }; } + + /** + * Tells whether the host is an IPv6. + */ + public static function isIpv6(Stringable|string|null $host): bool + { + return null !== self::parse($host)['ipAddress']; + } } diff --git a/IPv6/ConverterTest.php b/IPv6/ConverterTest.php index 195ef19..08faba3 100644 --- a/IPv6/ConverterTest.php +++ b/IPv6/ConverterTest.php @@ -81,4 +81,23 @@ public static function invalidIpv6(): iterable yield 'IPv6 with zoneIdentifier' => ['invalidIp' => 'fe80::a%25en1']; } + + #[DataProvider('providerInvalidHost')] + public function testParseWithInvalidHost(?string $input): void + { + self::assertFalse(Converter::isIpv6($input)); + } + + public static function providerInvalidHost(): array + { + return [ + 'null host' => ['input' => null], + 'empty host' => ['input' => ''], + 'non ip host' => ['input' => 'ulb.ac.be'], + 'invalid host (0)' => ['input' => '192.168.1.1'], + 'invalid host (1)' => ['input' => '[192.168.1.1]'], + 'invalid host (2)' => ['input' => 'v42.fdfsffd'], + 'invalid host (3)' => ['input' => '::1'], + ]; + } }