Oihana PHP

AlterNormalizePropertyTrait

Provides a method to normalize a property value according to configurable cleaning flags.

This trait is typically used in alteration pipelines to ensure that values are cleaned, trimmed, and standardized before being stored or further processed.

The normalization behavior is controlled via CleanFlag constants:

  • By default, CleanFlag::DEFAULT | CleanFlag::RETURN_NULL is applied.
  • Custom flags can be provided as the first element of the $definition array.

Normalization handles:

  • Arrays recursively, removing empty values, nulls, or falsy values according to flags.
  • Strings by trimming and optionally converting empty strings to null.
  • Scalars are generally returned as-is unless CleanFlag::FALSY is used.
  • Objects are returned as-is unless empty stdClass and CleanFlag::RETURN_NULL is set.

The $modified flag is set to true if the resulting value differs from the original.

Tags
see
normalize

For the underlying normalization logic.

CleanFlag

For the enumeration of cleaning modes.

example
use oihana\models\traits\alters\AlterNormalizePropertyTrait;
use oihana\core\arrays\CleanFlag;

class Product
{
    use AlterNormalizePropertyTrait;
}

$product = new Product();
$modified = false;

// Default normalization
$value = $product->alterNormalizeProperty(['', ' foo ', null], [], $modified);
// Result: ['foo']
// $modified === true

// Custom flags
$value = $product->alterNormalizeProperty('   ', [CleanFlag::TRIM | CleanFlag::EMPTY], $modified);
// Result: null
// $modified === true

// Scalars remain unchanged if not affected by flags
$value = $product->alterNormalizeProperty(42, [], $modified);
// Result: 42
// $modified === false
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterNormalizeProperty()  : mixed
Normalize a document property using configurable flags.

Methods

alterNormalizeProperty()

Normalize a document property using configurable flags.

public alterNormalizeProperty(mixed $value[, array<string|int, mixed> $definition = [] ][, bool &$modified = false ]) : mixed

The normalization can be customized via the $definition array:

  • If empty or no flags provided, uses CleanFlag::DEFAULT | CleanFlag::RETURN_NULL
  • If a flags value is provided at index 0, uses that instead
Parameters
$value : mixed

The value to normalize (array, string, scalar or object).

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

Optional flags array [ CleanFlag bitmask , ...other params ]. When omitted, CleanFlag::DEFAULT | CleanFlag::RETURN_NULL is used.

$modified : bool = false

Reference flag set to true when the normalized value differs from the original.

Tags
example
// Use default flags
$this->alterNormalizeProperty( $value );
// Uses: CleanFlag::DEFAULT | CleanFlag::RETURN_NULL

// Use custom flags
$this->alterNormalizeProperty($value, [ CleanFlag::NULLS | CleanFlag::EMPTY ] );

// Only remove nulls
$this->alterNormalizeProperty($value, [CleanFlag::NULLS]);
Return values
mixed

The normalized value, or null when it was entirely cleaned away.

On this page

Search results