timostamm/url-builder

一个用于解析、操作或创建URL的对象,具有流畅的接口

v1.0.1 2020-09-28 09:17 UTC

This package is auto-updated.

Last update: 2024-08-28 17:33:45 UTC


README

Build Status

一个用于解析、操作或创建URL的对象,具有流畅的接口。

负责路径、查询参数和凭证的正确解码/编码。

对象公开所有常见的Web URL组件作为公共属性

$url = new Url('https://peter%40example.com:pass@domain.tld:8080/all+products/search?query=all#fragment');
$url->scheme->get(); // -> "https"
$url->host->get(); // -> "domain.tld"
$url->port->get(); // -> 8080
$url->credentials->username; // peter@example.com
$url->credentials->password;
$url->path->get(); // "all products/search"
$url->path->filename(); // "search"
$url->query->get('query'); // "all"
$url->fragment->get(); // "fragment"
$url->getUrl(); 

URL操作

$url->clearHost(); // remove the scheme, host, port, credentials
$url->clearPath(); // remove the path, query, fragment
$url->clear(Url::QUERY | Url::FRAGMENT); // remove only specific components

// replace components with components from another URL
$url->replace('http://example.com/index.html', Url::SCHEME);
$url->replacePath('http://example.com/index.html');

$url = new Url('../styles/main.css');
$url->makeAbsolutePath('http://domain.tld/products/search'); // -> /styles/main.css
$url->makeAbsolute('http://domain.tld/products/search'); // -> http://domain.tld/styles/main.css

路径操作

$url->path->set('automatically encöded/')
$url->path->normalize(); // resolves /foo/../bar to /bar
$url->filename(); // returns just the filename

查询操作

$url->query->set('text', 'encöded');
$url->query->has('text');
$url->query->replace([
	'a' => 'foo', 
	'b' => 'bar'
]);
foreach( $url->query as list($key, $values) ) {
	print $key . ": " . join(', ', $values) . "\n"; 
}

凭证操作

$url->credentials->username = 'peter@example.com';
$url->credentials->password = 'pass;
$url->credentials->clear(); // removes username and password if present
$url->crdentials->isEmpty(); // Checks whether a username and/or password is present.
$url->crdentials->equals($otherUrl->credentials); // True if both are empty or both are same.

比较URL或单独的组件

$url->equals($otherUrl);
$url->equals('/all+products/search', Url::PATH);
$url->scheme->equals($otherUrl->scheme);

所有组件的常见方法

->isEmpty(); // is the component present?
->clear(); // makes the component empty
->equals($otherurl->component);