Oihana PHP System

AlterFloatPropertyTrait

Provides a standardized way to cast a property value to `float` within the AlterDocument system.

This alteration is typically declared as:

'price' => Alter::FLOAT

or inside a chained definition:

'prices' => [ Alter::ARRAY, Alter::FLOAT ]

Behavior details:

  • If the input is already a float, it is returned unchanged and $modified remains false.
  • If the input is an array, each element is converted using floatval().
  • If the input is any scalar (string, int, bool), it is cast using floatval().
  • Non-scalar, non-array values should generally not occur, but if they do, PHP's floatval() semantics apply.

This alteration is useful for ensuring numeric consistency when working with user input, JSON data, APIs, DTO hydration, or internal normalization.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterFloatProperty()  : float|array<string|int, mixed>
Casts a value (or every element of an array) to float.

Methods

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.


        
On this page

Search results