Oihana PHP System

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
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterIntProperty()  : int|array<string|int, mixed>
Casts a value (or all elements of an array) to integer.

Methods

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