Kotchasan

Uri extends KBase
in package
implements UriInterface

Class for managing Uri (PSR-7)

Tags
see
https://www.kotchasan.com/

Interfaces, Classes and Traits

UriInterface
Value object representing a URI.

Table of Contents

$cfg  : object
Config class instance.
$fragment  : string
Uri fragment after #
$host  : string
Uri host
$path  : string
Uri path
$port  : int
Uri port
$query  : string
Uri query string after ?
$request  : Request
Server request class instance.
$scheme  : string
Uri scheme
$userInfo  : string
Uri user info
__construct()  : mixed
Constructs a new Uri object.
__toString()  : string
Magic function to output the class as a String
createBackUri()  : string
Create a URL for redirecting query strings from one page to another to allow creating URLs that can be sent back to the original page with the `back()` function Remove null items
createFromGlobals()  : Uri
Creates a new Uri object based on the current server environment.
createFromUri()  : Uri
Creates a new Uri object based on the provided URI string.
getAuthority()  : string
Retrieves the authority component of the URI.
getBack()  : string
Convert GET parameters to a query string for returning to the original page after form submission.
getFragment()  : string
Get the fragment (data after #) of the Uri.
getHost()  : string
Get the hostname of the Uri, e.g., domain.tld.
getPath()  : string
Get the path of the Uri, e.g., /kotchasan.
getPort()  : null|int
Get the port number of the Uri.
getQuery()  : string
Get the query string (data after ?) of the Uri.
getScheme()  : string
Get the scheme of the Uri without ://, e.g., http, https.
getUserInfo()  : string
Get the user information of the Uri user[:password].
pagination()  : string
Generate a pagination HTML string.
paramsToQuery()  : string
Convert an array of query parameters to a query string.
parseQueryParams()  : array<string|int, mixed>
Parse the query string and return an array of query parameters.
postBack()  : string
Generate a URL with query string for posting back to the original page, typically used with form submissions.
withFragment()  : static
Set the fragment of the URL.
withHost()  : static
Set the host of the URL.
withoutParams()  : static
Remove specified query parameters from the URL.
withoutQuery()  : static
Remove the specified query parameters from the URL.
withParams()  : static
Set or update query parameters in the URL.
withPath()  : static
Set the path of the URI.
withPort()  : static
Set the port number of the URI.
withQuery()  : static
Set the query string of the URI.
withScheme()  : static
Set the scheme (http or https) of the URI.
withUserInfo()  : self
Return a new instance of the class with the specified user information (username and password).
createBack()  : string
Convert POST data to a query string for redirecting back to the previous page after form submission.
createUriString()  : string
Create a URI string.
filterPath()  : string
Validate the path.
filterPort()  : int|null
Filter and validate the port number for a URI.
filterQueryFragment()  : string
Check and filter the query and fragment components of a URL.
filterScheme()  : string
Check the URL scheme.
isNonStandardPort()  : bool
Check if the port is a non-standard port for the given scheme and host.

Properties

$cfg

Config class instance.

protected static object $cfg

$fragment

Uri fragment after #

protected string $fragment = ''

$host

Uri host

protected string $host = ''

$path

Uri path

protected string $path = ''

$port

Uri port

protected int $port

$query

Uri query string after ?

protected string $query = ''

$request

Server request class instance.

protected static Request $request

$scheme

Uri scheme

protected string $scheme = ''

$userInfo

Uri user info

protected string $userInfo = ''

Methods

__construct()

Constructs a new Uri object.

public __construct(string $scheme, string $host[, string $path = '/' ][, string $query = '' ][, int|null $port = null ][, string $user = '' ][, string $pass = '' ][, string $fragment = '' ]) : mixed
Parameters
$scheme : string

The URI scheme (e.g., "http", "https").

$host : string

The URI host.

$path : string = '/'

The URI path (default is "/").

$query : string = ''

The URI query string (default is an empty string).

$port : int|null = null

The URI port (optional, default is null).

$user : string = ''

The username for URI authentication (optional, default is an empty string).

$pass : string = ''

The password for URI authentication (optional, default is an empty string).

$fragment : string = ''

The URI fragment (part after the # character, optional, default is an empty string).

Return values
mixed

__toString()

Magic function to output the class as a String

public __toString() : string
Return values
string

createBackUri()

Create a URL for redirecting query strings from one page to another to allow creating URLs that can be sent back to the original page with the `back()` function Remove null items

public createBackUri(array<string|int, mixed> $query_string) : string
Parameters
$query_string : array<string|int, mixed>
Return values
string

createFromGlobals()

Creates a new Uri object based on the current server environment.

public static createFromGlobals() : Uri
Tags
throws
InvalidArgumentException

If the provided URI is invalid.

Return values
Uri

A new Uri object based on the server environment.

createFromUri()

Creates a new Uri object based on the provided URI string.

public static createFromUri(string $uri) : Uri
Parameters
$uri : string

The URI string to create the Uri object from.

Tags
throws
InvalidArgumentException

If the provided URI is invalid.

Return values
Uri

A new Uri object based on the provided URI.

getAuthority()

Retrieves the authority component of the URI.

public getAuthority() : string

The authority component consists of the user information, host, and optional port. If the user information is present, it is followed by "@" symbol. If the port is present, it is preceded by ":" symbol.

Return values
string

The authority component of the URI.

getBack()

Convert GET parameters to a query string for returning to the original page after form submission.

public getBack(string $url[, array<string|int, mixed> $query_string = [] ]) : string

Generate a URL for returning to the original page using the specified base URL and query string. The query string can be provided as an array in the form of key-value pairs. The method automatically includes the query string from the current request by default.

Parameters
$url : string

The URL to return to, e.g., index.php.

$query_string : array<string|int, mixed> = []

(optional) The query string to include as an array of key-value pairs. Default is an empty array.

Return values
string

The generated URL with the query string.

getFragment()

Get the fragment (data after #) of the Uri.

public getFragment() : string
Return values
string

The fragment of the Uri.

getHost()

Get the hostname of the Uri, e.g., domain.tld.

public getHost() : string
Return values
string

The hostname of the Uri.

getPath()

Get the path of the Uri, e.g., /kotchasan.

public getPath() : string
Return values
string

The path of the Uri.

getPort()

Get the port number of the Uri.

public getPort() : null|int

If not specified or default port (80, 443), return null.

Return values
null|int

The port number of the Uri.

getQuery()

Get the query string (data after ?) of the Uri.

public getQuery() : string
Return values
string

The query string of the Uri.

getScheme()

Get the scheme of the Uri without ://, e.g., http, https.

public getScheme() : string
Return values
string

The scheme of the Uri.

getUserInfo()

Get the user information of the Uri user[:password].

public getUserInfo() : string
Return values
string

The user information of the Uri.

pagination()

Generate a pagination HTML string.

public pagination(int $totalpage, int $page[, int $maxlink = 9 ]) : string
Parameters
$totalpage : int

The total number of pages.

$page : int

The current page.

$maxlink : int = 9

The maximum number of links to display.

Return values
string

The pagination HTML string.

paramsToQuery()

Convert an array of query parameters to a query string.

public paramsToQuery(array<string|int, mixed> $params, bool $encode) : string
Parameters
$params : array<string|int, mixed>

An array of query parameters.

$encode : bool

Whether to encode the query string using '&' (true) or '&' (false).

Return values
string

The generated query string.

parseQueryParams()

Parse the query string and return an array of query parameters.

public parseQueryParams([string|null $query = null ]) : array<string|int, mixed>
Parameters
$query : string|null = null

The query string to parse (optional). If not provided, the object's query string will be used.

Return values
array<string|int, mixed>

An array of query parameters.

postBack()

Generate a URL with query string for posting back to the original page, typically used with form submissions.

public postBack(string $url[, array<string|int, mixed> $query_string = [] ][, bool $encode = false ]) : string
Parameters
$url : string

The URL to which the form should be submitted.

$query_string : array<string|int, mixed> = []

Additional query string parameters to be included in the URL.

$encode : bool = false

Determines whether the query string should be encoded using & or & (default is false).

Return values
string

The generated URL with query string.

withFragment()

Set the fragment of the URL.

public withFragment(string $fragment) : static
Parameters
$fragment : string

The fragment to be set.

Tags
throws
InvalidArgumentException

If the fragment is not a string.

Return values
static

A new instance of the class with the updated fragment.

withHost()

Set the host of the URL.

public withHost(string $host) : static
Parameters
$host : string

The host to be set.

Return values
static

A new instance of the class with the updated host.

withoutParams()

Remove specified query parameters from the URL.

public withoutParams(string|array<string|int, mixed> $names[, bool $encode = false ]) : static
Parameters
$names : string|array<string|int, mixed>

The names of attributes to be removed.

$encode : bool = false

False (default) to connect query string with "&", true to connect with "&".

Return values
static

A new instance of the class without the specified query parameters.

withoutQuery()

Remove the specified query parameters from the URL.

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

An array of query parameters to be removed (e.g., ['q1' => 'value1', 'q2' => 'value2']).

Return values
static

A new instance of the class without the specified query parameters.

withParams()

Set or update query parameters in the URL.

public withParams(array<string|int, mixed> $params[, bool $encode = false ]) : static
Parameters
$params : array<string|int, mixed>

An array of query parameters to be set or updated.

$encode : bool = false

False (default) to connect query string with "&", true to connect with "&".

Return values
static

A new instance of the class with the updated query parameters.

withPath()

Set the path of the URI.

public withPath(string $path) : static

The path must start with '/' (e.g., '/kotchasan'), or it can be empty if it is the root of the domain.

Parameters
$path : string

The path name.

Return values
static

A new instance of the class with the updated path.

withPort()

Set the port number of the URI.

public withPort(null|int $port) : static
Parameters
$port : null|int

The port number (1-65535) or null.

Tags
throws
InvalidArgumentException

If the port number is invalid.

Return values
static

A new instance of the class with the updated port number.

withQuery()

Set the query string of the URI.

public withQuery(string $query) : static
Parameters
$query : string

The query string.

Tags
throws
InvalidArgumentException

If the query string is invalid.

Return values
static

A new instance of the class with the updated query string.

withScheme()

Set the scheme (http or https) of the URI.

public withScheme(string $scheme) : static
Parameters
$scheme : string

The scheme (http, https, or empty string).

Tags
throws
InvalidArgumentException

If the scheme is not empty, http, or https.

Return values
static

A new instance of the class with the updated scheme.

withUserInfo()

Return a new instance of the class with the specified user information (username and password).

public withUserInfo(string $user[, string $password = null ]) : self
Parameters
$user : string

The username.

$password : string = null

(optional) The password.

Return values
self

A new instance of the class with the updated user information.

createBack()

Convert POST data to a query string for redirecting back to the previous page after form submission.

private createBack(string $url, array<string|int, mixed> $source, array<string|int, mixed> $query_string[, bool $encode = false ]) : string

Returns the URL with the query string.

Parameters
$url : string

The URL to redirect back to, e.g., index.php.

$source : array<string|int, mixed>

The query string from $_POST or $_GET.

$query_string : array<string|int, mixed>

Additional query string parameters in the format array('key' => 'value', ...).

$encode : bool = false

False to concatenate query string with '&', true to use '&'.

Return values
string

The URL with the query string.

createUriString()

Create a URI string.

private static createUriString(string $scheme, string $authority, string $path, string $query, string $fragment) : string

Example: http://domain.tld/

Parameters
$scheme : string
$authority : string
$path : string
$query : string
$fragment : string
Return values
string

The URI string.

filterPath()

Validate the path.

private filterPath(string $path) : string
Parameters
$path : string
Return values
string

The filtered path.

filterPort()

Filter and validate the port number for a URI.

private filterPort(string $scheme, string $host, int|null $port) : int|null
Parameters
$scheme : string

The URI scheme (e.g., http, https).

$host : string

The URI host.

$port : int|null

The port number to be filtered and validated.

Tags
throws
InvalidArgumentException

If the port number is invalid (not within the range of 1 to 65535).

Return values
int|null

The filtered port number, or null if the default port for the given scheme and host is used.

filterQueryFragment()

Check and filter the query and fragment components of a URL.

private filterQueryFragment(string $str) : string
Parameters
$str : string

The string to filter.

Return values
string

The filtered string.

filterScheme()

Check the URL scheme.

private filterScheme(string $scheme) : string
Parameters
$scheme : string

The scheme to check.

Tags
throws
InvalidArgumentException

If the scheme is not empty, "http", or "https".

Return values
string

The filtered scheme.

isNonStandardPort()

Check if the port is a non-standard port for the given scheme and host.

private isNonStandardPort(string $scheme, string $host, int|null $port) : bool

This method determines if the specified scheme and host combination has a non-standard port. It returns true if either the scheme is not 'http' or 'https', or the port is not 80 or 443. Otherwise, it returns false.

Parameters
$scheme : string

The scheme (e.g., http, https).

$host : string

The host.

$port : int|null

The port number.

Return values
bool

True if the port is non-standard, false otherwise.

Search results