VariableHelper
in package
Helper class for work with variables
Table of Contents
Methods
- ArrayToObject() : mixed
- Converts an array to an object.
- ArrayToTree() : array<string|int, mixed>
- Converts a flat array to a tree structure.
- Bin2Hex() : string
- Converts binary data to a hexadecimal string.
- CallableToString() : string
- Converts a callable to its string representation.
- ChangeArrayKeyCase() : array<string|int, mixed>|null
- Changes the case of array keys.
- ChangeArrayValueCase() : array<string|int, mixed>|null
- Changes the case of array values (keys remain unchanged).
- Coalesce() : mixed
- Coalesces two values, returning the first non-null value.
- ConvertToAssotiative() : array<string|int, mixed>
- Converts a non-associative array into an associative array using specified keys and values.
- Extend() : mixed
- Extends or merges two objects or arrays.
- FromPhpArrayOutput() : array<string|int, mixed>
- Converts a string containing PHP array data into an actual array.
- Hex2Bin() : string
- Converts a hexadecimal string to its binary representation.
- IsArray() : bool
- Checks if a variable is an array.
- IsAssociativeArray() : bool
- Checks if an array is associative.
- IsBool() : bool
- Checks if a variable is a boolean (true or false).
- IsDate() : bool
- Checks if a variable represents a valid date.
- IsEmpty() : bool
- Checks if a variable is empty.
- IsNull() : bool
- Checks if a variable is null.
- IsNumeric() : bool
- Checks if a variable is numeric.
- IsObject() : bool
- Checks if a variable is an object.
- IsObjectFieldsAreEmpty() : bool
- Checks if the fields of an object are empty.
- isSerialized() : bool
- Checks if a string represents serialized data.
- IsSimilar() : bool
- Checks if two values are similar.
- IsString() : bool
- Checks if a variable is a string.
- IsTime() : bool
- Checks if a variable represents a valid time.
- Map() : mixed
- Applies a closure to each element of an input value.
- MixedToArray() : array<string|int, mixed>
- Converts a mixed value to an array.
- ObjectToArray() : array<string|int, mixed>
- Converts an object or an array to an associative array.
- Serialize() : string
- Serializes an object or value into a string representation.
- SplitArrayToParts() : array<string|int, mixed>
- Splits an array into parts of a specified length.
- Sum() : float
- Calculates the sum of values in an array.
- ToJsonFilters() : array<string|int, mixed>
- Converts a nested array or object to a JSON-like structure with filters.
- ToPlane() : array<string|int, mixed>
- Converts a nested array or object to a flat associative array.
- ToString() : string
- Converts an object or value to a formatted string representation.
- UniqueByProperty() : array<string|int, mixed>
- Filters an array to keep only unique elements based on a specified property.
- Unserialize() : mixed
- Unserializes a string representation into a PHP value.
Methods
ArrayToObject()
Converts an array to an object.
public
static ArrayToObject(array<string|int, mixed> $array) : mixed
Parameters
- $array : array<string|int, mixed>
-
The input array to be converted.
Return values
mixed —The resulting object.
ArrayToTree()
Converts a flat array to a tree structure.
public
static ArrayToTree(array<string|int, mixed> $array[, int $parent = 0 ][, string $parentName = 'parent' ][, string $childrenName = 'children' ][, string $keyName = 'id' ]) : array<string|int, mixed>
This function takes an input array and organizes it into a hierarchical tree. Each element in the array represents a node, and the tree is formed based on parent-child relationships defined by specific keys.
Parameters
- $array : array<string|int, mixed>
-
The flat input array to be transformed into a tree.
- $parent : int = 0
-
The default parent ID (usually 0 for the root).
- $parentName : string = 'parent'
-
The key representing the parent ID in the array (default: 'parent').
- $childrenName : string = 'children'
-
The key to store child nodes (default: 'children').
- $keyName : string = 'id'
-
The key representing the unique identifier (default: 'id').
Return values
array<string|int, mixed> —The resulting tree structure.
Bin2Hex()
Converts binary data to a hexadecimal string.
public
static Bin2Hex(string $data) : string
This function takes a binary string as input and returns its hexadecimal representation. Each byte in the input string corresponds to two hexadecimal characters in the output.
Parameters
- $data : string
-
The binary data to be converted.
Return values
string —The hexadecimal representation of the input data.
CallableToString()
Converts a callable to its string representation.
public
static CallableToString(mixed $callable) : string
This function takes a callable (such as a closure or function) and returns its string representation. The resulting string can be used to identify the callable.
Parameters
- $callable : mixed
-
The callable to be converted.
Return values
string —The string representation of the callable.
ChangeArrayKeyCase()
Changes the case of array keys.
public
static ChangeArrayKeyCase(array<string|int, mixed> $array[, int $case = CASE_LOWER ]) : array<string|int, mixed>|null
Parameters
- $array : array<string|int, mixed>
-
The input array.
- $case : int = CASE_LOWER
-
The desired case (CASE_LOWER or CASE_UPPER, default is CASE_LOWER).
Return values
array<string|int, mixed>|null —The modified array with keys in the specified case, or null if input is not an array.
ChangeArrayValueCase()
Changes the case of array values (keys remain unchanged).
public
static ChangeArrayValueCase(array<string|int, mixed> $array[, int $case = CASE_LOWER ]) : array<string|int, mixed>|null
Parameters
- $array : array<string|int, mixed>
-
The input array.
- $case : int = CASE_LOWER
-
The desired case (CASE_LOWER or CASE_UPPER, default is CASE_LOWER).
Return values
array<string|int, mixed>|null —The modified array with value case changed, or null if input is not an array.
Coalesce()
Coalesces two values, returning the first non-null value.
public
static Coalesce(mixed $d, mixed $def) : mixed
This function takes two input values and returns the first non-null value. If both values are null, it returns the default value provided as the second argument.
Parameters
- $d : mixed
-
The primary value to check.
- $def : mixed
-
The default value to use if $d is null.
Return values
mixed —The first non-null value or the default value.
ConvertToAssotiative()
Converts a non-associative array into an associative array using specified keys and values.
public
static ConvertToAssotiative(array<string|int, mixed> $array, mixed $fieldKey[, mixed|null $fieldValue = null ]) : array<string|int, mixed>
This function takes a non-associative input array and constructs an associative array where each element is keyed by a specific field (property). Optionally, you can provide a separate field for the associated values.
Parameters
- $array : array<string|int, mixed>
-
The input non-associative array.
- $fieldKey : mixed
-
The field to use as keys for the associative array.
- $fieldValue : mixed|null = null
-
The field to use as values (optional; defaults to null).
Return values
array<string|int, mixed> —The resulting associative array.
Extend()
Extends or merges two objects or arrays.
public
static Extend(mixed $o1, mixed $o2[, bool $recursive = false ][, bool $emptyAsUnset = false ][, bool $removeEmptyValues = false ]) : mixed
This function combines the properties of two input objects or arrays into a single result. By default, non-empty values from the second object/array overwrite corresponding values in the first object/array. You can choose to merge recursively and handle empty values differently using optional parameters.
Parameters
- $o1 : mixed
-
The first object or array to be extended.
- $o2 : mixed
-
The second object or array containing additional properties.
- $recursive : bool = false
-
Whether to merge recursively (default: false).
- $emptyAsUnset : bool = false
-
Whether to treat empty values as unset (default: false).
- $removeEmptyValues : bool = false
-
Wheret to remove empty values from first array (default: false)
Return values
mixed —The extended or merged result.
FromPhpArrayOutput()
Converts a string containing PHP array data into an actual array.
public
static FromPhpArrayOutput(string $string) : array<string|int, mixed>
This function parses the input string and constructs an associative array based on the PHP array syntax found in the string.
Parameters
- $string : string
-
The input string containing PHP array data.
Return values
array<string|int, mixed> —The resulting associative array.
Hex2Bin()
Converts a hexadecimal string to its binary representation.
public
static Hex2Bin(string $data) : string
This function takes a hexadecimal string as input and returns its binary equivalent. Each pair of hexadecimal characters corresponds to one byte in the output.
Parameters
- $data : string
-
The hexadecimal string to be converted.
Return values
string —The binary representation of the input hexadecimal data.
IsArray()
Checks if a variable is an array.
public
static IsArray(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is an array, false otherwise.
IsAssociativeArray()
Checks if an array is associative.
public
static IsAssociativeArray(array<string|int, mixed> $array) : bool
An associative array is one where the keys are strings or non-sequential integers. For example:
- ['name' => 'John', 'age' => 30] is associative.
- [0 => 'apple', 1 => 'banana'] is not associative.
Parameters
- $array : array<string|int, mixed>
-
The input array to check.
Return values
bool —Returns true if the array is associative, false otherwise.
IsBool()
Checks if a variable is a boolean (true or false).
public
static IsBool(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is a boolean, false otherwise.
IsDate()
Checks if a variable represents a valid date.
public
static IsDate(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable represents a valid date, false otherwise.
IsEmpty()
Checks if a variable is empty.
public
static IsEmpty(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is empty, false otherwise.
IsNull()
Checks if a variable is null.
public
static IsNull(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is null, false otherwise.
IsNumeric()
Checks if a variable is numeric.
public
static IsNumeric(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is numeric, false otherwise.
IsObject()
Checks if a variable is an object.
public
static IsObject(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is an object, false otherwise.
IsObjectFieldsAreEmpty()
Checks if the fields of an object are empty.
public
static IsObjectFieldsAreEmpty(mixed $object) : bool
Parameters
- $object : mixed
-
The object to check.
Return values
bool —True if all fields are empty, false otherwise.
isSerialized()
Checks if a string represents serialized data.
public
static isSerialized(string $v) : bool
This function determines whether the input string is a valid serialized representation.
It checks if the string can be successfully unserialized using PHP's unserialize
function.
Parameters
- $v : string
-
The input string to check.
Return values
bool —Returns true if the string is serialized, false otherwise.
IsSimilar()
Checks if two values are similar.
public
static IsSimilar(mixed $actual, mixed $expected) : bool
This function compares the actual value with the expected value and returns true if they are considered similar. The definition of similarity depends on the context and specific use case.
Parameters
- $actual : mixed
-
The actual value to compare.
- $expected : mixed
-
The expected value for comparison.
Return values
bool —Returns true if the values are similar, false otherwise.
IsString()
Checks if a variable is a string.
public
static IsString(mixed $var) : bool
Parameters
- $var : mixed
-
The variable to check.
Return values
bool —True if the variable is a string, false otherwise.
IsTime()
Checks if a variable represents a valid time.
public
static IsTime(mixed $value) : bool
Parameters
- $value : mixed
-
The variable to check.
Return values
bool —True if the variable represents a valid time, false otherwise.
Map()
Applies a closure to each element of an input value.
public
static Map(mixed $object, Closure|null $closure) : mixed
This function takes an input value (which can be an object, array, or other type) and applies the provided closure to each element. The result is an array or modified value based on the closure's transformation.
Parameters
- $object : mixed
-
The input value to be mapped.
- $closure : Closure|null
-
The closure to apply to each element (optional).
Return values
mixed —The result of applying the closure to each element.
MixedToArray()
Converts a mixed value to an array.
public
static MixedToArray(mixed $object) : array<string|int, mixed>
This function takes a mixed input (which can be an object, scalar, or other type) and converts it into an array representation.
Parameters
- $object : mixed
-
The input value to be converted.
Return values
array<string|int, mixed> —The resulting array representation.
ObjectToArray()
Converts an object or an array to an associative array.
public
static ObjectToArray(object|array<string|int, mixed> $object) : array<string|int, mixed>
Parameters
- $object : object|array<string|int, mixed>
-
The object or array to convert.
Return values
array<string|int, mixed> —An associative array representation of the input.
Serialize()
Serializes an object or value into a string representation.
public
static Serialize(mixed $obj) : string
This function converts the given object or value into a storable string format. Serialization is useful for saving data to files, databases, or transmitting it across different systems.
Parameters
- $obj : mixed
-
The object or value to be serialized.
Return values
string —The serialized representation of the input.
SplitArrayToParts()
Splits an array into parts of a specified length.
public
static SplitArrayToParts(array<string|int, mixed> $array, int $partlength) : array<string|int, mixed>
This function divides the input array into smaller arrays (parts) with a maximum length determined by the second argument. If the array length is not evenly divisible by the specified part length, the last part may be shorter.
Parameters
- $array : array<string|int, mixed>
-
The input array to split.
- $partlength : int
-
The desired length of each part.
Return values
array<string|int, mixed> —An array of smaller arrays (parts).
Sum()
Calculates the sum of values in an array.
public
static Sum(array<string|int, mixed> $array) : float
This function takes an input array and computes the sum of its numeric values.
Parameters
- $array : array<string|int, mixed>
-
The input array containing numeric values.
Return values
float —The total sum of the array values.
ToJsonFilters()
Converts a nested array or object to a JSON-like structure with filters.
public
static ToJsonFilters(array<string|int, mixed>|object $var[, string $prefix = '' ]) : array<string|int, mixed>
This function recursively traverses the input array or object and constructs a filtered representation suitable for JSON serialization. Filters can be applied based on specific criteria.
Parameters
- $var : array<string|int, mixed>|object
-
The input array or object to be transformed.
- $prefix : string = ''
-
An optional prefix to prepend to each filter key.
Return values
array<string|int, mixed> —The resulting JSON-like structure with filters.
ToPlane()
Converts a nested array or object to a flat associative array.
public
static ToPlane(array<string|int, mixed>|object $var[, string $prefix = '' ]) : array<string|int, mixed>
This function recursively traverses the input array or object and flattens it into a one-dimensional associative array. Each key in the resulting array represents a path to the original nested value.
Parameters
- $var : array<string|int, mixed>|object
-
The input array or object to be flattened.
- $prefix : string = ''
-
An optional prefix to prepend to each flattened key.
Return values
array<string|int, mixed> —The resulting flat associative array.
ToString()
Converts an object or value to a formatted string representation.
public
static ToString(mixed $object[, string $spl1 = ' ' ][, string $spl2 = '=' ][, bool $quote = true ][, string $keyPrefix = '' ]) : string
This function takes an input object or value and constructs a string representation using specified separators and formatting options.
Parameters
- $object : mixed
-
The object or value to be converted.
- $spl1 : string = ' '
-
The primary separator (default: ' ').
- $spl2 : string = '='
-
The secondary separator (default: '=').
- $quote : bool = true
-
Whether to enclose values in quotes (default: true).
- $keyPrefix : string = ''
-
An optional prefix for keys (default: '').
Return values
string —The formatted string representation.
UniqueByProperty()
Filters an array to keep only unique elements based on a specified property.
public
static UniqueByProperty(array<string|int, mixed> $array, string $field) : array<string|int, mixed>
This function takes an input array and removes duplicate elements based on the value of a specific property (field). The resulting array contains only unique elements with distinct property values.
Parameters
- $array : array<string|int, mixed>
-
The input array to filter.
- $field : string
-
The name of the property to use for uniqueness comparison.
Return values
array<string|int, mixed> —The filtered array with unique elements based on the specified property.
Unserialize()
Unserializes a string representation into a PHP value.
public
static Unserialize(string $string) : mixed
This function takes a serialized string and converts it back into its original PHP data structure. It reconstructs objects, arrays, and other complex types.
Parameters
- $string : string
-
The serialized string to be unserialized.
Tags
Return values
mixed —The unserialized PHP value (bool, int, float, string, array, or object).