Skip to content

Commit

Permalink
Add RFC8089 documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 20, 2023
1 parent 5af8aa3 commit b2ed7fb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
19 changes: 18 additions & 1 deletion docs/uri/7.0/base-uri.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,21 @@ BaseUri::from('file:///path%20empty/bar')->unixPath();
//returns '/path empty/bar'
~~~

If the URI scheme is present and is not the `file` scheme, `null` will be returned,
If the URI scheme is present and is not the `file` scheme, `null` will be returned.

### BaseUri::toRfc8089

<p class="message-notice">since version <code>7.4.0</code></p>

Returns the RFC8089 representation of a file URI

~~~php
BaseUri::from('file://localhost/etc/fstab')->toRfc8089();
//returns 'file:/etc/fstab'

BaseUri::from('file:///path%20empty/bar')->toRfc8089();
//returns 'file:/path empty/bar'
~~~

If the URI scheme is not the `file` scheme, `null` will be returned.

7 changes: 6 additions & 1 deletion docs/uri/7.0/rfc3986.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The `fromData`
named constructor generates a [Data URI](https://datatracker.ietf.org/doc/html/rfc2397)
following its RFC specification. with the provided data and an optional mimetype and parameters.

Last but not least, you can easily translate Windows and Unix paths to URI using the two
Last but not least, you can easily translate RFC8089, Windows, Unix paths to URI using the three (3)
following methods.

~~~php
Expand All @@ -106,8 +106,13 @@ echo $uri; //returns 'file://localhost/c:My%20Documents/my%20word.docx'

$uri = Uri::fromUnixPath('/path/to/my/file.xml');
echo $uri; //returns 'file://localhost/path/to/my/file.xml'

$uri = Uri::fromRfc8089('file:/etc/fstab');
echo $uri = //returns 'file:///etc/fstab'
~~~

<p class="message-notice"><code>fromRfc8089</code> is added since version <code>7.4.0</code></p>

Accessing URI properties
-------

Expand Down
14 changes: 14 additions & 0 deletions uri/BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function windowsPath(): ?string

/**
* 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
*/
public function toRfc8089(): ?string
{
Expand All @@ -161,6 +163,18 @@ public function toRfc8089(): ?string
};
}

/**
* Tells whether the `file` scheme base URI represents a local file.
*/
public function isLocalFile(): bool
{
return match (true) {
'file' !== $this->uri->getScheme() => false,
in_array($this->uri->getAuthority(), ['', null, 'localhost'], true) => true,
default => false,
};
}

/**
* Tells whether two URI do not share the same origin.
*/
Expand Down
2 changes: 2 additions & 0 deletions uri/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ All Notable changes to `League\Uri` will be documented in this file
### Added

- `Uri::fromData`
- `Uri::fromRfc8089`
- `BaseUri::unixPath`
- `BaseUri::windowsPath`
- `BaseUri::toRfc8089`

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,10 @@ public static function fromWindowsPath(Stringable|string $path): self
public static function fromRfc8089(Stringable|string $uri): UriInterface
{
$fileUri = self::new((string) preg_replace(',^(file:/)([^/].*)$,i', 'file:///$2', (string) $uri));
$scheme = (string) $fileUri->getScheme();
$scheme = $fileUri->getScheme();

return match (true) {
'file' !== $scheme => throw new SyntaxError('The URI scheme must be `file`; `'.$scheme.'` given.'),
'file' !== $scheme => throw new SyntaxError('As per RFC8089, the URI scheme must be `file`.'),
'localhost' === $fileUri->getAuthority() => $fileUri->withHost(''),
default => $fileUri,
};
Expand Down

0 comments on commit b2ed7fb

Please sign in to comment.