Skip to content

Commit

Permalink
Adding new URIrenderer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 26, 2024
1 parent 038e7ea commit 6370d7c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 46 additions & 5 deletions Contracts/UriEncoder.php → Contracts/UriRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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>|string|null $class
* @param iterable<string, string|null> $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<string, string|null> $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<string, string|int|float|bool> $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.
Expand All @@ -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;
}
8 changes: 8 additions & 0 deletions IPv6/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
19 changes: 19 additions & 0 deletions IPv6/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}

0 comments on commit 6370d7c

Please sign in to comment.