| 1 |
# PHP URI Template |
| 2 |
|
| 3 |
This is a URI Template implementation in PHP based on [](http://tools.ietf.org/html/rfc6570RFC 6570 URI Template](http://tools.ietf.org/html/rfc6570](http://tools.ietf.org/html/rfc6570). In addition to URI expansion, it also supports URI extraction (used by [](https://github.com/googleapis/google-cloud-php-coreGoogle Cloud Core](https://github.com/googleapis/google-cloud-php-core](https://github.com/googleapis/google-cloud-php-core) and [](https://github.com/googleapis/google-cloud-phpGoogle Cloud Client Library](https://github.com/googleapis/google-cloud-php](https://github.com/googleapis/google-cloud-php)). |
| 4 |
|
| 5 |
 [](https://packagist.org/packages/rize/uri-template](https://packagist.org/packages/rize/uri-template](https://packagist.org/packages/rize/uri-template) [](https://packagist.org/packages/rize/uri-template](https://packagist.org/packages/rize/uri-template](https://packagist.org/packages/rize/uri-template) [](https://packagist.org/packages/rize/uri-template](https://packagist.org/packages/rize/uri-template](https://packagist.org/packages/rize/uri-template) |
| 6 |
|
| 7 |
> [!NOTE] |
| 8 |
> |
| 9 |
> Due to the deprecation of implictly nullable parameter types in [](https://wiki.php.net/rfc/deprecate-implicitly-nullable-typesPHP 8.4](https://wiki.php.net/rfc/deprecate-implicitly-nullable-types](https://wiki.php.net/rfc/deprecate-implicitly-nullable-types), we must introduce breaking change by adding explicit nullable types (`?T`) which requires PHP 7.1+. |
| 10 |
> |
| 11 |
> As a result, version [](https://github.com/rize/UriTemplate/releases/tag/0.4.00.4.0](https://github.com/rize/UriTemplate/releases/tag/0.4.0](https://github.com/rize/UriTemplate/releases/tag/0.4.0) and later will no longer support PHP versions below 8.1. |
| 12 |
|
| 13 |
## Usage |
| 14 |
|
| 15 |
### Expansion |
| 16 |
|
| 17 |
A very simple usage (string expansion). |
| 18 |
|
| 19 |
```php |
| 20 |
<?php |
| 21 |
|
| 22 |
use Rize\UriTemplate; |
| 23 |
|
| 24 |
$uri = new UriTemplate(); |
| 25 |
$uri->expand('/{username}/profile', ['username' => 'john']); |
| 26 |
|
| 27 |
>> '/john/profile' |
| 28 |
``` |
| 29 |
|
| 30 |
`Rize\UriTemplate` supports all `Expression Types` and `Levels` specified by RFC6570. |
| 31 |
|
| 32 |
```php |
| 33 |
<?php |
| 34 |
|
| 35 |
use Rize\UriTemplate; |
| 36 |
|
| 37 |
$uri = new UriTemplate(); |
| 38 |
$uri->expand('/search/{term:1}/{term}/{?q*,limit}', [ |
| 39 |
'term' => 'john', |
| 40 |
'q' => ['a', 'b'], |
| 41 |
'limit' => 10, |
| 42 |
]) |
| 43 |
|
| 44 |
>> '/search/j/john/?q=a&q=b&limit=10' |
| 45 |
``` |
| 46 |
|
| 47 |
#### `/` Path segment expansion |
| 48 |
|
| 49 |
```php |
| 50 |
<?php |
| 51 |
|
| 52 |
use Rize\UriTemplate; |
| 53 |
|
| 54 |
$uri = new UriTemplate(); |
| 55 |
$uri->expand('http://{host}{/segments*}/{file}{.extensions*}', [ |
| 56 |
'host' => 'www.host.com', |
| 57 |
'segments' => ['path', 'to', 'a'], |
| 58 |
'file' => 'file', |
| 59 |
'extensions' => ['x', 'y'], |
| 60 |
]); |
| 61 |
|
| 62 |
>> 'http://www.host.com/path/to/a/file.x.y' |
| 63 |
``` |
| 64 |
|
| 65 |
`Rize\UriTemplate` accepts `base-uri` as a 1st argument and `default params` as a 2nd argument. This is very useful when you're working with API endpoint. |
| 66 |
|
| 67 |
Take a look at real world example. |
| 68 |
|
| 69 |
```php |
| 70 |
<?php |
| 71 |
|
| 72 |
use Rize\UriTemplate; |
| 73 |
|
| 74 |
$uri = new UriTemplate('https://api.twitter.com/{version}', ['version' => 1.1]); |
| 75 |
$uri->expand('/statuses/show/{id}.json', ['id' => '210462857140252672']); |
| 76 |
|
| 77 |
>> https://api.twitter.com/1.1/statuses/show/210462857140252672.json |
| 78 |
``` |
| 79 |
|
| 80 |
### Extraction |
| 81 |
|
| 82 |
It also supports URI Extraction (extract all variables from URI). Let's take a look at the example. |
| 83 |
|
| 84 |
```php |
| 85 |
<?php |
| 86 |
|
| 87 |
use Rize\UriTemplate; |
| 88 |
|
| 89 |
$uri = new UriTemplate('https://api.twitter.com/{version}', ['version' => 1.1]); |
| 90 |
|
| 91 |
$params = $uri->extract('/search/{term:1}/{term}/{?q*,limit}', '/search/j/john/?q=a&q=b&limit=10'); |
| 92 |
|
| 93 |
>> print_r($params); |
| 94 |
( |
| 95 |
[term:1] => j |
| 96 |
[term] => john |
| 97 |
[q] => Array |
| 98 |
( |
| 99 |
[0] => a |
| 100 |
[1] => b |
| 101 |
) |
| 102 |
|
| 103 |
[limit] => 10 |
| 104 |
) |
| 105 |
``` |
| 106 |
|
| 107 |
Note that in the example above, result returned by `extract` method has an extra keys named `term:1` for `prefix` modifier. This key was added just for our convenience to access prefix data. |
| 108 |
|
| 109 |
#### `strict` mode |
| 110 |
|
| 111 |
```php |
| 112 |
<?php |
| 113 |
|
| 114 |
use Rize\UriTemplate; |
| 115 |
|
| 116 |
$uri = new UriTemplate(); |
| 117 |
$uri->extract($template, $uri, $strict = false) |
| 118 |
``` |
| 119 |
|
| 120 |
Normally `extract` method will try to extract vars from a uri even if it's partially matched. For example |
| 121 |
|
| 122 |
```php |
| 123 |
<?php |
| 124 |
|
| 125 |
use Rize\UriTemplate; |
| 126 |
|
| 127 |
$uri = new UriTemplate(); |
| 128 |
$params = $uri->extract('/{?a,b}', '/?a=1') |
| 129 |
|
| 130 |
>> print_r($params); |
| 131 |
( |
| 132 |
[a] => 1 |
| 133 |
[b] => null |
| 134 |
) |
| 135 |
``` |
| 136 |
|
| 137 |
With `strict mode`, it will allow you to extract uri only when variables in template are fully matched with given uri. |
| 138 |
|
| 139 |
Which is useful when you want to determine whether the given uri is matched against your template or not (in case you want to use it as routing service). |
| 140 |
|
| 141 |
```php |
| 142 |
<?php |
| 143 |
|
| 144 |
use Rize\UriTemplate; |
| 145 |
|
| 146 |
$uri = new UriTemplate(); |
| 147 |
|
| 148 |
// Note that variable `b` is absent in uri |
| 149 |
$params = $uri->extract('/{?a,b}', '/?a=1', true); |
| 150 |
|
| 151 |
>>> null |
| 152 |
|
| 153 |
// Now we give `b` some value |
| 154 |
$params = $uri->extract('/{?a,b}', '/?a=1&b=2', true); |
| 155 |
|
| 156 |
>>> print_r($params) |
| 157 |
( |
| 158 |
[a] => 1 |
| 159 |
[b] => 2 |
| 160 |
) |
| 161 |
``` |
| 162 |
|
| 163 |
#### Array modifier `%` |
| 164 |
|
| 165 |
By default, RFC 6570 only has 2 types of operators `:` and `*`. This `%` array operator was added to the library because current spec can't handle array style query e.g. `list[]=a` or `key[user]=john`. |
| 166 |
|
| 167 |
Example usage for `%` modifier |
| 168 |
|
| 169 |
```php |
| 170 |
<?php |
| 171 |
|
| 172 |
$uri->expand('{?list%,keys%}', [ |
| 173 |
'list' => [ |
| 174 |
'a', 'b', |
| 175 |
), |
| 176 |
'keys' => [ |
| 177 |
'a' => 1, |
| 178 |
'b' => 2, |
| 179 |
), |
| 180 |
]); |
| 181 |
|
| 182 |
// '?list[]=a&list[]=b&keys[a]=1&keys[b]=2' |
| 183 |
>> '?list%5B%5D=a&list%5B%5D=b&keys%5Ba%5D=1&keys%5Bb%5D=2' |
| 184 |
|
| 185 |
// [] get encoded to %5B%5D i.e. '?list[]=a&list[]=b&keys[a]=1&keys[b]=2' |
| 186 |
$params = $uri->extract('{?list%,keys%}', '?list%5B%5D=a&list%5B%5D=b&keys%5Ba%5D=1&keys%5Bb%5D=2', ) |
| 187 |
|
| 188 |
>> print_r($params); |
| 189 |
( |
| 190 |
[list] => Array |
| 191 |
( |
| 192 |
[0] => a |
| 193 |
[1] => b |
| 194 |
) |
| 195 |
|
| 196 |
[keys] => Array |
| 197 |
( |
| 198 |
[a] => 1 |
| 199 |
[b] => 2 |
| 200 |
) |
| 201 |
) |
| 202 |
``` |
| 203 |
|
| 204 |
## Installation |
| 205 |
|
| 206 |
Using `composer` |
| 207 |
|
| 208 |
``` |
| 209 |
{ |
| 210 |
"require": { |
| 211 |
"rize/uri-template": "~0.3" |
| 212 |
} |
| 213 |
} |
| 214 |
``` |
| 215 |
|
| 216 |
### Changelogs |
| 217 |
|
| 218 |
* **0.2.0** Add a new modifier `%` which allows user to use `list[]=a&list[]=b` query pattern. |
| 219 |
* **0.2.1** Add nested array support for `%` modifier |
| 220 |
* **0.2.5** Add strict mode support for `extract` method |
| 221 |
* **0.3.0** Improve code quality + RFC3986 support for `extract` method by @Maks3w |
| 222 |
* **0.3.1** Improve `extract` method to parse two or more adjacent variables separated by dot by @corleonis |
| 223 |
* **0.4.0** Fixes the deprecation of implicitly nullable parameter types introduced in PHP 8.4. This version requires PHP 8.1 or later. |
| 224 |
|
| 225 |
## Contributors |
| 226 |
|
| 227 |
### Code Contributors |
| 228 |
|
| 229 |
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)]. |
| 230 |
<a href="https://github.com/rize/UriTemplate/graphs/contributors"><img src="https://opencollective.com/rize-uri-template/contributors.svg?width=890&button=false" /></a> |
| 231 |
|
| 232 |
### Financial Contributors |
| 233 |
|
| 234 |
Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/rize-uri-template/contribute)] |
| 235 |
|
| 236 |
#### Individuals |
| 237 |
|
| 238 |
<a href="https://opencollective.com/rize-uri-template"><img src="https://opencollective.com/rize-uri-template/individuals.svg?width=890"></a> |
| 239 |
|
| 240 |
#### Organizations |
| 241 |
|
| 242 |
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/rize-uri-template/contribute)] |
| 243 |
|
| 244 |
<a href="https://opencollective.com/rize-uri-template/organization/0/website"><img src="https://opencollective.com/rize-uri-template/organization/0/avatar.svg"></a> |
| 245 |
<a href="https://opencollective.com/rize-uri-template/organization/1/website"><img src="https://opencollective.com/rize-uri-template/organization/1/avatar.svg"></a> |
| 246 |
<a href="https://opencollective.com/rize-uri-template/organization/2/website"><img src="https://opencollective.com/rize-uri-template/organization/2/avatar.svg"></a> |
| 247 |
<a href="https://opencollective.com/rize-uri-template/organization/3/website"><img src="https://opencollective.com/rize-uri-template/organization/3/avatar.svg"></a> |
| 248 |
<a href="https://opencollective.com/rize-uri-template/organization/4/website"><img src="https://opencollective.com/rize-uri-template/organization/4/avatar.svg"></a> |
| 249 |
<a href="https://opencollective.com/rize-uri-template/organization/5/website"><img src="https://opencollective.com/rize-uri-template/organization/5/avatar.svg"></a> |
| 250 |
<a href="https://opencollective.com/rize-uri-template/organization/6/website"><img src="https://opencollective.com/rize-uri-template/organization/6/avatar.svg"></a> |
| 251 |
<a href="https://opencollective.com/rize-uri-template/organization/7/website"><img src="https://opencollective.com/rize-uri-template/organization/7/avatar.svg"></a> |
| 252 |
<a href="https://opencollective.com/rize-uri-template/organization/8/website"><img src="https://opencollective.com/rize-uri-template/organization/8/avatar.svg"></a> |
| 253 |
<a href="https://opencollective.com/rize-uri-template/organization/9/website"><img src="https://opencollective.com/rize-uri-template/organization/9/avatar.svg"></a> |
| 254 |
|