Luminova Framework

PHP Luminova: URI Class with Internationalized Domain Name (IDN) Support

Last updated: 2026-11-28 20:57:12

The HTTP URI class provides methods for creating and managing URIs, including support for Internationalized Domain Names (IDNs), while adhering to the PSR-7 HTTP Message Interface standards.

The Luminova Uri class provides an implementation of the Psr\Http\Message\UriInterface for constructing, parsing, and manipulating Uniform Resource Identifiers (URIs). This class is designed to comply with the PSR-7 HTTP Message Interface standards and is capable of handling complex URI operations with including IDN (Internationalized Domain Name) support.


Usage Examples

Constructs a new Uri instance with optional components.

$uri = new Luminova\Http\Uri(
    'https',
    'username:password',
    'example.com',
    443,
    '/api/resource',
    'key=value',
    'section1'
);

echo $uri; 
// Output: https://username:[email protected]:443/api/resource?key=value#section1

Create From Array

Create with an array of URI components.

use Luminova\Http\Uri;

$uriComponents = [
    'scheme' => 'https',
    'host' => 'example.com',
    'path' => '/api/resource',
    'query' => 'key=value',
    'fragment' => 'section1'
];

$uri = Luminova\Http\Uri::fromArray($uriComponents);

// Output: https://example.com/api/resource?key=value#section1
echo $uri;

Modifying Components

Each method returns a new instance with the modified component, maintaining immutability.

$uri = new Luminova\Http\Uri('https', '', 'example.com', 443, '/path', '', '');

$newUri = $uri
    ->withScheme('http')
    ->withHost('test.com')
    ->withPath('/new-path')
    ->withQuery('id=123')
    ->withFragment('section');

echo $newUri;
// Output: http://test.com/new-path?id=123#section

Adding User Info

$uri = new Luminova\Http\Uri('https', '', 'example.com', 443);

$updatedUri = $uri->withUserInfo('user', 'password');

echo $updatedUri;
// Output: https://user:[email protected]:443

IDN Support

Create a URI with an internationalized domain name

use Luminova\Http\Uri;

$uri = new Uri('https', '', '例子.测试', 443, '/path', 'query=value', 'fragment');

// Convert the host to its ASCII representation
try {
    $asciiUri = $uri->toAsciiIdn();
    echo $asciiUri;
    // Output: https://xn--fsq.xn--0zwm56d:443/path?query=value#fragment
} catch (Luminova\Exceptions\ErrorException $e) {
    echo "Error converting to ASCII: " . $e->getMessage();
}

With Options for IDN Conversion

You can customize the conversion process by passing specific options to the method. For example, using IDNA_NONTRANSITIONAL_TO_ASCII:

use Luminova\Http\Uri;

$uri = new Uri('https', '', 'bücher.de', null, '/store', '', '');

// Convert the host using a non-transitional option
try {
    $asciiUri = $uri->toAsciiIdn(IDNA_NONTRANSITIONAL_TO_ASCII);
    echo $asciiUri;
    // Output: https://xn--bcher-kva.de/store
} catch (ErrorException $e) {
    echo "Error converting to ASCII: " . $e->getMessage();
}

Class Definition


Methods

constructor

Constructs a new Uri instance with optional components. This initializer allows you to define a URI with individual parts, providing flexibility for creating URIs programmatically.

public function __construct(
    string $scheme = '',
    string $userInfo = '',
    string $host = '',
    ?int $port = null,
    string $path = '/',
    string $query = '',
    string $fragment = ''
)

Parameters:

ParameterTypeDescription
$schemestringOptional URI scheme (e.g., "http", "https").
$userInfostringOptional user information (e.g., "username:password").
$hoststringOptional host (e.g., "example.com").
$portint|nullOptional port (e.g., 80, 443, or null for default ports).
$pathstringOptional path (e.g., "/api/resource").
$querystringOptional query string (e.g., "key=value").
$fragmentstringOptional fragment (e.g., "section1").

fromArray

Creates a new Uri instance from an array of URI components.

The fromArray method creates a new Uri instance from an associative array of URI components. This static constructor is particularly useful when working with the results of functions like parse_url.

public static function fromArray(array $parts): self

Parameters:

ParameterTypeDescription
$partsarray<string, mixed>An associative array of URI components.

Return:

\Psr\Http\Message\UriInterface - Return a new Uri instance populated with the provided array values.

Supported keys in the array include:

  • scheme: (string) - The URI scheme (e.g., "http", "https").
  • user: (string) - The username for authentication.
  • pass: (string) - The password for authentication (appended to the user).
  • host: (string) - The host portion of the URI (e.g., "example.com").
  • port: (int|null) - The port number (e.g., 80, 443).
  • path: (string) - The URI path (e.g., "/foo/bar").
  • query: (string) - The query string (e.g., "key=value").
  • fragment: (string) - The fragment identifier (e.g., "#section").

