The UserInfo

The UserInfo 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.

Creating a new object

The UserInfo 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\UserInfo;
use League\Uri\UriString;

$authority = new UserInfo('user', 'pass');
$authority->toString(); //returns 'user:pass'

UserInfo::new('user:pass')->value(); //returns 'user:pass'
UserInfo::fromUri("http://www.example.com/path/to/the/sky")->getUser(); //return null;
UserInfo::new()->value(); //return null;
UserInfo::fromComponents(
	UriString::parse("http://user:pass@example.com:42/5.0/uri/api")
)->value(); //returns 'user:pass'

submitted string is normalized to be RFC3986 compliant.

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

Accessing User information content

To access the user login and password information you need to call the respective UserInfo::getUser and UserInfo::getPass methods like shown below.

use League\Uri\Components\UserInfo;

$info = new UserInfo('user', 'p@ss');
$info->getUser();    //returns 'user'
$info->getPass();    //returns 'p@ss'
$info->getUsername(); //returns 'user'
$info->getPassword(); //returns 'p%40ss'
$info->components(); //returns array {"user" => "user", "pass" => "p@ss"}

getUsername and getPassword are added in version 7.5.0

Modifying the user information

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

use League\Uri\Components\UserInfo;

$info = UserInfo::fromUri('https://login:pass@thephpleague.com/path/to/heaven');
echo $info;  //displays login:pass
echo $info->withUser('john')->withPass('doe'); //displays john:doe

If the user part is `null`, trying to give the password any other value than the `null` value with throw an Exception.

new UserInfo(null, 'bar');  // throws a SyntaxError
UserInfo::fromAuthority('thephpleague:443')->withPass('foo'); // throws a SyntaxError

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