Oihana PHP System

AlterArrayPropertyTrait uses trait:short, trait:short, trait:short

Table of Contents

Methods

alterArrayElements()  : array<string|int, mixed>
Alters all elements in an array.
alterArrayProperty()  : array<string|int, mixed>
Transform a string expression separated by semi-colon ';' to creates an array.
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()

Alters all elements in an array.

public alterArrayElements(array<string|int, mixed> $array[, array<string|int, mixed> $options = [] ][, Container|null $container = null ]) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

The array reference to alter.

$options : array<string|int, mixed> = []

The options representation.

$container : Container|null = null

An optional DI container reference.

Tags
throws
ContainerExceptionInterface
throws
DependencyException
throws
NotFoundException
throws
NotFoundExceptionInterface
throws
ReflectionException
Return values
array<string|int, mixed>

alterArrayProperty()

Transform a string expression separated by semi-colon ';' to creates an array.

public alterArrayProperty(mixed $value[, array<string|int, mixed> $options = [] ][, Container|null $container = null ][, bool &$modified = false ]) : array<string|int, mixed>

You can chain multiple alter definition to transform the content of the array, ex:

Property::CATEGORY => [ Alter::ARRAY , Alter::CLEAN , Alter::JSON_PARSE ] ,

The previous example transform the 'category' string in an Array and after remove all null or empty array elements and JSON parse all elements.

Parameters
$value : mixed
$options : array<string|int, mixed> = []
$container : Container|null = null

An optional DI container reference.

$modified : bool = false
Tags
throws
ContainerExceptionInterface
throws
DependencyException
throws
NotFoundException
throws
NotFoundExceptionInterface
throws
ReflectionException
Return values
array<string|int, mixed>

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
$modified : bool = false

Output flag set to true if the callable altered the value.

Tags
throws
InvalidArgumentException

If the callable is a string but cannot be resolved.

example
$definition = [
    fn($value, $factor) => $value * $factor,
    2
];

$value = 10;
$value = $this->alterCallableProperty($value, $definition, $modified);

// $value    = 20
// $modified = true
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 $value is already a float → returned as is, not marked modified.
  • If $value is an array → each element converted via floatval().
  • Otherwise → $value is cast to float.
Parameters
$value : mixed

The value to convert. Can be scalar or array.

$modified : bool = false

Output flag set to true if a cast was performed.

Tags
example
$value = '12.5';
$new   = $this->alterFloatProperty($value, $modified);

// $new      = 12.5
// $modified = true
example

Array usage

$value = ['1.2', '3.4', 5];
$new   = $this->alterFloatProperty($value, $modified);

// $new      = [1.2, 3.4, 5.0]
// $modified = true
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 $value is an array, each element is individually cast using intval().
  • If $value is already an integer, it is returned unchanged and $modified remains false.
  • If $value is not an integer, it is cast to integer and $modified becomes true.

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\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.


        
On this page

Search results