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
$__typecast__
private
Typecast
$__typecast__
Methods
__construct()
public
__construct([iterable<string|int, mixed> $config = [] ]) : mixed
Parameters
- $config : iterable<string|int, mixed> = []
__toString()
public
__toString() : string
Tags
Return values
stringaddParam()
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
staticaddParams()
Merge parameters into the query.
public
addParams(array<string|int, mixed> $query) : static
Parameters
- $query : array<string|int, mixed>
Tags
Return values
staticbuild()
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
stringdecode()
public
static decode(string|array<string|int, mixed> $query) : array<string, mixed>
Parameters
- $query : string|array<string|int, mixed>
Tags
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
stringgetDefaultPort()
public
static getDefaultPort(string $scheme) : null|int
Parameters
- $scheme : string
Return values
null|intgetDefaultPorts()
public
static getDefaultPorts() : array<string, int>
Return values
array<string, int>getQueryString()
Get the query string.
public
getQueryString() : string
Return values
stringgetTypecast()
The typecast instance for this class type.
public
getTypecast() : Typecast
Return values
TypecasthasParam()
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
Return values
selfremoveParam()
Remove a query param by name.
public
removeParam(string $name) : static
Parameters
- $name : string
Return values
staticsetParam()
Set a parameter.
public
setParam(string $name, mixed $value) : static
Parameters
- $name : string
- $value : mixed
Return values
staticsetQuery()
Replace the query.
public
setQuery(array<string|int, mixed>|string $query) : static
Parameters
- $query : array<string|int, mixed>|string
Tags
Return values
statictoString()
Build a URL from the parts.
public
toString([bool $normalize = true ]) : string
Parameters
- $normalize : bool = true
Return values
stringupdate()
public
update(iterable<string|int, mixed> $config) : void
Parameters
- $config : iterable<string|int, mixed>