WpOrgRequestsIri

IRI (Internationalized Resource Identifier) parser, serializer, and normalizer.

Source: wp-includes/Requests/src/Iri.php
Namespace: WpOrgRequests
Package: RequestsUtilities
Authors: Geoffrey Sneddon, Steve Minutillo
License: BSD


Overview

Parses, normalizes, and serializes IRIs per RFC 3987/3986. Supports scheme normalization for acap, dict, file, http, and https. Provides magic property access for IRI components in both IRI and URI forms. Handles IPv6 host compression, percent-encoding normalization, and dot-segment removal.


Properties

All properties are accessed via magic __get/__set. The class stores internal ("i"-prefixed) forms and derives URI forms on access.

PropertyTypeDescription
$iristringComplete IRI (read via get_iri())
$uristringIRI converted to URI form (read-only, via to_uri())
$schemestring|nullScheme component
$authoritystringAuthority in URI form
$iauthoritystringAuthority in IRI form
$userinfostringUserinfo in URI form
$iuserinfostring|nullUserinfo in IRI form
$hoststringHost in URI form
$ihoststring|nullHost in IRI form
$portstring|nullPort number
$pathstringPath in URI form
$ipathstringPath in IRI form (defaults to '')
$querystringQuery in URI form
$iquerystring|nullQuery in IRI form
$fragmentstringFragment in URI form
$ifragmentstring|nullFragment in IRI form

Internal Properties

PropertyTypeDefaultDescription
$schemestring|nullnullScheme
$iuserinfostring|nullnullUser information
$ihoststring|nullnullHost
$portstring|nullnullPort
$ipathstring''Path
$iquerystring|nullnullQuery
$ifragmentstring|nullnullFragment
$normalizationarray(see below)Scheme-specific normalization defaults

Normalization Database

php
$normalization = [
    'acap'  => ['port' => Port::ACAP],       // 674
    'dict'  => ['port' => Port::DICT],       // 2628
    'file'  => ['ihost' => 'localhost'],
    'http'  => ['port' => Port::HTTP],       // 80
    'https' => ['port' => Port::HTTPS],      // 443
];

When a component is null and the scheme has a default, the default is returned by __get(). During normalization, components matching defaults are set to null to suppress them.


Public Methods

__construct()

php
public function __construct( string|Stringable|null $iri = null )
ParameterTypeDescription
$iristring|Stringable|nullIRI string to parse

Throws: InvalidArgument when $iri is not a string, Stringable, or null.


__toString()

php
public function __toString(): string

Returns the complete IRI via get_iri().


__set() / __get() / __isset() / __unset()

Magic property access. __set dispatches to set_* methods. __get dispatches to get_* methods or reads internal properties directly, with fallback mapping between IRI/URI forms (e.g., hostihost, ischemescheme). Returns scheme normalization defaults when a property is null.


absolutize()

Resolve a relative IRI against an absolute base IRI.

php
public static function absolutize( Iri|string $base, Iri|string $relative ): Iri|false
ParameterTypeDescription
$baseIri|stringAbsolute base IRI
$relativeIri|stringRelative IRI to resolve

Returns: Iri on success, false if the base has no scheme or either IRI is invalid.

Implements RFC 3986 Section 5 reference resolution algorithm.


is_valid()

php
public function is_valid(): bool

Checks structural validity: path must start with / when authority is present; relative paths without authority cannot have : before the first /.


__wakeup()

Validates property types on unserialization. Throws UnexpectedValueException on type mismatch, then sets all properties to null.


Protected Methods

MethodSignatureDescription
parse_iri()(string $iri): arrayRegex-based parsing into scheme/authority/path/query/fragment
remove_dot_segments()(string $input): stringRFC 3986 Section 5.2.4 dot segment removal
replace_invalid_with_pct_encoding()(string $text, string $extra_chars, bool $iprivate = false): stringNormalize and percent-encode invalid characters
remove_iunreserved_percent_encoded()(array $regex_match): stringCallback: decode percent-encoded iunreserved chars
scheme_normalization()(): voidRemove components matching scheme defaults
set_iri()(string $iri): boolParse and set all components (cached)
set_scheme()(string $scheme): boolValidate and lowercase scheme
set_authority()(string $authority): boolParse authority into userinfo/host/port (cached)
set_userinfo()(string $iuserinfo): boolSet userinfo with percent-encoding
set_host()(string $ihost): boolSet host; handles IPv6 compression and lowercasing
set_port()(string $port): boolValidate numeric port
set_path()(string $ipath): boolSet path with percent-encoding and dot removal (cached)
set_query()(string $iquery): boolSet query with percent-encoding (iprivate allowed)
set_fragment()(string $ifragment): boolSet fragment with percent-encoding
to_uri()(string|bool $iri): string|falseConvert IRI to URI by percent-encoding non-ASCII bytes
get_iri()(): string|falseAssemble complete IRI from components
get_uri()(): stringGet complete URI
get_iauthority()(): string|nullAssemble iauthority (userinfo@host:port)
get_authority()(): stringGet authority in URI form

IRI Resolution Flow

absolutize($base, $relative)
│
├─ $relative has scheme → clone $relative (already absolute)
├─ $relative has authority → use $base scheme + $relative authority/path/query
├─ $relative has path
│  ├─ Starts with '/' → use as-is
│  └─ Merge with base path, remove dot segments
│     └─ Use $relative query
├─ $relative has query → use base path + relative query
└─ Empty relative → clone base (clear fragment)
   └─ Apply scheme_normalization()