The Authority part
The Authority
class represents a URI authority component. Apart from the package common API the class
exposes basic properties and method to manipulate its different component.
Instantiation
The Authority
class comes with named constructors to ease instantiation. The following examples show
how to instantiate the class:
<?php
use League\Uri\Components\Authority;
use League\Uri\UriString;
$authority = new Authority('eXamPle.cOm', 42, 'user:pass');
$authority->toString(); //returns 'user:pass@example.com:42'
Authority::new('user:pass@example.com:42')->value(); //returns 'user:pass@example.com:42'
Authority::fromUri("http://www.example.com/path/to/the/sky")->getPort(); //return null;
Authority::new()->value(); //return null;
Authority::fromComponents(
UriString::parse("http://user:pass@example.com:42/5.0/uri/api")
)->value(); //returns 'user:pass@example.com:42'
Accessing properties
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:
use League\Uri\Components\Authority;
$authority = Authority::new("foo:bar@www.example.com:81");
echo $authority->getUserInfo(); //displays "foo:bar"
echo $authority->getHost(); //displays "www.example.com"
echo $authority->getPort(); //displays 81 as an integer
echo $authority;
//displays "foo:bar@www.example.com:81"
echo json_encode($authority);
//displays "foo:bar@www.example.com:81"
$authority->components();
// returns array {
// "user" => "foo",
// "pass" => "bar",
// "host" => "www.example.com",
// "port" => 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.
Since All URI object are immutable you can chain each modifying methods to simplify URI creation and/or modification.
echo Authority::new("thephpleague.com")
->withUserInfo("foo", "bar")
->withHost("www.example.com")
->withPort(81)
->toString(); //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
echo Authority::new("www.ExAmPLE.com:80"); //displays www.example.com:80