-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#148 Adding more convenient methods to UriInterface
- Loading branch information
Showing
12 changed files
with
1,065 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,8 +105,7 @@ 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 | ||
------- | ||
## Accessing URI properties | ||
|
||
Let's examine the result of building a URI: | ||
|
||
|
@@ -122,6 +121,7 @@ echo $uri->getAuthority(); //displays "foo:[email protected]:81" | |
echo $uri->getPath(); //displays "/how/are/you" | ||
echo $uri->getQuery(); //displays "foo=baz" | ||
echo $uri->getFragment(); //displays "title" | ||
echo $uri->getOrigin(); //returns '' | ||
echo $uri->toString(); | ||
//displays "http://foo:[email protected]:81/how/are/you?foo=baz#title" | ||
echo json_encode($uri); | ||
|
@@ -147,8 +147,113 @@ $uri->getComponents(); | |
The returned value for each URI component is kept encoded. If you need the decoded value you should use the | ||
[league/uri-component](/components) to extract and manipulate each individual component. | ||
|
||
Modifying URI properties | ||
------- | ||
<p class="message-notice"><code>getOrigin</code> is added in version <code>7.6.0</code></p> | ||
|
||
The `getOrigin` method returns the URI origin used for comparison when calling the `isCrossOrigin` and `isSameOrigin` methods. | ||
The algorithm used is defined by the [WHATWG URL Living standard](https://url.spec.whatwg.org/#origin) | ||
|
||
~~~php | ||
echo Uri::new('https://uri.thephpleague.com/uri/6.0/info/')->getOrigin(); //display 'https://uri.thephpleague.com'; | ||
echo Uri::new('blob:https://mozilla.org:443')->getOrigin(); //display 'https://mozilla.org' | ||
Uri::new('file:///usr/bin/php')->getOrigin(); //returns null | ||
Uri::new('data:text/plain,Bonjour%20le%20monde%21')->getOrigin(); //returns null | ||
~~~ | ||
|
||
<p class="message-info">For absolute URI with the <code>file</code> scheme the method will return <code>null</code> (as this is left to the implementation decision)</p> | ||
Because the origin property does not exist in the RFC3986 specification this additional steps is implemented: | ||
|
||
- For non-absolute URI the method will return `null` | ||
|
||
~~~php | ||
Uri::new('/path/to/endpoint')->getOrigin(); //returns null | ||
~~~ | ||
|
||
## URI information | ||
|
||
The class also exposes a list of public methods which returns the URI state. | ||
|
||
### Uri::isAbsolute | ||
|
||
Tells whether the URI represents an absolute URI. | ||
|
||
~~~php | ||
Uri::fromServer($_SERVER)->isAbsoulte(); //returns true | ||
Uri::new("/🍣🍺")->isAbsolute(); //returns false | ||
~~~ | ||
|
||
### Uri::isAbsolutePath | ||
|
||
Tells whether the URI represents an absolute URI path. | ||
|
||
~~~php | ||
Uri::fromServer($_SERVER)->isAbsolutePath(); //returns false | ||
Uri::new("/🍣🍺")->isAbsolutePath(); //returns true | ||
~~~ | ||
|
||
### Uri::isNetworkPath | ||
|
||
Tells whether the URI represents a network path URI. | ||
|
||
~~~php | ||
Uri::new("//example.com/toto")->isNetworkPath(); //returns true | ||
Uri::new("/🍣🍺")->isNetworkPath(); //returns false | ||
~~~ | ||
|
||
### Uri::isOpaque | ||
|
||
Tells whether the given URI object represents an opaque URI. An URI is said to be | ||
opaque if and only if it is absolute but does not have an authority | ||
|
||
~~~php | ||
Uri::new("email:[email protected]?subject=🏳️🌈")->isOpaque(); //returns true | ||
Uri::new("/🍣🍺")->isOpaque(); //returns false | ||
~~~ | ||
|
||
### Uri::isRelativePath | ||
|
||
Tells whether the given URI object represents a relative path. | ||
|
||
~~~php | ||
Uri::new("🏳️🌈")->isRelativePath(); //returns true | ||
Uri::new("/🍣🍺")->isRelativePath(); //returns false | ||
~~~ | ||
|
||
### Uri::isSameDocument | ||
|
||
Tells whether the given URI object represents the same document. | ||
|
||
~~~php | ||
Uri::new("example.com?foo=bar#🏳️🌈")->isSameDocument("exAMpLE.com?foo=bar#🍣🍺"); //returns true | ||
~~~ | ||
|
||
### Uri::hasIDN | ||
|
||
Tells whether the given URI object contains a IDN host. | ||
|
||
~~~php | ||
Uri::new("https://bébé.be")->hasIdn(); //returns true | ||
~~~ | ||
|
||
### Uri::isCrossOrigin and Uri::isSameOrigin | ||
|
||
Tells whether the given URI object represents different origins. | ||
According to [RFC9110](https://www.rfc-editor.org/rfc/rfc9110#section-4.3.1) The "origin" | ||
for a given URI is the triple of scheme, host, and port after normalizing | ||
the scheme and host to lowercase and normalizing the port to remove | ||
any leading zeros. | ||
|
||
~~~php | ||
<?php | ||
Uri::new('blob:http://xn--bb-bjab.be./path') | ||
->isCrossOrigin('http://Bébé.BE./path'); // returns false | ||
|
||
Uri::new('https://example.com/123') | ||
->isSameOrigin('https://www.example.com/'); // returns false | ||
~~~ | ||
|
||
The method takes into account i18n while comparing both URI if the PHP's `idn_*` functions can be used. | ||
|
||
## Modifying URI properties | ||
|
||
Use the modifying methods exposed by all URI instances to replace one of the URI component. | ||
If the modifications do not alter the current object, it is returned as is, otherwise, | ||
|
@@ -194,9 +299,28 @@ echo Uri::new('https://uri.thephpleague.com/components/7.0/modifiers/') | |
// returns 'https://uri.thephpleague.com/default'; | ||
``` | ||
|
||
## URI resolution | ||
|
||
<p class="message-notice">Available since version <code>7.6.0</code></p> | ||
|
||
The `Uri::resolve` resolves a URI as a browser would for a relative URI while the `Uri::relativize` | ||
does the opposite. | ||
|
||
~~~php | ||
$baseUri = Uri::new('http://www.ExaMPle.com'); | ||
$uri = 'http://www.example.com/?foo=toto#~typo'; | ||
|
||
$relativeUri = $baseUri->relativize($uri); | ||
echo $relativeUri; // display "/?foo=toto#~typo | ||
echo $baseUri->resolve($relativeUri); | ||
echo $baseUri; // display 'http://www.example.com' | ||
// display 'http://www.example.com/?foo=toto#~typo' | ||
echo $baseUri->getUri()::class; //display \League\Uri\Uri | ||
~~~ | ||
|
||
## URI normalization and comparison | ||
|
||
URI normalization | ||
------- | ||
### Non destructive normalization | ||
|
||
Out of the box the package normalizes any given URI according to the non-destructive rules | ||
of [RFC3986](https://tools.ietf.org/html/rfc3986). | ||
|
@@ -217,3 +341,41 @@ echo $uri; //displays http://xn--bb-bjab.be?# | |
~~~ | ||
|
||
<p class="message-info">The last example depends on the presence of the <code>idn_to_*</code> functions, otherwise the code will trigger a <code>MissingFeature</code> exception</p> | ||
|
||
### Destructive normalization | ||
|
||
<p class="message-notice">Available since version <code>7.6.0</code></p> | ||
|
||
The `normalize` method applies extra normalization that may modifier the URI definitions, those extra rules are: | ||
|
||
- removing dot segments from the path | ||
- sorting the query pairs | ||
- normalizing the IPv6 and IPv4 host | ||
- url decode all non reserved characters in the path and the query | ||
|
||
```php | ||
echo Uri::new('eXAMPLE://a/./b/../b/%63/%7bfoo%7d')->normalize()->toString(); | ||
echo Uri::new('eXAMPLE://a/./b/../b/%63/%7bfoo%7d')->toNormalizedString(); | ||
// both calls display example://a/b/c/%7Bfoo%7D | ||
``` | ||
|
||
If you are only interested in the normalized string version of the URI you can call the `toNormalizedString` | ||
which is the equivalent to calling `toString` after calling `normalize`. | ||
|
||
### URI comparison | ||
|
||
Once normalized a URI can be compare using the two new comparison methods, `isSameDocument` and `equals` methods. | ||
|
||
The two methods uses the normalized string representation of two URI to tell whether they are referencing the | ||
same resource. | ||
|
||
```php | ||
|
||
$uri = Uri::new('example://a/b/c/%7Bfoo%7D?foo=bar'); | ||
$uri->isSameDocument('eXAMPLE://a/./b/../b/%63/%7bfoo%7d'); // returns true | ||
$uri->equals('eXAMPLE://a/./b/../b/%63/%7bfoo%7d'); // returns true | ||
$uri->equals('eXAMPLE://a/./b/../b/%63/%7bfoo%7d', excludeFragment: false); // returns false | ||
``` | ||
|
||
In the last example the `equals` method took into account the URI `fragment` component. The `isSameDocument` | ||
follow closely RFC3986 and never takes into account the URI `fragment` component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/** | ||
* League.Uri (https://uri.thephpleague.com) | ||
* | ||
* (c) Ignace Nyamagana Butera <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Uri\Contracts; | ||
|
||
interface Conditionable | ||
{ | ||
/** | ||
* Apply the callback if the given "condition" is (or resolves to) true. | ||
* | ||
* @param (callable(static): bool)|bool $condition | ||
* @param callable(static): (static|null) $onSuccess | ||
* @param ?callable(static): (static|null) $onFail | ||
*/ | ||
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): static; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.