LanguageUtility
Language utility.
Table of Contents
- convertArray() : array<string|int, mixed>
- Convert elements of an array with a given converter and generate a new array.
- convertArrayCopyableToArray() : array<string|int, mixed>
- Convert an ArrayCopyable instance to an array.
- convertArrayCopyableToJson() : string
- Convert an ArrayCopyable instance to a JSON string.
- convertArrayOfArrayCopyableToArray() : array<string|int, mixed>
- Convert an array whose elements implement the ArrayCopyable interface to an array.
- convertArrayToArrayCopyable() : mixed
- Convert an array to an object.
- convertArrayToArrayOfArrayCopyable() : array<string|int, mixed>
- Convert an array to an array whose elements implement the ArrayCopyable interface.
- convertArrayToStringArray() : array<string|int, string>
- Convert an array to a string array.
- convertJsonToArrayCopyable() : mixed
- Convert a JSON string to an object.
- getFromArray() : mixed
- Get an element identified by a key from an array.
- getFromArrayAsBoolean() : bool
- Get an element identified by a key from an array as boolean
- getFromEnv() : string
- Get the value of the environment variable identified by the key.
- initializeClass() : mixed
- Call initialize() method of the specified class.
- orEmpty() : mixed
- Get the given object itself or an empty string if the object is `null`.
- orZero() : mixed
- Get the given object itself or 0 if the object is `null`.
- parseBoolean() : bool
- Parse the given object as boolean.
- parseInteger() : int
- Parse the given object as integer.
- toString() : string
- Get the string value of the given object.
Methods
convertArray()
Convert elements of an array with a given converter and generate a new array.
public
static convertArray([array<string|int, mixed> &$array = null ], callable $converter[, mixed $arg = null ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed> = null
-
A reference to an array.
- $converter : callable
-
A function that converts an element to another object. When
$arg
isnull
,$converter
should be a function that takes one argument (an element). When$arg
is notnull
,$converter
should be a function that takes two arguments, an element and$arg
. - $arg : mixed = null
-
An optional argument given to the converter.
Return values
array<string|int, mixed> —A reference of a new array that holds converted elements.
convertArrayCopyableToArray()
Convert an ArrayCopyable instance to an array.
public
static convertArrayCopyableToArray([ArrayCopyable $object = null ]) : array<string|int, mixed>
Parameters
- $object : ArrayCopyable = null
-
An object that implements the
ArrayCopyable
interface.copyToArray()
method of the object will be called.
Return values
array<string|int, mixed> —An array generated from the given object.
convertArrayCopyableToJson()
Convert an ArrayCopyable instance to a JSON string.
public
static convertArrayCopyableToJson([ArrayCopyable $object = null ], int $options) : string
Parameters
- $object : ArrayCopyable = null
-
An object that implements the
ArrayCopyable
interface.copyToArray()
method of the object will be called. - $options : int
-
Options passed
json_encode()
.
Return values
string —A JSON string generated from the given object.
convertArrayOfArrayCopyableToArray()
Convert an array whose elements implement the ArrayCopyable interface to an array.
public
static convertArrayOfArrayCopyableToArray([array<string|int, mixed> &$array = null ]) : array<string|int, mixed>
Each element in the given array will be converted to an array by
LanguageUtility::convertArrayCopyableToArray()
.
Parameters
- $array : array<string|int, mixed> = null
-
A reference to an array whose elements implement the
ArrayCopyable
interface.
Return values
array<string|int, mixed> —An array of arrays.
convertArrayToArrayCopyable()
Convert an array to an object.
public
static convertArrayToArrayCopyable([array<string|int, mixed> &$array = null ], string $className) : mixed
Parameters
- $array : array<string|int, mixed> = null
-
A reference to an array.
- $className : string
-
A name of a class that implements the
ArrayCopyable
interface. An instance of the class will be created and itscopyFromArray()
method will be called.
Return values
mixed —An instance of the class which is specified by the class name.
convertArrayToArrayOfArrayCopyable()
Convert an array to an array whose elements implement the ArrayCopyable interface.
public
static convertArrayToArrayOfArrayCopyable([array<string|int, mixed> &$array = null ], string $className) : array<string|int, mixed>
Each element inthe given array will be converted to an object that
implements the ArrayCopyable
interface by
LanguageUtility::convertArrayToArrayCopyable()
.
Parameters
- $array : array<string|int, mixed> = null
-
A reference to an array of arrays.
- $className : string
-
A name of a class that implements the
ArrayCopyable
interface.
Return values
array<string|int, mixed> —An array of objects that implement the ArrayCopyable
interface.
convertArrayToStringArray()
Convert an array to a string array.
public
static convertArrayToStringArray([array<string|int, mixed> &$array = null ]) : array<string|int, string>
Elements in the given array will be converted to string by
LanguageUtility::toString()
method.
Parameters
- $array : array<string|int, mixed> = null
-
A reference to an array.
Return values
array<string|int, string> —A string array.
convertJsonToArrayCopyable()
Convert a JSON string to an object.
public
static convertJsonToArrayCopyable(string $json, string $className) : mixed
Parameters
- $json : string
-
A JSON string.
- $className : string
-
A name of a class that implements the
ArrayCopyable
interface. An instance of the class will be created and itscopyFromArray()
method will be called.
Return values
mixed —An instance of the class which is specified by the class name.
getFromArray()
Get an element identified by a key from an array.
public
static getFromArray(string $key, array<string|int, mixed> &$array) : mixed
Parameters
- $key : string
-
Key of an element.
- $array : array<string|int, mixed>
-
Reference to an array.
Return values
mixed —An element identifed by the key. If the key does not exist
in the array, null
is returned.
getFromArrayAsBoolean()
Get an element identified by a key from an array as boolean
public
static getFromArrayAsBoolean(string $key, array<string|int, mixed> &$array) : bool
Parameters
- $key : string
-
Key of an element.
- $array : array<string|int, mixed>
-
Reference to an array.
Tags
Return values
bool —A boolean value generated by parsing the element identified
by the key. If the key does not exist in the array, null
is returned.
getFromEnv()
Get the value of the environment variable identified by the key.
public
static getFromEnv(string $key) : string
Parameters
- $key : string
-
The name of an environment variable.
Return values
string —The value of the environment variable. If the environment
variable is not defined, null
is returned.
initializeClass()
Call initialize() method of the specified class.
public
static initializeClass(string $class) : mixed
This method assumes that the specified class has a static method
named initialize()
. Even if the initialize()
method is
private
, this method calls it via reflection.
Parameters
- $class : string
-
Class name.
Return values
mixed —orEmpty()
Get the given object itself or an empty string if the object is `null`.
public
static orEmpty(mixed $value) : mixed
Parameters
- $value : mixed
-
An object.
Return values
mixed —The given object itself or an empty string.
orZero()
Get the given object itself or 0 if the object is `null`.
public
static orZero(mixed $value) : mixed
Parameters
- $value : mixed
-
An object.
Return values
mixed —The given object itself or 0.
parseBoolean()
Parse the given object as boolean.
public
static parseBoolean(mixed $value) : bool
Parameters
- $value : mixed
-
An object.
Tags
Return values
bool —If the given object is null
, false
is returned.
Otherwise, if the type of the given object is boolean
,
the given object itself is returned. Otherwise, if the
type of the given object is not string
, an
InvalidArgumentException
is thrown. Otherwise, this
method compares the given string to "true" in a
case-insensitive manner. If they match, this method
returns true
. Otherwise, false
is returned.
parseInteger()
Parse the given object as integer.
public
static parseInteger(mixed $value) : int
Parameters
- $value : mixed
-
An object.
Tags
Return values
int —If the given object is null
, 0 is returned. Otherwise, if
the type of the given object is integer
, the given object
itself is returned. Otherwise, if the type of the given
object is not string
, an InvalidArgumentException
is
thrown. Otherwise, this method returns intval($value)
.
toString()
Get the string value of the given object.
public
static toString(mixed $value) : string
Parameters
- $value : mixed
-
An object.
Return values
string —If the given $value
is null
or a string
object,
the $value
itself is returned. Otherwise, if it is
a boolean object, "true"
or "false"
is returned.
In other cases, strval($value)
is returned.