phrity/net-uri
PSR-7 Uri and PSR-17 UriFactory implementation
Maintainers
Requires
- php: ^8.1
- ext-mbstring: *
- phrity/comparison: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 | ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.0 || ^11.0 || ^12.0
- phrity/util-errorhandler: ^1.1
- robiningelbrecht/phpunit-coverage-tools: ^1.9
- squizlabs/php_codesniffer: ^3.5 || ^4.0
Suggests
- ext-intl: Enables IDN conversion for non-ASCII domains
Provides
None
Conflicts
None
Replaces
None
MIT 0737de026b75177ae302ac9fdbbd0ffc2610f3b8
- SΓΆren Jensen <sirn.woop@sirn.se>
README
π Build Status
π Coverage Status
Introduction
Implementation of the PSR-7 UriInterface and PSR-17 UriFactoryInterface interfaces.
Nothing fancy. Just working. Because I need a URI implementation not hardwired to HTTP messaging. And some extras. Allow all valid schemes.
Installation
Install with Composer;
composer require phrity/net-uri
Uri class methods
Implemts PSR-7 UriInterface and provides some extra metods and options. More info here.
use Phrity\Net\Uri; $uri = new Uri('http://example.com/path/to/file.html?query1=1#fragment'); // PSR-7 getters $uri->getScheme(); $uri->getHost(); $uri->getPort(); $uri->getPath(); $uri->getQuery(); $uri->getFragment(); $uri->getAuthority(); $uri->getUserInfo(); // PSR-7 setters $uri->withScheme('https'); $uri->withHost('example2.com'); $uri->withPort(8080); $uri->withPath('/path/to/another/file.html'); $uri->withQuery('query2=2'); $uri->withFragment('another-fragment'); $uri->withUserInfo('username', 'password'); // Equalable implementation $uri->equals('http://example.com/path/to/file.html'); // Stringable implementation $uri->__toString(); // JsonSerializable implementation $uri->jsonSerialize(); // Additional methods $uri->toString(); $uri->getQueryItems(); $uri->getQueryItem('query1'); $uri->withQueryItems(['query1' => '1', 'query2' => '2']); $uri->withQueryItem('query1', '1'); $uri->getComponents(); $uri->withComponents(['scheme' => 'https', 'host' => 'example2.com']);
UriFactory class methods
Implemts PSR-17 UriFactoryInterface and provides some extra metods and options. More info here.
use Phrity\Net\UriFactory; $factory = new UriFactory(); $factory->createUri('http://example.com/path/to/file.html'); $factory->createUriFromInterface(new GuzzleHttp\Psr7\Uri('http://example.com/path/to/file.html'));
Modifiers
Out of the box, it will behave as specified by PSR standards.
To change behaviour, there are some modifiers available.
These can be added as last argument in all get and with methods, plus the toString method.
REQUIRE_PORT- Attempt to show port, even if defaultABSOLUTE_PATH- Will cause path to use absolute form, i.e. starting with/NORMALIZE_PATH- Will attempt to normalize pathIDN_ENCODE/IDN_DECODE- Encode or decode IDN-format for non-ASCII hostURI_DECODE/URI_ENCODE/URI_ENCODE_3986- Encode or decode URI components
Examples
$uri = new Uri('http://example.com'); $uri->getPort(Uri::REQUIRE_PORT); // => 80 $uri->toString(Uri::REQUIRE_PORT); // => 'http://example.com:80' $uri = new Uri('a/./path/../to//something'); $uri->getPath(Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH); // => '/a/to/something' $uri->toString(Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH); // => '/a/to/something' $clone = $uri->withPath('path/./somewhere/else/..', Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH); $clone->getPath(); // => '/path/somewhere' $uri = new Uri('https://Ξ·ΓΓΆΓΈεΏ Πγ.com'); $uri->getHost(Uri::IDN_ENCODE); // => 'xn--zca0cg32z7rau82strvd.com'
Versions
| Version | PHP | |
|---|---|---|
2.2 |
^8.1 |
Equable implementation |
2.1 |
^8.0 |
URI encode/decode options |
2.0 |
^8.0 |
Query helpers, with([]) and getComponents() methods, IDN encode/decode |
1.3 |
^7.4|^8.0 |
|
1.2 |
^7.4|^8.0 |
IDNA modifier |
1.1 |
^7.4|^8.0 |
Require port, Absolute path, Normalize path modifiers |
1.0 |
^7.4|^8.0 |
Initial version |