fromString

Create a URI instance from a string.

Parses a URI string into its individual components (scheme, host, port,path, and query) and returns a new Uri instance populated with those values.

This method supports both absolute URIs (e.g. https://example.com/path)and relative URIs (e.g. /login?next=/admin). If the URI does not define a path, the path defaults to /.

public static function fromString(string $uri): self

Parameters:

ParameterTypeDescription
$uristringThe URI string to parse.

Return:

static - Returns a new Uri instance created from the parsed URI.

Throws:


getScheme

Retrieves the URI scheme component.

public function getScheme(): string

Return:

string - Return the URI scheme (e.g., "http", "https").


getAuthority

Retrieves the URI authority component.

public function getAuthority(): string

Return:

string - Return the URI authority, including user info, host, and port.


getUserInfo

Retrieves the user information component.

public function getUserInfo(): string

Return:

string - Return the user information (e.g., "username:password").


getHost

Retrieves the host component.

public function getHost(): string

Return:

string - Return the host (e.g., "example.com").


getPort

Retrieves the port component.

public function getPort(): ?int

Return:

string - Return the port number or null if no port is specified.


getPath

Retrieves the path component.

public function getPath(): string

Return:

string - Return the path (e.g., "/api/resource").


getQuery

Retrieves the query string component.

public function getQuery(): string

Return:

string - Return the query string (e.g., "key=value").


getFragment

Retrieves the fragment component.

public function getFragment(): string

Return:

string - Return the fragment (e.g., "section1").


toArray

Convert the URI into an associative array.

Returns the individual URI components as an array. Missing components are returned as empty strings or null where appropriate.

public function toArray(): array

Return:

array<string,string|int|null> - Returns an array representation of the URI.


toString

Converts the URI object to a string representation.

public function toString(): string

Return:

string - Return the full URI as a string.


toAsciiIdn

Converts the URI's host to its ASCII representation using IDN (Internationalized Domain Names) conversion.

This method converts the host component of the URI to its ASCII representation, which is necessary for proper handling of internationalized domain names. It uses the idn_to_ascii function with the specified options for the conversion.

public function toAsciiIdn(int $option = \IDNA_DEFAULT, int $variant = \INTL_IDNA_VARIANT_UTS46): string

Parameters:

ParameterTypeDescription
$optionintThe option flag for IDN conversion. Defaults to IDNA_DEFAULT.
See the PHP manual for possible values (e.g., IDNA_NONTRANSITIONAL_TO_ASCII).
$variantintIDNA variant passed to idn_to_ascii(). Defaults to INTL_IDNA_VARIANT_UTS46.

Return:

string - Return the full URI string with the host converted to its ASCII representation.

Throws:


withScheme

Returns a new instance with the specified scheme.

public function withScheme(string $scheme): self

Parameters:

ParameterTypeDescription
$schemestringThe new scheme (e.g., "http", "https").

Return:

self - Returns a new instance with the updated scheme.


withUserInfo

Returns a new instance with the specified user info.

public function withUserInfo(string $user, ?string $password = null): self

Parameters:

ParameterTypeDescription
$userstringThe username.
$passwordstring|nullThe password (optional).

Return:

self - Returns a new instance with the updated user info.


withHost

Returns a new instance with the specified host.

public function withHost(string $host): self

Parameters:

ParameterTypeDescription
$hoststringThe new host (e.g., "example.com").

Return:

self - Returns a new instance with the updated host.


withPort

Returns a new instance with the specified port.

public function withPort(?int $port): self

Parameters:

ParameterTypeDescription
$portint|nullThe new port or null for no port.

Return:

self - Returns a new instance with the updated port.


withPath

Returns a new instance with the specified path.

public function withPath(string $path): self

Parameters:

ParameterTypeDescription
$pathstringThe new path (e.g., "/api/resource").

Return:

self - Returns a new instance with the updated path.


withQuery

Returns a new instance with the specified query string.

public function withQuery(string $query): self

Parameters:

ParameterTypeDescription
$querystringThe new query string (e.g., "key=value").

Return:

self - Returns a new instance with the updated query string.


withFragment

Returns a new instance with the specified fragment.

public function withFragment(string $fragment): self

Parameters:

ParameterTypeDescription
$fragmentstringThe new fragment (e.g., "section1").

Return:

self - Returns a new instance with the updated fragment.


getPaths

Split the URI path into segments after trimming leading and trailing slashes.

public function getPaths(): array

Return:

string[] - Returns the path segments. An empty path produces a list containing an empty string.


jsonSerialize

Get the URI components used by json_encode(). This method delegates to the same component conversion as toArray().

public function jsonSerialize(): mixed

Return:

array<string,string|int|null> - Returns the URI components keyed by name.