DoIfMapper / DoIfSingleClosureMapper
Conditional control-flow mappers. A condition (a bool or a closure returning bool) is evaluated, and when it is true the mapper can:
- abort the bind (
abort: true) — the result becomes invalid and later mappers are skipped; - skip the following mappers on the same prop (
skipNextMappers: true) — the value passes through unchanged; - change existence / logical state of the bindables (
chExists,chLogical), optionally cascading to descendants (cascaded: true).
There are two shapes:
| Factory | Scope | Condition receives |
|---|---|---|
Mappers::doIf($cond, ...) | whole boundary | BindBoundary (and BindContext) |
Mappers::doIfValueClosure($closure, ...) | per bindable | the bindable's value |
Mappers::doIfBindableClosure($closure, ...) | per bindable | the Bindable instance |
Mappers::doIfNull(...) / doIfNotNull(...) | per bindable | value === null / !== null |
Mappers::doIfInvalid(...) | per bindable | !$bindable->isValid() |
The deleteIf family — Mappers::deleteIf($cond), deleteIfValueClosure($closure), deleteIfBindableClosure($closure) — are shortcuts that mark the bindable (and, by default, its descendants) non-existing when the condition is true.
Example: skipping a mapper when the value is null
$result = Bind::attrs(['prop' => null])->prop('prop',
Mappers::doIfNull(skipNextMappers: true),
Mappers::value(fn (string $v) => $v . '-x'))
->toArray()->exec();
var_dump($result->get());
Output:
array(1) {
["prop"]=>
NULL
}
Because the value is null, doIfNull skips the following value mapper, so the value passes through unchanged. With ['prop' => 'holeradio'] the value mapper would run and produce 'holeradio-x'.
Example: aborting the bind from a boundary condition
$result = Bind::attrs(['prop' => 'holeradio'])->prop('prop',
Mappers::value(fn (string $v) => $v . '-1'),
Mappers::doIf(fn (BindBoundary $b) => true, abort: true),
Mappers::value(function () { /* never reached */ }))
->toArray()->exec();
var_dump($result->isValid()); // false
Output:
bool(false)
The doIf condition is true, so abort: true marks the result invalid and the third mapper is never executed.