fm-labs/php-uri

PHP 简单 URI 库

0.6.1 2021-01-01 14:46 UTC

This package is auto-updated.

Last update: 2024-09-29 05:35:43 UTC


README

PHP 简单 URI 库。符合 PSR-7 UriInterface 规范。包含符合 RFC3968 的 URI 规范化器。支持 PHP 7.1+ 和 PHP 8.0。

Build Status

需求

  • php7.1+ | php8.0

安装

$ composer require fm-labs/php-uri

Uri

// > Create new Uri
$uri = \FmLabs\Uri\UriFactory::create();
$uri = $uri
    ->withScheme('https')
    ->withHost('example.org')
    ->withPort(8080)
    ->withPath('/my/path')
    ->withQuery('foo=bar&hello=world')
    ->withFragment('top')
    ->withUserInfo('user', 's3cret');

echo (string)$uri;
// https://user:s3cret@example.org:8080/my/path?foo=bar&hello=world#top
// > Create Uri from string
$uri = \FmLabs\Uri\UriFactory::fromString('http://user:s3cret@www.example.org/test?q=hello#world');

// PSR-7 interface methods
$schema = $uri->getScheme(); // "http"
$host = $uri->getHost(); // "www.example.org"
$path = $uri->getPath(); // "/test"
$frag = $uri->getFragment(); // "world"
$userinfo = $uri->getUserInfo(); // "user:s3cret"
$authority = $uri->getAuthority(); // "user:s3cret@www.example.org"

// Convenience methods
$user = $uri->getUser(); // "user"
$pass = $uri->getUserPass(); // "s3cret"
$queryData = $uri->getQueryData(); // ['q' => 'hello']
$queryData = $uri->getQueryData('q'); // 'hello'

// Array access (read-only)
$host = $uri['host'];

// Property access (read-only)
$host = $uri->host;

PSR-7 UriInterface 方法

  • getScheme()

  • getHost()

  • getPort()

  • getPath()

  • getQuery()

  • getFragment()

  • getUserInfo()

  • getAuthority()

  • __toString()

  • withScheme()

  • withHost()

  • withPort()

  • withUserInfo()

  • withPath()

  • withQuery()

  • withFragment()

便利方法

  • getHostInfo() - 返回主机信息字符串
  • getUser() - 返回用户信息用户名
  • getUserPass() - 返回用户信息密码
  • getQueryData(?string $key = null) - 返回查询数据数组 OR 特定键的值
  • getComponents() - 返回组件数组

数组和属性访问

可用键: scheme, host, port, path, query, fragment, user, pass, userinfo, authority, hostinfo, querydata

/** @var \FmLabs\Uri\Uri $uri **/

// Array access
$uri['KEY_NAME'];

// Property access
$uri->KEY_NAME;

UriFactory

使用 UriFactory 类创建 Uri 对象

UriFactory::create()

// Examples
$uri = \FmLabs\Uri\UriFactory::create();
$uri = $uri
    ->withScheme('https')
    ->withHost('example.org');

UriFactory::fromString(string $uriString)

// Examples
\FmLabs\Uri\UriFactory::fromString('http://www.example.org');
\FmLabs\Uri\UriFactory::fromString('https://user:secret@www.example.org/my/path?some=query#frag');
\FmLabs\Uri\UriFactory::fromString('https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top');
\FmLabs\Uri\UriFactory::fromString('mailto:John.Doe@example.com');
\FmLabs\Uri\UriFactory::fromString('tel:+1-816-555-1212');
\FmLabs\Uri\UriFactory::fromString('ldap://[2001:db8::7]/c=GB?objectClass?one');
\FmLabs\Uri\UriFactory::fromString('urn:oasis:names:specification:docbook:dtd:xml:4.1.2');

UriFactory::fromComponents(array $components)

// Examples
// http://www.example.org
\FmLabs\Uri\UriFactory::fromComponents(['scheme' => 'http', 'host' => 'www.example.org']);
// tel:+123456789
\FmLabs\Uri\UriFactory::fromComponents(['scheme' => 'tel', 'path' => '+123456789']);

UriFactory::fromUri(UriInterface $uri)

从任何 UriInterface 创建 \FmLabs\Uri\Uri 对象。

