Skip to content

Core\Uri

Reads, builds and encodes URIs and query strings. parse reads a URI into its parts, and with and resolved build a new one. The encode and decode methods escape and unescape text, and parseQuery and buildQuery convert between a query string and an array. signed and verifySignature sign a link and check it.

Core\Uri::parse reads a URI reference and answers an instance whose readers report each component exactly as written — still percent-encoded, in its own case. Every reader but path is ?string (port is ?int) and answers null where the component was not written; tryParse is the same read with null in place of RuntimeError, and is the spelling of "is this text a URI". with and resolved answer a fresh Uri, and compareTo is the normalized content comparison — == on two Uri objects is identity. parseQuery and buildQuery read and write the bracket convention, and queryParameter/withQueryParameter are the same convention over one parameter by name — a name with cannot take as a bag key, because a bag's keys are declared and a parameter's name is chosen at run time. The name is top-level, so an array $value is what writes a[b]; a null $value removes the pair, and removing the last one leaves no query at all rather than a bare ?. encodeComponent writes a space as %20, and encodeFormValue writes it as +. The two **decoders answer bytes**, because percent-decoding is defined over octets and a client may send any of them: Core\Uri::decodeComponent("%FF") has an answer, and text is one as string away — which throws for octets no string can hold, exactly where a string-returning decoder would have. parseQuery answers those same octets for a value; a name is the array key, so it is a string.

<?nvs
var $u = Core\Uri::parse('https://example.com:8443/a/b%20c?x=1&y=2#top');
echo $u->scheme() ?? '-', ' ', $u->host() ?? '-', ' ', $u->port() ?? 0, "\n";
echo $u->path(), ' ', $u->query() ?? '-', ' ', $u->fragment() ?? '-', "\n";
echo $u->with({ path: '/c', fragment: 'end' })->toString(), "\n";
echo $u->resolved('../d?z=3')->toString(), "\n";
var $rel = Core\Uri::parse('/only/a/path');
echo $rel->scheme() == null ? 'relative' : 'absolute', "\n";
echo Core\Uri::tryParse('http://example.com/a b') == null ? 'not a uri' : 'a uri', "\n";
var $same = Core\Uri::parse('HTTPS://EXAMPLE.com:8443/a/b%20c?x=1&y=2#top');
echo $u->compareTo($same) == 0 ? 'same uri' : 'different', "\n";
var $q = Core\Uri::parseQuery('a=1&b[]=2&b[]=3');
echo Core\Str::join(Core\Arr::keys($q), ','), "\n";
array<mixed> $params = ['name' => 'a b', 'page' => 2];
echo Core\Uri::buildQuery($params), "\n";
echo $u->queryParameter('x') as bytes as string, "\n";
echo $u->withQueryParameter('y', null)->withQueryParameter('z', '3')->toString(), "\n";
echo Core\Uri::encodeComponent('a b&c'), ' ', Core\Uri::encodeFormValue('a b&c'), "\n";
Output
https example.com 8443
/a/b%20c x=1&y=2 top
https://example.com:8443/c?x=1&y=2#end
https://example.com:8443/d?z=3
relative
not a uri
same uri
a,b
name=a+b&page=2
1
https://example.com:8443/a/b%20c?x=1&z=3#top
a%20b%26c a+b%26c

Members

23 members.

MemberSignature
parseparse(string $uri): Core\Uri
tryParsetryParse(string $uri): ?Core\Uri
encodeComponentencodeComponent(string $s): string
decodeComponentdecodeComponent(string $s): bytes
encodeFormValueencodeFormValue(string $s): string
decodeFormValuedecodeFormValue(string $s): bytes
parseQueryparseQuery(string $query): array<mixed>
buildQuerybuildQuery(array<mixed> $parameters): string
schemescheme(): ?string
userInfouserInfo(): ?string
hosthost(): ?string
portport(): ?int
pathpath(): string
queryquery(): ?string
fragmentfragment(): ?string
toStringtoString(): string
withwith({ scheme?: string, host?: string, port?: ?int, path?: string, query?: ?string, fragment?: ?string }): Core\Uri
queryParameterqueryParameter(string $name): mixed
withQueryParameterwithQueryParameter(string $name, mixed $value): Core\Uri
resolvedresolved(string $reference): Core\Uri
compareTocompareTo(Core\Uri $other): int
signedsigned({ keys: array<secret bytes>, until: ?Core\Time\Instant } $settings): Core\Uri
verifySignatureverifySignature(array<secret bytes> $keys): void