This library implements URI and URL specification and URI Templates, based on RFC3986 and RFC6570 (depends on external lib).
This library can create objects like:
- Uri RFC3986 - includes wide abstraction with: Scheme, Authority, Path, Query, Fragment
- UriReference RFC3986 - can be resolved with valid Uri (eg.
$resolvedUri = $uriReference->resolve($uri);
) - UriTemplate RFC6570 - produces Uri or UriReference objects (depends on template)
Install with Composer
composer require madkom/uri
This library requires PHP in ~7
version.
At this point this library depends on another own library madkom/collection and some external packages:
- rize/uri-template: for UriTemplate impl
- true/punycode: for IDNA domain names conversion
Parsing url string:
use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;
$factory = new UriFactory();
/** @var Uri $uri */
$uri = $factory->createUri('http://user:[email protected]/some/path?and=query¶m=2#fragment');
$uri->getScheme(); // Instance of \Madkom\Uri\Scheme\Http
$uri->getAuthority(); // Instance of \Madkom\Uri\Authority
$uri->getPath(); // Instance of \Madkom\Uri\Path
$uri->getQuery(); // Instance of \Madkom\Uri\Query
TODO Missing fragment impl yet.
Parsing and resolving UriReference:
use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;
use Madkom\Uri\UriReference;
$factory = new UriFactory();
/** @var Uri $uri */
$uri = $factory->createUri('http://user:[email protected]/some/path?and=query¶m=2#fragment');
/** @var UriReference $uriReference */
$uriReference = $factory->createUriReference('../another/path?and=different');
(string)$uriReference->resolveUri($uri); // http://user:[email protected]/some/another/path?and=different
Parsing isbn uri:
use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;
$factory = new UriFactory();
/** @var Uri $uri */
$uri = $factory->createUri('isbn:978-83-283-0525-0'); // Instance of \Madkom\Uri\Uri
$uri->getScheme(); // Instance of \Madkom\Uri\Scheme\Custom
$uri->getAuthority(); // NULL
$uri->getPath(); // Instance of \Madkom\Uri\Path with "978-83-283-0525-0"
$uri->getQuery(); // Instance of \Madkom\Uri\Query which is empty
Creating URI object:
use Madkom\Uri\Uri;
use Madkom\Uri\Scheme\Https;
use Madkom\Uri\Authority;
use Madkom\Uri\Authority\Host\IPv6;
use Madkom\Uri\Authority\UserInfo;
use Madkom\Uri\Path;
use Madkom\Uri\Query;
use Madkom\Uri\Query\Parameter;
/** @var Uri $uri */
$uri = new Uri(
new Https(),
new Authority(
new IPv6('::1'),
443,
new UserInfo('user', 'pass')
),
new Path([
'some',
'path'
]),
new Query([
new Parameter('name', 'value')
])
);
$uri->toString(); // https://user:pass@[::1]:443/some/path?name=value
(string)$uri; // same as above
- Implement Uri to string conversion
- Implement fragment component
- Replace IRI library with RFC Regex in
\Madkom\Uri\Parser
- Implement additional parsing modes in
\Madkom\Uri\Parser\Query
for various languages (parameter duplicate problem) - Implement normalization
- Implement UriReference based on RFC3986
The MIT License (MIT)
Copyright (c) 2016 Madkom S.A.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.