Utility
final class Utility (View source)
Holder for various miscellaneous utility function.
Methods
Casts any variable (including objects) to an array recursively.
Expands array wildcards by injecting the necessary keys from the available keys in the passed data.
Retrieves a value from an array via dot notation.
Determines whether the variable or the fallback should be used and returns it.
Injects variables into an array containing placeholders as values.
Injects variables into a string containing placeholders.
Interpolates context values into text placeholders.
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.
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.
NOTE: Keys are allowed to have dots in them. NOTE: Keys with dots in their names have precedence over nested array.
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
.
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
.
static string
injectInString(string $string, array $variables)
Injects variables into a string containing placeholders.
Placeholder must match: /(\$\{(?:@?[a-z0-9]+[a-z0-9_\-\.]*+)(?::.+?)?\})/i
.
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).
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.