Encode

Reference for the Encode module: safely encode a value for a given output context - HTML, a URL, JSON, a SQL identifier or number, or a path segment. Every function with its full signature, parameters and return type.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • Encode - static functions that encode a value for a given output context.
  • EncodeError - thrown when a value cannot be encoded to JSON.

Encode

Static functions that take a value and return it encoded for a particular output context, listed alphabetically. Each converts its input with the To helpers before encoding, and several run a Security check on the input first.

Html

Encode::Html($text): string

Encode a value as text for HTML output, escaping special characters with htmlspecialchars.

NameTypeDescription
$textmixedThe value to encode; converted to a string first.

Returns string The HTML-escaped text.

HtmlNum

Encode::HtmlNum($value): string

Encode a value as a number for HTML output.

NameTypeDescription
$valuemixedThe value to encode; converted to a number first.

Returns string The HTML-escaped number.

HtmlUrl

Encode::HtmlUrl($text): string

URL-encode a value and then HTML-escape it, for a URL placed in HTML.

NameTypeDescription
$textmixedThe value to encode; asserted to be a string, then converted to a string.

Returns string The URL-encoded, HTML-escaped text.

HtmlUrlArray

Encode::HtmlUrlArray($array): string

Build a URL query string from an array and HTML-escape it.

NameTypeDescription
$arrayarrayThe name-to-value pairs; security-checked as an array first.

Returns string The HTML-escaped query string.

Json

Encode::Json(mixed $data, bool $pretty = false): string

Encode a value as JSON, dropping null values, and optionally pretty-printing.

NameTypeDescription
$datamixedThe value to encode; objects are cast to arrays and null values are removed recursively.
$prettyboolWhen true, format the output with indentation. Defaults to false.

Returns string The JSON text. Throws EncodeError when the value cannot be encoded.

JsonBoolean

Encode::JsonBoolean($data): string

Encode a value as a JSON boolean.

NameTypeDescription
$datamixedThe value to encode; converted to true or false first.

Returns string The JSON boolean.

JsonNum

Encode::JsonNum($data, $min = null, $max = null): string

Encode a value as a JSON number, clamped to an optional range.

NameTypeDescription
$datamixedThe value to encode; converted to a number first.
$minmixedThe minimum allowed value, or null for no minimum. Defaults to null.
$maxmixedThe maximum allowed value, or null for no maximum. Defaults to null.

Returns string The JSON number.

JsonStr

Encode::JsonStr($data): string

Encode a value as a JSON string.

NameTypeDescription
$datamixedThe value to encode; converted to a string first.

Returns string The JSON string.

PathDate

Encode::PathDate($date): string

Encode a value as a date for use in a path.

NameTypeDescription
$datemixedThe value to encode; security-checked as a date, then converted to a date.

Returns string The date as a path segment.

PathID

Encode::PathID($id): int

Encode a value as an id for use in a path.

NameTypeDescription
$idmixedThe value to encode; security-checked as an id, then converted to an id.

Returns int The id.

SqlDatabaseName

Encode::SqlDatabaseName($text): string

Encode a value as a SQL database name.

NameTypeDescription
$textmixedThe value to encode; security-checked as a database name, then converted to a database name.

Returns string The database name.

SqlID

Encode::SqlID($id): int

Encode a value as a SQL id.

NameTypeDescription
$idmixedThe value to encode; converted to an id.

Returns int The id.

SqlNum

Encode::SqlNum($id): int

Encode a value as a SQL number.

NameTypeDescription
$idmixedThe value to encode; converted to a number.

Returns int The number.

SqlNumArray

Encode::SqlNumArray(array $array): array

Encode every element of an array as a SQL number.

NameTypeDescription
$arrayarrayThe values to encode; each element is converted to a number.

Returns array The list of encoded numbers.

Url

Encode::Url($text): string

URL-encode a value with urlencode.

NameTypeDescription
$textmixedThe value to encode; converted to a string first.

Returns string The URL-encoded text.

UrlArray

Encode::UrlArray($array): string

Build a URL query string from an array, URL-encoding each name and value and joining them with ampersands.

NameTypeDescription
$arrayarrayThe name-to-value pairs; security-checked as an array first.

Returns string The query string.

UrlID

Encode::UrlID($value): int

URL-encode a value as an id.

NameTypeDescription
$valuemixedThe value to encode; converted to an id, then URL-encoded.

Returns int The URL-encoded id.

UrlNum

Encode::UrlNum($value): string

URL-encode a value as a number.

NameTypeDescription
$valuemixedThe value to encode; converted to a number, then URL-encoded.

Returns string The URL-encoded number.

EncodeError

An exception thrown by Encode::Json when a value cannot be encoded to JSON. Extends the built-in Exception and adds no members of its own.