Misc
in package
A class that serves as a holder for various miscellaneous utility function.
Tags
Table of Contents
- backtrace() : string|int|array<string|int, mixed>|null
- Returns the passed key(s) from the backtrace.
- callObjectMethod() : mixed
- Calls a private, protected, or public method on an object.
- cutArrayValueByKey() : mixed
- Cuts a value of an array via dot-notation, the value will be returned and the key will be unset from the array.
- getArrayValueByKey() : mixed
- Gets a value from an array via dot-notation.
- getObjectProperty() : mixed
- Gets a private, protected, or public property (default, static, or constant) of an object.
- interpolate() : string
- Interpolates context values into text placeholders.
- setArrayValueByKey() : bool
- Sets a value of an array via dot-notation.
- setObjectProperty() : mixed
- Sets a private, protected, or public property (default or static) of an object.
- transform() : string
- Transforms the case/content of a string by applying a one or more of the 26 available transformations.
Methods
backtrace()
Returns the passed key(s) from the backtrace.
public
static backtrace([null|string|array<string|int, string> $pluck = null ], int $offset) : string|int|array<string|int, mixed>|null
Parameters
- $pluck : null|string|array<string|int, string> = null
-
[optional] $pluck The key to to get as a string or an array of strings (keys) from this list
[file, line, function, class, type, args]
, passingnull
will return the entire backtrace. - $offset : int
-
[optional] The offset of the backtrace, passing
-1
will reference the last item in the backtrace.
Return values
string|int|array<string|int, mixed>|null —A string or int if a string is passed, an array if an array or null is passed, and null if no match was found.
callObjectMethod()
Calls a private, protected, or public method on an object.
public
static callObjectMethod(object|string $object, string $method, mixed ...$arguments) : mixed
Parameters
- $object : object|string
-
Class instance, or FQN if static.
- $method : string
-
Method name.
- $arguments : mixed
Tags
Return values
mixed —The function result, or false on error.
cutArrayValueByKey()
Cuts a value of an array via dot-notation, the value will be returned and the key will be unset from the array.
public
static cutArrayValueByKey(array<string|int, mixed> &$array, string $key[, mixed $default = null ]) : mixed
Parameters
- $array : array<string|int, mixed>
-
The array to cut the value from.
- $key : string
-
The dotted key representation.
- $default : mixed = null
-
[optional] The default fallback value.
Return values
mixed —The requested value if found otherwise the default parameter.
getArrayValueByKey()
Gets a value from an array via dot-notation.
public
static getArrayValueByKey(array<string|int, mixed> &$array, string $key[, mixed $default = null ]) : mixed
Parameters
- $array : array<string|int, mixed>
-
The array to get the value from.
- $key : string
-
The dotted key representation.
- $default : mixed = null
-
[optional] The default fallback value.
Return values
mixed —The requested value if found otherwise the default parameter.
getObjectProperty()
Gets a private, protected, or public property (default, static, or constant) of an object.
public
static getObjectProperty(object|string $object, string $property) : mixed
Parameters
- $object : object|string
-
Class instance, or FQN if static.
- $property : string
-
Property name.
Tags
Return values
mixed —The property value.
interpolate()
Interpolates context values into text placeholders.
public
static interpolate(string $text[, array<string|int, mixed> $context = [] ][, string $placeholderIndicator = '{}' ]) : string
Parameters
- $text : string
-
The text to interpolate.
- $context : array<string|int, mixed> = []
-
An associative array like
['varName' => 'varValue']
. - $placeholderIndicator : string = '{}'
-
The wrapper that indicate a variable. Max 2 chars, anything else will be ignored and "}" will be used instead.
Return values
string —The interpolated string.
setArrayValueByKey()
Sets a value of an array via dot-notation.
public
static setArrayValueByKey(array<string|int, mixed> &$array, string $key, mixed $value) : bool
Parameters
- $array : array<string|int, mixed>
-
The array to set the value in.
- $key : string
-
The dotted key representation.
- $value : mixed
-
The value to set.
Return values
bool —True on success.
setObjectProperty()
Sets a private, protected, or public property (default or static) of an object.
public
static setObjectProperty(object|string $object, string $property, mixed $value) : mixed
Parameters
- $object : object|string
-
Class instance, or FQN if static.
- $property : string
-
Property name.
- $value : mixed
-
Property value.
Tags
Return values
mixed —The new property value.
transform()
Transforms the case/content of a string by applying a one or more of the 26 available transformations.
public
static transform(string $subject, string ...$transformations) : string
The transformations are applied in the order 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
]. -
lower
: lowercase [Example String
->example string
]. -
upper
: uppercase [Example String
->EXAMPLE STRING
]. -
pascal
: titlecase, no spaces [example string
->ExampleString
]. -
camel
: titlecase, no spaces, first letter lowercase [example string
->exampleString
]. -
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
]. -
snake
: lowercase, spaces to underscores [Example String
->example_string
]. -
kebab
: lowercase, spaces to dashes [Example String
->example-string
]. -
dot
: lowercase, spaces to dots [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
- $subject : string
-
The string to transform.
- $transformations : string
-
One or more transformations to apply.
Return values
string —The transformed string.