Documentation

Utils
in package

Table of Contents

Methods

canonicalizePath()  : string
Makes sure if a relative path is passed in it is turned into an absolute path
detectAndCleanUtf8()  : mixed
Detect invalid UTF-8 string characters and convert to valid UTF-8.
handleJsonError()  : string
Handle a json_encode failure.
jsonEncode()  : string
Return the JSON representation of a value
throwEncodeError()  : mixed
Throws an exception according to a given code with a customized message

Methods

canonicalizePath()

Makes sure if a relative path is passed in it is turned into an absolute path

public static canonicalizePath(string $streamUrl) : string
Parameters
$streamUrl : string

stream URL or path without protocol

Return values
string

detectAndCleanUtf8()

Detect invalid UTF-8 string characters and convert to valid UTF-8.

public static detectAndCleanUtf8(mixed &$data) : mixed

Valid UTF-8 input will be left unmodified, but strings containing invalid UTF-8 codepoints will be reencoded as UTF-8 with an assumed original encoding of ISO-8859-15. This conversion may result in incorrect output if the actual encoding was not ISO-8859-15, but it will be clean UTF-8 output and will not rely on expensive and fragile detection algorithms.

Function converts the input in place in the passed variable so that it can be used as a callback for array_walk_recursive.

Parameters
$data : mixed

Input to check and convert if needed, passed by ref

Tags
private

handleJsonError()

Handle a json_encode failure.

public static handleJsonError(int $code, mixed $data[, int $encodeFlags = null ]) : string

If the failure is due to invalid string encoding, try to clean the input and encode again. If the second encoding attempt fails, the inital error is not encoding related or the input can't be cleaned then raise a descriptive exception.

Parameters
$code : int

return code of json_last_error function

$data : mixed

data that was meant to be encoded

$encodeFlags : int = null

flags to pass to json encode, defaults to JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE

Tags
throws
RuntimeException

if failure can't be corrected

Return values
string

JSON encoded data after error correction

jsonEncode()

Return the JSON representation of a value

public static jsonEncode(mixed $data[, int $encodeFlags = null ][, bool $ignoreErrors = false ]) : string
Parameters
$data : mixed
$encodeFlags : int = null

flags to pass to json encode, defaults to JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE

$ignoreErrors : bool = false

whether to ignore encoding errors or to throw on error, when ignored and the encoding fails, "null" is returned which is valid json for null

Tags
throws
RuntimeException

if encoding fails and errors are not ignored

Return values
string

throwEncodeError()

Throws an exception according to a given code with a customized message

private static throwEncodeError(int $code, mixed $data) : mixed
Parameters
$code : int

return code of json_last_error function

$data : mixed

data that was meant to be encoded

Tags
throws
RuntimeException

        
On this page

Search results