k911/uri-builder

此包已被废弃且不再维护。没有建议的替代包。

UriBuilder 库简化了对与 PSR-7 兼容的 Uri 值对象的操作

v0.9.0 2017-10-06 21:37 UTC

This package is auto-updated.

Last update: 2022-02-01 13:10:16 UTC


README

CircleCI

简化了与 PSR-7 兼容的 URI 值对象的操作。在底层,它利用了 League\Uri 强大的

Code Climate Test Coverage Issue Count

安装

$ composer require k911/uri-builder

支持的方案

目前支持的、经过测试的 URI 方案,UriBuilder 可以从裸 URI 字符串或 URI 组件中管理和解析。

  • http/https
  • ftp/sftp
  • ws/wss
  • file
  • data

使用

完整公共接口可在此处找到 这里

使用示例

// Simple URI string
$uri = 'wss://foo.bar:9999';

// Create UriBuilder and its dependencies
// ..you should either use DI container to manage it
// ..or use UriBuilder facade
$parser = new K911\UriBuilder\Adapter\UriParserAdapter();
$factory = new K911\UriBuilder\UriFactory($parser);
$builder = new K911\UriBuilder\UriBuilder($factory);

// Intiliaze UriBuilder with URI string
$builder->from($uri);
// or $builder->fromUri(UriInterface $uri);
// or $builder->fromComponents(array $components);

// UriBuilder is mutable, and allows method chaining
$builder
    // under the hood, it automatically transforms Uri object
    ->setScheme('https')
    // simple setters
    ->setHost('api.foo.bar')
    ->setFragment('foobar')
    // setting DEFAULT port for https scheme
    ->setPort(443)
    // domain-related paths must always start with forward slash '/'
    ->setPath('/v1')
    // query string is generated safely from pairs according to RFC3986
    ->setQuery([
        'api_token' => 'Qwerty! @#$TYu',
    ])
    // set user info (password can be omitted)
    ->setUserInfo('user', 'password');


// Print result
echo (string) $builder->getUri();
// https://user:password@api.foo.bar/v1?api_token=Qwerty%21%20%40%23%24TYu#foobar