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
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 tonullwhen absent). - $modified : bool = false
-
Reference flag set to
truewhen the value was actually replaced.
Tags
Return values
mixed —The replacement value when it differs from the original, otherwise the original.