{"id":13339960,"url":"https://github.com/rize/UriTemplate","last_synced_at":"2025-03-11T15:31:58.426Z","repository":{"id":15553448,"uuid":"18288517","full_name":"rize/UriTemplate","owner":"rize","description":"PHP URI Template (RFC 6570) supports both URI expansion \u0026 extraction","archived":false,"fork":false,"pushed_at":"2024-09-09T15:34:48.000Z","size":111,"stargazers_count":403,"open_issues_count":1,"forks_count":15,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-29T19:23:52.996Z","etag":null,"topics":["expansion","extraction","php","rfc-6570","uri-template"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rize.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"rezigned","open_collective":"rize-uri-template","custom":"https://www.paypal.me/rezigned"}},"created_at":"2014-03-31T09:42:43.000Z","updated_at":"2024-10-29T14:30:37.000Z","dependencies_parsed_at":"2024-03-17T05:42:38.762Z","dependency_job_id":"f347bc2e-da76-4afb-9b2f-0d489a1e66d5","html_url":"https://github.com/rize/UriTemplate","commit_stats":{"total_commits":61,"total_committers":16,"mean_commits":3.8125,"dds":0.7049180327868853,"last_synced_commit":"b36fe7dafc28c8bfd45ba4149ef60c59041b1e30"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rize%2FUriTemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rize%2FUriTemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rize%2FUriTemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rize%2FUriTemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rize","download_url":"https://codeload.github.com/rize/UriTemplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242764701,"owners_count":20181615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expansion","extraction","php","rfc-6570","uri-template"],"created_at":"2024-07-29T19:21:23.124Z","updated_at":"2025-03-11T15:31:55.897Z","avatar_url":"https://github.com/rize.png","language":"PHP","funding_links":["https://github.com/sponsors/rezigned","https://opencollective.com/rize-uri-template","https://www.paypal.me/rezigned","https://opencollective.com/rize-uri-template/contribute","https://opencollective.com/rize-uri-template/organization/0/website","https://opencollective.com/rize-uri-template/organization/1/website","https://opencollective.com/rize-uri-template/organization/2/website","https://opencollective.com/rize-uri-template/organization/3/website","https://opencollective.com/rize-uri-template/organization/4/website","https://opencollective.com/rize-uri-template/organization/5/website","https://opencollective.com/rize-uri-template/organization/6/website","https://opencollective.com/rize-uri-template/organization/7/website","https://opencollective.com/rize-uri-template/organization/8/website","https://opencollective.com/rize-uri-template/organization/9/website"],"categories":[],"sub_categories":[],"readme":"# PHP URI Template\n\nThis is a URI Template implementation in PHP based on [RFC 6570 URI Template](http://tools.ietf.org/html/rfc6570). In addition to URI expansion, it also supports URI extraction (used by [Google Cloud Core](https://github.com/googleapis/google-cloud-php-core) and [Google Cloud Client Library](https://github.com/googleapis/google-cloud-php)).\n\n![CI](https://github.com/rize/UriTemplate/workflows/CI/badge.svg) [![Total Downloads](https://poser.pugx.org/rize/uri-template/downloads)](https://packagist.org/packages/rize/uri-template) [![Latest Stable Version](https://poser.pugx.org/rize/uri-template/v)](https://packagist.org/packages/rize/uri-template) [![PHP Version Require](https://poser.pugx.org/rize/uri-template/require/php)](https://packagist.org/packages/rize/uri-template)\n\n\u003e [!NOTE]\n\u003e\n\u003e Due to the deprecation of implictly nullable parameter types in [PHP 8.4](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+.\n\u003e\n\u003e As a result, version [0.4.0](https://github.com/rize/UriTemplate/releases/tag/0.4.0) and later will no longer support PHP versions below 8.1.\n\n## Usage\n\n### Expansion\n\nA very simple usage (string expansion).\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate();\n$uri-\u003eexpand('/{username}/profile', ['username' =\u003e 'john']);\n\n\u003e\u003e '/john/profile'\n```\n\n`Rize\\UriTemplate` supports all `Expression Types` and `Levels` specified by RFC6570.\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate();\n$uri-\u003eexpand('/search/{term:1}/{term}/{?q*,limit}', [\n    'term'  =\u003e 'john',\n    'q'     =\u003e ['a', 'b'],\n    'limit' =\u003e 10,\n])\n\n\u003e\u003e '/search/j/john/?q=a\u0026q=b\u0026limit=10'\n```\n\n#### `/` Path segment expansion\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate();\n$uri-\u003eexpand('http://{host}{/segments*}/{file}{.extensions*}', [\n    'host'       =\u003e 'www.host.com',\n    'segments'   =\u003e ['path', 'to', 'a'],\n    'file'       =\u003e 'file',\n    'extensions' =\u003e ['x', 'y'],\n]);\n\n\u003e\u003e 'http://www.host.com/path/to/a/file.x.y'\n```\n\n`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.\n\nTake a look at real world example.\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate('https://api.twitter.com/{version}', ['version' =\u003e 1.1]);\n$uri-\u003eexpand('/statuses/show/{id}.json', ['id' =\u003e '210462857140252672']);\n\n\u003e\u003e https://api.twitter.com/1.1/statuses/show/210462857140252672.json\n```\n\n### Extraction\n\nIt also supports URI Extraction (extract all variables from URI). Let's take a look at the example.\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate('https://api.twitter.com/{version}', ['version' =\u003e 1.1]);\n\n$params = $uri-\u003eextract('/search/{term:1}/{term}/{?q*,limit}', '/search/j/john/?q=a\u0026q=b\u0026limit=10');\n\n\u003e\u003e print_r($params);\n(\n    [term:1] =\u003e j\n    [term] =\u003e john\n    [q] =\u003e Array\n        (\n            [0] =\u003e a\n            [1] =\u003e b\n        )\n\n    [limit] =\u003e 10\n)\n```\n\nNote 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.\n\n#### `strict` mode\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate();\n$uri-\u003eextract($template, $uri, $strict = false)\n```\n\nNormally `extract` method will try to extract vars from a uri even if it's partially matched. For example\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate();\n$params = $uri-\u003eextract('/{?a,b}', '/?a=1')\n\n\u003e\u003e print_r($params);\n(\n    [a] =\u003e 1\n    [b] =\u003e null\n)\n```\n\nWith `strict mode`, it will allow you to extract uri only when variables in template are fully matched with given uri.\n\nWhich 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).\n\n```php\n\u003c?php\n\nuse Rize\\UriTemplate;\n\n$uri = new UriTemplate();\n\n// Note that variable `b` is absent in uri\n$params = $uri-\u003eextract('/{?a,b}', '/?a=1', true);\n\n\u003e\u003e\u003e null\n\n// Now we give `b` some value\n$params = $uri-\u003eextract('/{?a,b}', '/?a=1\u0026b=2', true);\n\n\u003e\u003e\u003e print_r($params)\n(\n  [a] =\u003e 1\n  [b] =\u003e 2\n)\n```\n\n#### Array modifier `%`\n\nBy 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`.\n\nExample usage for `%` modifier\n\n```php\n\u003c?php\n\n$uri-\u003eexpand('{?list%,keys%}', [\n    'list' =\u003e [\n        'a', 'b',\n    ),\n    'keys' =\u003e [\n        'a' =\u003e 1,\n        'b' =\u003e 2,\n    ),\n]);\n\n// '?list[]=a\u0026list[]=b\u0026keys[a]=1\u0026keys[b]=2'\n\u003e\u003e '?list%5B%5D=a\u0026list%5B%5D=b\u0026keys%5Ba%5D=1\u0026keys%5Bb%5D=2'\n\n// [] get encoded to %5B%5D i.e. '?list[]=a\u0026list[]=b\u0026keys[a]=1\u0026keys[b]=2'\n$params = $uri-\u003eextract('{?list%,keys%}', '?list%5B%5D=a\u0026list%5B%5D=b\u0026keys%5Ba%5D=1\u0026keys%5Bb%5D=2', )\n\n\u003e\u003e print_r($params);\n(\n    [list] =\u003e Array\n        (\n            [0] =\u003e a\n            [1] =\u003e b\n        )\n\n    [keys] =\u003e Array\n        (\n            [a] =\u003e 1\n            [b] =\u003e 2\n        )\n)\n```\n\n## Installation\n\nUsing `composer`\n\n```\n{\n    \"require\": {\n        \"rize/uri-template\": \"~0.3\"\n    }\n}\n```\n\n### Changelogs\n\n* **0.2.0** Add a new modifier `%` which allows user to use `list[]=a\u0026list[]=b` query pattern.\n* **0.2.1** Add nested array support for `%` modifier\n* **0.2.5** Add strict mode support for `extract` method\n* **0.3.0** Improve code quality + RFC3986 support for `extract` method by @Maks3w\n* **0.3.1** Improve `extract` method to parse two or more adjacent variables separated by dot by @corleonis\n* **0.4.0** Fixes the deprecation of implicitly nullable parameter types introduced in PHP 8.4. This version requires PHP 8.1 or later.\n\n## Contributors\n\n### Code Contributors\n\nThis project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].\n\u003ca href=\"https://github.com/rize/UriTemplate/graphs/contributors\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/contributors.svg?width=890\u0026button=false\" /\u003e\u003c/a\u003e\n\n### Financial Contributors\n\nBecome a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/rize-uri-template/contribute)]\n\n#### Individuals\n\n\u003ca href=\"https://opencollective.com/rize-uri-template\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/individuals.svg?width=890\"\u003e\u003c/a\u003e\n\n#### Organizations\n\nSupport 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)]\n\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/0/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/0/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/1/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/1/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/2/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/2/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/3/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/3/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/4/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/4/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/5/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/5/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/6/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/6/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/7/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/7/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/8/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/8/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/rize-uri-template/organization/9/website\"\u003e\u003cimg src=\"https://opencollective.com/rize-uri-template/organization/9/avatar.svg\"\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frize%2FUriTemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frize%2FUriTemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frize%2FUriTemplate/lists"}