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.
Creating a new object
The UserInfo
class comes with named constructors to ease instantiation. The following examples show
how to instantiate the class:
<?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'
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"}
Modifying the user information
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
new UserInfo(null, 'bar'); // throws a SyntaxError
UserInfo::fromAuthority('thephpleague:443')->withPass('foo'); // throws a SyntaxError