Oihana PHP

AlterNotPropertyTrait

Provides an alteration to invert boolean values.

This trait defines a single alteration type Alter::NOT which will invert a boolean value or an array of boolean values. Non-boolean values will be cast to boolean before inversion.

Example usage in AlterDocumentTrait:

$this->alters = [
    'active' => Alter::NOT,          // single boolean
    'flags'  => Alter::NOT,          // array of booleans
];

$data = [
    'active' => true,
    'flags'  => [true, false, true],
];

$result = $this->alter($data);
// $result = [
//     'active' => false,
//     'flags'  => [false, true, false],
// ]

Supported input types:

  • bool → returns the opposite boolean.
  • array<bool> → returns a new array with all elements inverted.
  • any other value → cast to boolean and inverted.
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterNotProperty()  : bool|array<string|int, mixed>
Invert a boolean value or an array of booleans.

Methods

alterNotProperty()

Invert a boolean value or an array of booleans.

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

The value to invert: a boolean, an array of booleans, or any other value (which is coerced to boolean before inversion).

$modified : bool = false

Reference flag; always set to true since the inversion is unconditional.

Tags
example
$processor = new class {
    use \oihana\models\traits\alters\AlterNotPropertyTrait;
};

// Single boolean
$modified = false;
$result = $processor->alterNotProperty(true, $modified);
// $result === false
// $modified === true

// Array of booleans
$array = [true, false, true];
$result = $processor->alterNotProperty($array, $modified);
// $result === [false, true, false]
// $modified === true
Return values
bool|array<string|int, mixed>

The inverted boolean, or an array of inverted booleans when an array was given.

On this page

Search results