final class Utility (View source)

Holder for various miscellaneous utility function.

Methods

static array
castToArray(mixed $variable)

Casts any variable (including objects) to an array recursively.

static array
expandArrayWildcards(array $array, array $data)

Expands array wildcards by injecting the necessary keys from the available keys in the passed data.

static mixed
getArrayValue(array $array, string $key, mixed $fallback = null)

Retrieves a value from an array via dot notation.

static mixed
getInjectionVariable(mixed $variable, string|null $fallback)

Determines whether the variable or the fallback should be used and returns it.

static array
injectInArray(array $array, array $variables)

Injects variables into an array containing placeholders as values.

static string
injectInString(string $string, array $variables)

Injects variables into a string containing placeholders.

static string
interpolate(string $text, array $context = [], string $wrapper = '{}')

Interpolates context values into text placeholders.

static string
transform(string $subject, string ...$transformations)

Transforms the case/content of a string by applying a one or more of the 26 available transformations.

Details

static array castToArray(mixed $variable)

Casts any variable (including objects) to an array recursively.

If the variable is an object or has an object as one of its items, the returned array will contain all public, protected, and private properties of the object as an associative array without prefixing.

NOTE: This method is not recursive as in recalling the method as many times as needed. It uses iteration (uses 1 recursion stack frame) and leverages references and pointers instead of using copies, this makes the casting super fast, very efficient and consistent.

Parameters

mixed $variable

The variable to cast to an array.

Return Value

array

static array expandArrayWildcards(array $array, array $data)

Expands array wildcards by injecting the necessary keys from the available keys in the passed data.

Parameters

array $array

The array with keys containing * in their dot notation representation.

array $data

The data to retrieve the wildcards from.

Return Value

array

The final array with the keys expanded.

static mixed getArrayValue(array $array, string $key, mixed $fallback = null)

Retrieves a value from an array via dot notation.

NOTE: Keys are allowed to have dots in them. NOTE: Keys with dots in their names have precedence over nested array.

Parameters

array $array

The array to get the value from. To be able to retrieve object properties (or an object within the passed array), pass the $array to self::castToArray() before passing it to this function.

string $key

The key to the value.

mixed $fallback

The fallback value to return if the key has no value or null.

Return Value

mixed

The actual value or the fallback value.

static private mixed getInjectionVariable(mixed $variable, string|null $fallback)

Determines whether the variable or the fallback should be used and returns it.

The fallback is returned if the $variable is null or '' (empty string); and $fallback is not null.

Parameters

mixed $variable

The variable to use.

string|null $fallback

The fallback to use.

Return Value

mixed

The value to inject.

static array injectInArray(array $array, array $variables)

Injects variables into an array containing placeholders as values.

Placeholder must match: /^((?:@?[a-z0-9]+[a-z0-9_\-\.]*)(?::.+?)?)$/i.

Parameters

array $array

An array containing elements, where their values are strings for a key (in dot notation) in the passed variables. The syntax for the placeholder is @someKey, or @someKey:fallback to provide a fallback value. Fallbacks are used if the value of the provided key does not exist, equals null, or an empty string ''. Note that no type casting is applied when injecting the values.

array $variables

The variables to get the values from to replace placeholders with their corresponding values. To be able to retrieve object properties (or an object within the passed array), pass the $variables to self::castToArray() before passing it to this function.

Return Value

array

The final array with the values injected.

static string injectInString(string $string, array $variables)

Injects variables into a string containing placeholders.

Placeholder must match: /(\$\{(?:@?[a-z0-9]+[a-z0-9_\-\.]*+)(?::.+?)?\})/i.

Parameters

string $string

The string to inject the values into. The placeholders will be simply replaced with their corresponding values. The syntax for the placeholder is {@someKey}, or {@someKey:fallback} to provide a fallback value. Fallbacks are used if the value of the provided key does not exist, equals null, or an empty string ''. Note that type casting is applied when injecting the values (see Serializer::serialize() for the expected results).

array $variables

The variables to get the values from to replace placeholders with their corresponding values. To be able to retrieve object properties (or an object within the passed array), pass the $variables to self::castToArray() before passing it to this function.

Return Value

string

The final string with the variables injected.

static string interpolate(string $text, array $context = [], string $wrapper = '{}')

Interpolates context values into text placeholders.

NOTE: Interpolated values be serialized to the most human readable form (mostly as JSON).

Parameters

string $text

The text to interpolate.

array $context

An associative array like ['varName' => 'varValue'].

string $wrapper

The wrapper that indicate a variable. Max 2 chars or 0 for none, anything else will be ignored and "}" will be used instead.

Return Value

string

The interpolated string.

static string transform(string $subject, string ...$transformations)

Transforms the case/content of a string by applying a one or more of the 26 available transformations.

The transformations are applied in the fixes they are specified. Available transformations:

  • clean: discards all punctuations and meta-characters (@#%&$^*+-=_~:;,.?!()}[]|/\'"`), separates concatenated words [ExampleString-num.1, Example String num 1].
  • alnum: removes every thing other that english letters, numbers and spaces. [Example@123 -> Example123]
  • alpha: removes every thing other that english letters. [Example123 -> Example]
  • numeric: removes every thing other that numbers. [Example123 -> 123]
  • slug: lowercase, all letters to their A-Z representation (transliteration), spaces to dashes, no special characters (URL-safe) [Example (String) -> example-string].
  • title: titlecase [example string -> Example String].
  • sentence: lowercase, first letter uppercase [exampleString -> Example string].
  • pascal: titlecase, no spaces [example string -> ExampleString].
  • camel: titlecase, no spaces, first letter lowercase [example string -> exampleString].
  • dot: lowercase, spaces to dots [Example String -> example.string].
  • kebab: lowercase, spaces to dashes [Example String -> example-string].
  • snake: lowercase, spaces to underscores [Example String -> example_string].
  • constant: uppercase, spaces to underscores [Example String -> EXAMPLE_STRING].
  • cobol: uppercase, spaces to dashes [example string -> EXAMPLE-STRING].
  • train: titlecase, spaces to dashes [example string -> Example-String].
  • lower: lowercase (MB) [Example String -> example string].
  • upper: uppercase (MB) [Example String -> EXAMPLE STRING].
  • spaceless: removes any whitespaces [Example String -> ExampleString].
  • A built-in function name from this list can also be used: strtolower, strtoupper, lcfirst, ucfirst, ucwords, trim, ltrim, rtrim.

NOTE: Unknown transformations will be ignored silently.

NOTE: The subject (string) loses some of its characteristics when a transformation is applied, that means reversing the transformations will not guarantee getting the old subject back.

Parameters

string $subject

The string to transform.

string ...$transformations

One or more transformations to apply.

Return Value

string

The transformed string.