Oihana PHP

AlterValueTrait

Provides a method to replace a value with a fixed new one if different.

This trait is part of the alteration system and is intended to be used in combination with AlterDocumentTrait. It encapsulates the logic for the Alter::VALUE transformation type.

Example usage:

use oihana\models\enums\Alter;
use oihana\models\traits\AlterDocumentTrait;
use oihana\models\traits\alters\AlterValueTrait;

class Example
{
    use AlterDocumentTrait, AlterValueTrait;

    public function __construct()
    {
        $this->alters =
        [
            'status' => [ Alter::VALUE , 'published' ] ,
        ];
    }
}

$doc = [ 'status' => 'draft' ];

$processor = new Example();
$result    = $processor->alter($doc);

// Result:
// [
//     'status' => 'published'
// ]
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterValue()  : mixed
Replaces a value with a fixed replacement when they differ, otherwise keeps the original.

Methods

alterValue()

Replaces a value with a fixed replacement when they differ, otherwise keeps the original.

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

The replacement is read from $definition[0]. When it is strictly equal (!==) to the current value, nothing changes and $modified is left untouched; otherwise the new value is returned and $modified is set to true.

Parameters
$value : mixed

The original value.

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

The alter definition; $definition[0] holds the replacement value (defaults to null when absent).

$modified : bool = false

Reference flag set to true when the value was actually replaced.

Tags
example
use oihana\models\traits\alters\AlterValueTrait;

class Example
{
    use AlterValueTrait;
}

$example  = new Example();
$modified = false;

$status = $example->alterValue( 'draft' , [ 'published' ] , $modified );
// $status   === 'published'
// $modified === true

// Same value: no change
$same = $example->alterValue( 'published' , [ 'published' ] , $modified );
// $same === 'published'
Return values
mixed

The replacement value when it differs from the original, otherwise the original.

On this page

Search results