RFC6570 implementation.
composer require innmind/url-template
use Innmind\UrlTemplate\Template;
use Innmind\Immutable\Map;
use Innmind\Url\Url;
$url = Template::of('http://example.com/dictionary/{term:1}/{term}')->expand(
Map::of(['term', 'dog']),
);
$url instanceof Url; // true
$url->toString(); // http://example.com/dictionary/d/dog
$variables = Template::of('http://example.com/dictionary/{term:1}/{term}')->extract(
Url::of('http://example.com/dictionary/d/dog')
);
$variables; // Map<string, string>
$variables->size(); // 1
$variables->get('term'); // dog
Important: variable extraction is not supported for list (ie {foo*}
expression).