AlterIntPropertyTrait
Casts a value (or all elements in an array) to integer.
This method is typically used in property alteration pipelines
to ensure that the given value is strictly an integer type.
If the value is already an integer, it is returned unchanged and
the $modified flag remains false
.
- If the value is an array, each element is individually cast to integer.
- If the value is not an integer, it is cast using intval().
The $modified
flag will be set to true
whenever at least one
value is cast to integer (i.e., when its original type was not already int
).
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 (int)
// $modified === true
// Casting an array of values
$values = $product->alterIntProperty(["10", "20", "30"], $modified);
// $values === [10, 20, 30] (all int)
// $modified === true
// Already an int
$count = $product->alterIntProperty(5, $modified);
// $count === 5 (int)
// $modified === false
Table of Contents
Methods
- alterIntProperty() : array<string|int, mixed>|int
- Cast a value to integer.
Methods
alterIntProperty()
Cast a value to integer.
public
alterIntProperty(mixed $value[, bool &$modified = false ]) : array<string|int, mixed>|int
If the value is an array, all elements in the array are casted.
Property::PRICE => [ Alter::INT ] ,
Parameters
- $value : mixed
- $modified : bool = false