AlterArrayPropertyTrait uses trait:short, trait:short, trait:short
Turns a value into an array and optionally alters each of its elements.
This alteration is declared with the Alter::ARRAY type. It is the entry point of the
"array" branch of a property-transformation pipeline: a semicolon-separated string is
first exploded into an array, then a chain of nested alter definitions can be applied to
every element in turn. This makes it possible to declare a single rule that both builds
the array and normalizes its content:
Property::CATEGORY => [ Alter::ARRAY , Alter::CLEAN , Alter::JSON_PARSE ] ,
The example above splits the category string into an array, removes null/empty
elements, then JSON-decodes every remaining element.
The trait composes the element-level alters it can delegate to (AlterCallablePropertyTrait, AlterFloatPropertyTrait, AlterIntPropertyTrait) and dispatches the others (Alter::CLEAN, Alter::GET, Alter::HYDRATE, Alter::NORMALIZE, Alter::NOT, Alter::JSON_PARSE) through alterArrayElements().
The $modified flag is always set to true by alterArrayProperty() because the
value is unconditionally coerced to an array.
Tags
Table of Contents
Methods
- alterArrayElements() : array<string|int, mixed>
- Applies a chain of element-level alterations to every item of an array.
- alterArrayProperty() : array<string|int, mixed>
- Transforms a value into an array and applies an optional chain of element alterations.
- alterCallableProperty() : mixed
- Alters a value by invoking a user-defined callable.
- alterFloatProperty() : float|array<string|int, mixed>
- Casts a value (or every element of an array) to float.
- alterIntProperty() : int|array<string|int, mixed>
- Casts a value (or all elements of an array) to integer.
Methods
alterArrayElements()
Applies a chain of element-level alterations to every item of an array.
public
alterArrayElements(array<string|int, mixed> $array[, array<string|int, mixed> $options = [] ][, Container|null $container = null ]) : array<string|int, mixed>
Each entry of $options is either a bare Alter::* type or an array shaped as
[ type , ...params ] where the extra parameters are forwarded to the corresponding
element alter. The supported types are CALL, CLEAN, FLOAT, GET, HYDRATE,
NORMALIZE, NOT, INT and JSON_PARSE; any unknown type leaves the array
untouched. The alterations are applied sequentially, each operating on the result of
the previous one. If either the array or the option list is empty, the array is
returned unchanged.
Parameters
- $array : array<string|int, mixed>
-
The array whose elements must be altered.
- $options : array<string|int, mixed> = []
-
An ordered list of alter definitions, each a bare
Alter::*type or an array[ type , ...params ]. - $container : Container|null = null
-
An optional DI container, forwarded to alters that resolve services (notably
Alter::GET).
Tags
Return values
array<string|int, mixed> —The array after all element-level alterations have been applied.
alterArrayProperty()
Transforms a value into an array and applies an optional chain of element alterations.
public
alterArrayProperty(mixed $value[, array<string|int, mixed> $options = [] ][, Container|null $container = null ][, bool &$modified = false ]) : array<string|int, mixed>
A non-empty string is split on the semicolon (;) separator. The resulting array (or
the value itself when it is already an array) is then passed to alterArrayElements()
so that each chained alter definition in $options is applied to every element. Any value
that is neither a string nor an array becomes an empty array.
Parameters
- $value : mixed
-
The value to convert. A semicolon-separated string is exploded; an array is used as-is; anything else yields
[]. - $options : array<string|int, mixed> = []
-
An ordered list of element-level alter definitions to apply to each item (e.g.
[ Alter::CLEAN , Alter::INT ]). Each entry is either anAlter::*type or an array[ type , ...params ]. - $container : Container|null = null
-
An optional DI container, forwarded to element alters that need service resolution (e.g.
Alter::GET). - $modified : bool = false
-
Reference flag; always set to
truebecause the value is coerced to an array.
Tags
Return values
array<string|int, mixed> —The resulting array after splitting and element-level alterations.
alterCallableProperty()
Alters a value by invoking a user-defined callable.
public
alterCallableProperty(mixed $value[, array<string|int, mixed> $definition = [] ][, bool &$modified = false ]) : mixed
This method allows transforming a property value using any callable (closure, function name, static method, service callback, etc.). It is typically used when an alteration rule is defined as:
[
Alter::CALLABLE,
$callable,
...$additionalParams
]
The first element of $definition must be a callable or a string
resolvable to a callable via resolveCallable().
The callable will be invoked with the following signature:
function (mixed $value, mixed ...$additionalParams): mixed
Any additional elements from $definition will be forwarded as extra arguments.
If the callable returns a different value, $modified will be set to true.
Parameters
- $value : mixed
-
The current value of the property to be altered.
- $definition : array<string|int, mixed> = []
-
The alteration definition:
- index
0: the callable (closure, function name, or resolvable string) - index
1+: optional parameters passed to the callable
- index
- $modified : bool = false
-
Output flag set to
trueif the callable altered the value.
Tags
Return values
mixed —The altered value returned by the callable, or the original value if no callable was provided or callable resolution failed.
alterFloatProperty()
Casts a value (or every element of an array) to float.
public
alterFloatProperty(mixed $value[, bool &$modified = false ]) : float|array<string|int, mixed>
This method ensures that the resulting value is always of type float
(or an array of floats). It also sets $modified to true when any
transformation occurs.
Rules:
- If
$valueis already a float → returned as is, not marked modified. - If
$valueis an array → each element converted viafloatval(). - Otherwise →
$valueis cast to float.
Parameters
- $value : mixed
-
The value to convert. Can be scalar or array.
- $modified : bool = false
-
Output flag set to
trueif a cast was performed.
Tags
Return values
float|array<string|int, mixed> —The float-cast value or an array of float-cast values.
alterIntProperty()
Casts a value (or all elements of an array) to integer.
public
alterIntProperty(mixed $value[, bool &$modified = false ]) : int|array<string|int, mixed>
This alteration is typically used within property transformation
pipelines to ensure that the resulting value is strictly of type int.
Behavior:
- If
$valueis an array, each element is individually cast using intval(). - If
$valueis already an integer, it is returned unchanged and$modifiedremainsfalse. - If
$valueis not an integer, it is cast to integer and$modifiedbecomestrue.
The $modified flag will be set to true whenever at least one casting
occurs (i.e., when the original value or any array element was not already
an integer).
Example:
use oihana\models\traits\alters\AlterIntPropertyTrait;
class Product {
use AlterIntPropertyTrait;
}
$product = new Product();
$modified = false;
// Casting a single value
$id = $product->alterIntProperty("42", $modified);
// $id === 42
// $modified === true
// Casting an array of values
$values = $product->alterIntProperty(["10", "20", "30"], $modified);
// $values === [10, 20, 30]
// $modified === true
// Already an int
$count = $product->alterIntProperty(5, $modified);
// $count === 5
// $modified === false
Parameters
- $value : mixed
-
The input value, scalar or array.
- $modified : bool = false
-
Reference flag indicating if any cast occurred.
Return values
int|array<string|int, mixed> —The cast integer or array of integers.