Documentation

Url extends DataObject
in package
uses TypecastTrait

URL object and helper methods.

Think of this as an unholy mush of the JS-style URL and URLSearchParams.

Table of Contents

Properties

$fragment  : string|null
$host  : string|null
$pass  : string|null
$path  : string|null
$port  : int|null
$query  : array<string, mixed>
$scheme  : string|null
$user  : string|null
$__typecast__  : Typecast

Methods

__construct()  : mixed
__toString()  : string
addParam()  : static
Add to a query parameter.
addParams()  : static
Merge parameters into the query.
build()  : string
Cleanly joins base urls + paths.
decode()  : array<string, mixed>
encode()  : string
getDefaultPort()  : null|int
getDefaultPorts()  : array<string, int>
getQueryString()  : string
Get the query string.
getTypecast()  : Typecast
The typecast instance for this class type.
hasParam()  : bool
Does this query param exist?
normalize()  : void
Remove the port if the scheme infers a default one.
parse()  : self
Parse a string into a URL object.
removeParam()  : static
Remove a query param by name.
setParam()  : static
Set a parameter.
setQuery()  : static
Replace the query.
toString()  : string
Build a URL from the parts.
update()  : void

Properties

$fragment

public string|null $fragment = null

after the hashmark #

$host

public string|null $host = null

$pass

public string|null $pass = null

$path

public string|null $path = null

$port

public int|null $port = null

$query

public array<string, mixed> $query = []

after the question mark ?

Attributes
#[CastMethod]
'decode'

$scheme

public string|null $scheme = null

$user

public string|null $user = null

Methods

__construct()

public __construct([iterable<string|int, mixed> $config = [] ]) : mixed
Parameters
$config : iterable<string|int, mixed> = []

__toString()

public __toString() : string
Tags
inheritdoc
Return values
string

addParam()

Add to a query parameter.

public addParam(string $name, mixed $value) : static

This will append to the existing item if it's an array, otherwise converts it to an array and adds the value.

Parameters
$name : string
$value : mixed
Return values
static

addParams()

Merge parameters into the query.

public addParams(array<string|int, mixed> $query) : static
Parameters
$query : array<string|int, mixed>
Tags
throws
UrlDecodeException
Return values
static

build()

Cleanly joins base urls + paths.

public static build(string|array<string|int, mixed> ...$parts) : string

A path can be just a string or a bunch of fragments.

[ 'path', 'to', 'thing', 'param' => 123, 'neat' => ['one', 'two'], ]

Should return: '/path/to/thing?param=123&neat[0]=one&neat[1]=two'

Parameters
$parts : string|array<string|int, mixed>
Return values
string

decode()

public static decode(string|array<string|int, mixed> $query) : array<string, mixed>
Parameters
$query : string|array<string|int, mixed>
Tags
throws
UrlDecodeException
Return values
array<string, mixed> —

[key => value]

encode()

public static encode(array<string|int, mixed> $query) : string
Parameters
$query : array<string|int, mixed>

[key => value]

Return values
string

getDefaultPort()

public static getDefaultPort(string $scheme) : null|int
Parameters
$scheme : string
Return values
null|int

getDefaultPorts()

public static getDefaultPorts() : array<string, int>
Return values
array<string, int>

getQueryString()

Get the query string.

public getQueryString() : string
Return values
string

hasParam()

Does this query param exist?

public hasParam(string $name) : bool
Parameters
$name : string
Return values
bool —

exists

normalize()

Remove the port if the scheme infers a default one.

public normalize() : void

parse()

Parse a string into a URL object.

public static parse(string $url) : self
Parameters
$url : string
Tags
throws
UrlParseException
Return values
self

removeParam()

Remove a query param by name.

public removeParam(string $name) : static
Parameters
$name : string
Return values
static

setParam()

Set a parameter.

public setParam(string $name, mixed $value) : static
Parameters
$name : string
$value : mixed
Return values
static

setQuery()

Replace the query.

public setQuery(array<string|int, mixed>|string $query) : static
Parameters
$query : array<string|int, mixed>|string
Tags
throws
UrlDecodeException
Return values
static

toString()

Build a URL from the parts.

public toString([bool $normalize = true ]) : string
Parameters
$normalize : bool = true
Return values
string

update()

public update(iterable<string|int, mixed> $config) : void
Parameters
$config : iterable<string|int, mixed>
Tags
inheritdoc
On this page

Search results