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.

If the modifications do not change the current object, it is returned as is, otherwise, a new modified object is returned.

If the submitted value is not valid a League\Uri\Exceptions\SyntaxError exception is thrown.

Instantiation

The Authority class comes with named constructors to ease instantiation. The following examples show how to instantiate the class:

submitted string is normalized to be RFC3986 compliant.

<?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'

if no string is given a instance is returns using the empty string.

If you supply your own hash to fromComponents, you are responsible for providing well parsed components without their URI delimiters.

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.

Any modification method can trigger a League\Uri\Contracts\UriException exception if the resulting URI is not valid. Just like with the instantiation methods, validation is scheme dependant.

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:

echo Authority::new("www.ExAmPLE.com:80"); //displays www.example.com:80

Host conversion depends on the presence of the idn_to_* functions, if missing the code will trigger a MissingFeature exception