madkom/uri

统一资源标识符(URI)和统一资源定位符(URL)

1.0.5 2016-10-07 09:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:27 UTC


README

此库实现了基于 RFC3986RFC6570 的 URI 和 URL 规范以及 URI 模板(依赖外部库)。

PHP 7.0 Build Status Latest Stable Version Total Downloads License Coverage Status Code Climate Issue Count

功能

此库可以创建如下的对象

  • Uri RFC3986 - 包括广泛的抽象:方案权限路径查询片段
  • UriReference RFC3986 - 可以通过有效的 Uri 进行解析(例如:`$resolvedUri = $uriReference->resolve($uri);`)
  • UriTemplate RFC6570 - 生成 UriUriReference 对象(依赖于模板)

安装

使用 Composer 安装

composer require madkom/uri

要求

此库需要 PHP 版本为 ~7。目前,此库依赖于另一个自有的库 madkom/collection 以及一些外部包

  1. rize/uri-template:用于 UriTemplate 实现
  2. true/punycode:用于 IDNA 域名转换

使用方法

解析 URL 字符串

use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;

$factory = new UriFactory();

/** @var Uri $uri */
$uri = $factory->createUri('http://user:pass@host.tld/some/path?and=query&param=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

待办事项 还未实现片段。

解析和解析 UriReference

use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;
use Madkom\Uri\UriReference;

$factory = new UriFactory();

/** @var Uri $uri */
$uri = $factory->createUri('http://user:pass@host.tld/some/path?and=query&param=2#fragment');

/** @var UriReference $uriReference */
$uriReference = $factory->createUriReference('../another/path?and=different');
(string)$uriReference->resolveUri($uri); // http://user:pass@host.tld/some/another/path?and=different

解析 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

创建 URI 对象

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

待办事项

  • 实现 Uri 到字符串的转换
  • 实现片段组件
  • \Madkom\Uri\Parser 中用 RFC 正则表达式替换 IRI 库
  • \Madkom\Uri\Parser\Query 中实现针对不同语言的附加解析模式(参数重复问题)
  • 实现规范化
  • 基于 RFC3986 实现 UriReference

许可证

MIT 许可证 (MIT)

版权所有 © 2016 Madkom S.A.

特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得软件的人进行此类行为,但以下条件除外

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论该索赔、损害或其他责任是由于合同、侵权或其他方式引起的,并且与软件或软件的使用或任何其他相关行为有关。