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.
<?nvsvar $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";https example.com 8443/a/b%20c x=1&y=2 tophttps://example.com:8443/c?x=1&y=2#endhttps://example.com:8443/d?z=3relativenot a urisame uria,bname=a+b&page=21https://example.com:8443/a/b%20c?x=1&z=3#topa%20b%26c a+b%26cMembers
23 members.
| Member | Signature |
|---|---|
parse | parse(string $uri): Core\Uri |
tryParse | tryParse(string $uri): ?Core\Uri |
encodeComponent | encodeComponent(string $s): string |
decodeComponent | decodeComponent(string $s): bytes |
encodeFormValue | encodeFormValue(string $s): string |
decodeFormValue | decodeFormValue(string $s): bytes |
parseQuery | parseQuery(string $query): array<mixed> |
buildQuery | buildQuery(array<mixed> $parameters): string |
scheme | scheme(): ?string |
userInfo | userInfo(): ?string |
host | host(): ?string |
port | port(): ?int |
path | path(): string |
query | query(): ?string |
fragment | fragment(): ?string |
toString | toString(): string |
with | with({ scheme?: string, host?: string, port?: ?int, path?: string, query?: ?string, fragment?: ?string }): Core\Uri |
queryParameter | queryParameter(string $name): mixed |
withQueryParameter | withQueryParameter(string $name, mixed $value): Core\Uri |
resolved | resolved(string $reference): Core\Uri |
compareTo | compareTo(Core\Uri $other): int |
signed | signed({ keys: array<secret bytes>, until: ?Core\Time\Instant } $settings): Core\Uri |
verifySignature | verifySignature(array<secret bytes> $keys): void |
