The Authority part
The League\Uri\Components\Authority
class ease URI authority part creation and manipulation. This object exposes:
- the package common API,
but also provide specific methods to work with a URI authority part.
Instantiation
Using the default constructor
<?php
public Authority::__construct($authority = null): void
Using a string
<?php
use League\Uri\Components\Authority;
$authority = Authority::createFromString('user:pass@example.com:42');
$authority->getContent(); //returns 'user:pass@example.com:42'
Using Uri components
<?php
use League\Uri\Components\Authority;
use League\Uri\UriString;
$authority = Authority::createFromComponents(
UriString::parse("http://user:pass@example.com:42/5.0/uri/api")
);
$authority->getContent(); //returns 'user:pass@example.com:42'
Using an URI object
The URI must implements League UriInterface
or PSR-7 UriInterface
.
<?php
use Laminas\Diactoros\Uri as LaminasUri;
use League\Uri\Components\Authority;
$psr7Uri = new LaminasUri("http://www.example.com/path/to/the/sky");
$authority = Authority::createFromUri($psr7Uri);
$authority->getPort(); //return null;
Using null
<?php
use League\Uri\Components\Authority;
$authority = Authority::createFromNull();
$authority->getContent(); //return null;
Authority validation
A League\Uri\Contracts\UriException
exception is triggered if an invalid Authority value is given.
$uri = Authority::createFromString(':80');
// throws a League\Uri\Exceptions\SyntaxError
// because the URI string is invalid
Accessing properties
The Authority object exposes the following specific methods.
public function Authority::getUserInfo(void): ?string
public function Authority::getHost(void): ?string
public function Authority::getPort(void): ?int
You can access the authority string, its individual parts and components using their respective getter methods. This lead to the following result for a simple HTTP URI:
$uri = Authority::createFromString("foo:bar@www.example.com:81");
echo $uri->getUserInfo(); //displays "foo:bar"
echo $uri->getHost(); //displays "www.example.com"
echo $uri->getPort(); //displays 81 as an integer
echo $uri;
//displays "foo:bar@www.example.com:81"
echo json_encode($uri);
//displays "foo:bar@www.example.com:81"
Modifying properties
To replace one of the URI component you can use the modifying methods exposed by all URI object. If the modifications do not alter the current object, it is returned as is, otherwise, a new modified object is returned.
<?php
public function Authority::withUserInfo(?string $user, ?string $password = null): self
public function Authority::withHost(?string $host): self
public function Authority::withPort(?int $port): self
Since All URI object are immutable you can chain each modifying methods to simplify URI creation and/or modification.
$uri = Authority::createFromString("thephpleague.com")
->withUserInfo("foo", "bar")
->withHost("www.example.com")
->withPort(81);
echo $uri->getUriComponent(); //displays "//foo:bar@www.example.com:81"
Normalization
Out of the box the package normalizes the URI part according to the non destructive rules of RFC3986.
These non-destructive rules are:
- scheme and host components are lowercased;
- the host is converted to its ascii representation using punycode if needed
$uri = new Uri("www.ExAmPLE.com:80");
echo $uri; //displays www.example.com:80