phrity/net-uri
PSR-7 Uri和PSR-17 UriFactory实现
2.1.0
2024-07-08 06:14 UTC
Requires
- php: ^8.0
- ext-mbstring: *
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 | ^2.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^9.0 | ^10.0 | ^11.0
- phrity/util-errorhandler: ^1.1
- squizlabs/php_codesniffer: ^3.5
Suggests
- ext-intl: Enables IDN conversion for non-ASCII domains
This package is auto-updated.
Last update: 2024-09-08 06:54:17 UTC
README
简介
实现PSR-7 UriInterface和PSR-17 UriFactoryInterface接口。
没有花哨的功能。只是工作。因为我需要一个不硬编码到HTTP消息的URI实现。还有一些额外功能。允许所有有效的方案。
安装
使用Composer进行安装;
composer require phrity/net-uri
Uri类方法
实现了PSR-7 UriInterface并提供了额外的方法和选项。更多信息在这里。
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'); // Additional methods $uri->toString(); $uri->__toString(); $uri->jsonSerialize(); $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类方法
实现了PSR-17 UriFactoryInterface并提供了额外的方法和选项。更多信息在这里。
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'));
修饰符
默认情况下,它将按照PSR标准执行。要更改行为,有一些修饰符可用。这些可以作为所有get
和with
方法以及toString
方法的最后一个参数添加。
REQUIRE_PORT
- 尝试显示端口,即使它是默认的ABSOLUTE_PATH
- 将导致路径使用绝对形式,即以/
开头NORMALIZE_PATH
- 将尝试标准化路径IDN_ENCODE
/IDN_DECODE
- 对非ASCII主机进行IDN编码或解码URI_DECODE
/URI_ENCODE
/URI_ENCODE_3986
- 对URI组件进行编码或解码
示例
$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'