/** @var \Psr\Http\Message\UriInterface $anyObjectThatImplementsUriInterface */
$uri = \FmLabs\Uri\UriFactory::fromUri($anyObjectThatImplementsUriInterface);

UriNormalizer

static normalize()

规范化 URI。参见 RFC3968此处

返回一个新的 Uri 对象,具有规范化后的组件。

$uri = \FmLabs\Uri\UriFactory::fromString('hTTp://www.eXample.org:80/test/./../foo/../bar');
$normalized = \FmLabs\Uri\UriNormalizer::normalize($uri);
// http://www.example.org/test/foo/bar;
$uri = \FmLabs\Uri\UriFactory::fromString('hTTp://www.eXample.org:80/test/./../foo/../bar');
$normalizer = new \FmLabs\Uri\UriNormalizer($uri);
$uri = $normalizer
    // these normalizations preserve semantics of uri
    ->normalizeScheme()
    ->normalizeHost()
    ->normalizeDotSegements()
    ->normalizeTrailingSlash()
    ->normalizeUnreservedChars()
    ->normalizeEscapeSequences()
    ->normalizeDefaultPorts()
    // these normalizations change semantics of uri
    ->normalizeForceHttps()
    ->normalizeHostIp()
    ->normalizeWwwDomain()
    ->normalizeNonEmptyPath()
    ->normalizeDirectoryIndex()
    ->normalizeDuplicateSlashes()
    ->normalizeFragment()
    ->normalizeQuerySorting()
    ->normalizeEmptyQuery()
    ->getUri();

$normalizedUri = \FmLabs\Uri\UriNormalizer::normalize($uri, ['preserve' => false]);
// http://www.example.org/test/foo/bar;

用法

运行测试

$ composer run-script test
// or
$ composer run-script test-verbose
// or
$ ./vendor/bin/phpunit --bootstrap tests/bootstrap.php tests/

待办事项

  • UriNormalizer: normalize: 删除目录索引
  • UriNormalizer: normalize: 用域名替换 IP
  • UriNormalizer: normalize: 删除或添加“www”作为第一个域名标签
  • UriNormalizer: normalize: 删除未使用的查询变量
  • UriNormalizer: 使用更好的语义为规范化方法命名,使流畅接口更直观
  • UriNormalizer: 将规范化方法重构为类
  • UriFactory: 根据 RFC 自动规范化 Uri
  • 添加 UriFactoryTrait

变更日志

[0.6.1]

  • 添加更严格的类型转换
  • 添加更多 Uri 测试

[0.6]

  • 添加 UriFactory::create() 方法
  • 添加 UriFactory::setClassName() 方法
  • 添加更多测试
  • 重构 UriNormalizer 并添加流畅方法接口。
  • 添加更多规范化方法

[0.5]

  • 更改 Uri 类: 删除构造函数
  • 更改 Uri 类: 实现更好的创建新修改实例的方法
  • 更改 Uri 类: 使用类属性而不是属性数组
  • 添加 PHP8 注释
  • 添加 TravisCI 构建目标 php7.4 & php8.0
  • 添加对 PHPUnit9 的测试支持

[0.4]

  • 弃用 UriBuilder 以支持 UriFactory
  • 添加 UriFactory
  • 更改 Uri 构造函数
  • 将最小 PHP 语言级别更改为 7.1
  • 将许可证更改为 MIT 许可证
  • 添加对 PHPUnit8 的测试支持
  • 修复代码风格

[0.3.1]

  • 添加 PSR-7 兼容性。Uri 现在实现 PSR UriInterface
  • 添加 TravisCI 构建目标 php7.2 & php7.3

[0.3]

  • 将命名空间更改为 '\FmLabs\Uri'
  • 将项目名称更改为 'php-uri'
  • 移除 UrlExpander 类
  • 重构 Url 到 Uri 类
  • 重构 UrlNormalizer 到 UriNormalizer
  • 添加 UriBuilder 类
  • 将单元测试升级到 PHPUnit6
  • 将最小 PHP 语言级别更改为 7.1

[0.2]

  • 添加 UrlExpander 实用类 (需要 curl)

许可证

参见 LICENSE 文件