Skip to content

Commit

Permalink
Simplify UriString method return type
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 27, 2024
1 parent cb21a4b commit 73dbed4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 54 deletions.
60 changes: 20 additions & 40 deletions docs/interfaces/7.0/uri-parser-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ The class act as a drop-in replacement for PHP's `parse_url` feature.
## URI parsing

~~~php
UriString::resolve(string $uri, ?string $baseUri = null): array
UriString::parse(string $uri): array
UriString::normalize(string $uri): string
UriString::parseAuthority(string $autority): array
UriString::normalizeAuthority(string $autority): string
UriString::resolve(string $uri, ?string $baseUri = null): string
~~~

The parser is:
Expand Down Expand Up @@ -74,17 +76,31 @@ a base URI which must be absolute, the URI will then be resolved using the base

```php
$components = UriString::resolve('"/foo", "https://example.com");
//returns "https://example.com/foo"
```

It is possible to normalize a URI against the RFC3986 rules using the `UriString::normalize` method.
The method expects a string and will return the same array as `UriString::parse` but each component will
have been normalized.

```php
use League\Uri\UriString;

$parsed = UriString::parse("https://EXAMPLE.COM/foo/../bar");
//returns the following array
//array(
// 'scheme' => 'https',
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'example.com'',
// 'host' => 'EXAMPLE.COM',
// 'port' => null,
// 'path' => '/foo',
// 'path' => '/foo/../bar',
// 'query' => null,
// 'fragment' => null,
//);

$normalized = UriString::normalize("https://EXAMPLE.COM/foo/../bar");
//returns "https://example.com/bar"
```

## URI Building
Expand Down Expand Up @@ -119,39 +135,3 @@ echo UriString::build($components); //displays http://hello:[email protected][email protected]
The `build` method provides similar functionality to the `http_build_url()` function from v1.x of the [`pecl_http`](https://pecl.php.net/package/pecl_http) PECL extension.

<p class="message-notice">The class also exposes a <code>UriString::buildAuthority</code> you can use to build an authority from its hash representation.</p>

## URI Normalization

It is possible to normalize a URI against the RFC3986 rules using the `UriString::normalize` method.
The method expects a string and will return the same array as `UriString::parse` but each component will
have been normalized.

```php
use League\Uri\UriString;

$parsed = UriString::parse("https://EXAMPLE.COM/foo/../bar");
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'EXAMPLE.COM',
// 'port' => null,
// 'path' => '/foo/../bar',
// 'query' => null,
// 'fragment' => null,
//);

$normalized = UriString::normalize("https://EXAMPLE.COM/foo/../bar");
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'example.com',
// 'port' => null,
// 'path' => '/bar',
// 'query' => null,
// 'fragment' => null,
//);
```
3 changes: 2 additions & 1 deletion interfaces/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ All Notable changes to `League\Uri\Interfaces` will be documented in this file
- `League\Uri\IPv6\Converter::isIpv6`
- `UriString::resolve`
- `UriString::removeDotSegments`
- `UriString::normalize`
- `UriString::parseAndNormalize`
- `UriString::parseAuthorityAndNormalize`

### Fixed

Expand Down
35 changes: 25 additions & 10 deletions interfaces/UriString.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use function rawurldecode;
use function sprintf;
use function strpos;
use function strtolower;
use function substr;

use const FILTER_FLAG_IPV6;
Expand Down Expand Up @@ -275,10 +276,8 @@ public static function buildAuthority(array $components): ?string
* Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
*
* @throws SyntaxError if the URI is not parsable
*
* @return ComponentMap
*/
public static function parseAndNormalize(Stringable|string $uri): array
public static function normalize(Stringable|string $uri): string
{
$components = UriString::parse($uri);
if (null !== $components['scheme']) {
Expand All @@ -305,7 +304,25 @@ public static function parseAndNormalize(Stringable|string $uri): array
$components['user'] = Encoder::decodeUnreservedCharacters($components['user']);
$components['pass'] = Encoder::decodeUnreservedCharacters($components['pass']);

return $components;
return self::build($components);
}

/**
* Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
*
* @throws SyntaxError if the URI is not parsable
*/
public static function normalizeAuthority(Stringable|string $authority): string
{
$components = UriString::parseAuthority($authority);
if (null !== $components['host']) {
$components['host'] = IdnaConverter::toUnicode((string)IPv6Converter::compress($components['host']))->domain();
}

$components['user'] = Encoder::decodeUnreservedCharacters($components['user']);
$components['pass'] = Encoder::decodeUnreservedCharacters($components['pass']);

return (string) self::buildAuthority($components);
}

/**
Expand All @@ -320,10 +337,8 @@ public static function parseAndNormalize(Stringable|string $uri): array
* @see https://www.rfc-editor.org/rfc/rfc3986.html#section-5
*
* @throws SyntaxError if the BaseUri is not absolute or in absence of a BaseUri if the uri is not absolute
*
* @return ComponentMap
*/
public static function resolve(Stringable|string $uri, Stringable|string|null $baseUri = null): array
public static function resolve(Stringable|string $uri, Stringable|string|null $baseUri = null): string
{
$uri = self::parse($uri);
$baseUri = null !== $baseUri ? self::parse($baseUri) : $uri;
Expand All @@ -334,14 +349,14 @@ public static function resolve(Stringable|string $uri, Stringable|string|null $b
if (null !== $uri['scheme'] && '' !== $uri['scheme']) {
$uri['path'] = self::removeDotSegments($uri['path']);

return $uri;
return UriString::build($uri);
}

if (null !== self::buildAuthority($uri)) {
$uri['scheme'] = $baseUri['scheme'];
$uri['path'] = self::removeDotSegments($uri['path']);

return $uri;
return UriString::build($uri);
}

[$path, $query] = self::resolvePathAndQuery($uri, $baseUri);
Expand All @@ -354,7 +369,7 @@ public static function resolve(Stringable|string $uri, Stringable|string|null $b
$baseUri['query'] = $query;
$baseUri['fragment'] = $uri['fragment'];

return $baseUri;
return UriString::build($baseUri);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion interfaces/UriStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ public static function buildUriProvider(): array
#[DataProvider('resolveProvider')]
public function testCreateResolve(string $baseUri, string $uri, string $expected): void
{
self::assertSame($expected, UriString::build(UriString::resolve($uri, $baseUri)));
self::assertSame($expected, UriString::resolve($uri, $baseUri));
}

public static function resolveProvider(): array
Expand Down
4 changes: 2 additions & 2 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -1670,7 +1670,7 @@ public function equals(UriInterface|Stringable|string $uri, bool $excludeFragmen
*/
public function normalize(): UriInterface
{
return self::fromComponents(UriString::parseAndNormalize($this->toString()));
return self::new(UriString::normalize($this->toString()));
}

/**
Expand All @@ -1686,7 +1686,7 @@ public function normalize(): UriInterface
*/
public function resolve(Stringable|string $uri): UriInterface
{
return self::fromComponents(UriString::resolve($uri, $this->toString()));
return self::new(UriString::resolve($uri, $this->toString()));
}

/**
Expand Down

0 comments on commit 73dbed4

Please sign in to comment.