Skip to main content

group

On this page

Mappers::group() chains mappers (and validators) exactly like pipe — same PipeMapper, same sequential execution — but the group encapsulates skips: when a mapper inside the group requests to skip the next mappers (e.g. Mappers::doIfNull(skipNextMappers: true)), only the remaining mappers inside the group are skipped. Mappers following the group still run.

Internally the group returns a MapResult that only takes over isOk() of the inner mappers, never isSkipNext(). A not-ok result (e.g. a closure mapper returning false) still stops the group and is propagated.

Example: skip only inside the group​

$result = Bind::values(null)->map(
Mappers::group(
Mappers::doIfNull(skipNextMappers: true),
Mappers::value(fn (string $v) => strtoupper($v)),
),
Mappers::value(fn (?string $v) => $v ?? 'EMPTY'),
)->toValue()->exec();
var_dump($result->get());

Output:

string(5) "EMPTY"

doIfNull skips the strtoupper mapper inside the group, but the mapper after the group still runs and replaces null with 'EMPTY'.

Example: the same chain with pipe​

$result = Bind::values(null)->map(
Mappers::pipe(
Mappers::doIfNull(skipNextMappers: true),
Mappers::value(fn (string $v) => strtoupper($v)),
),
Mappers::value(fn (?string $v) => $v ?? 'EMPTY'),
)->toValue()->exec();
var_dump($result->get());

Output:

NULL

With pipe the skip is propagated, so the mapper after the pipe is skipped too and the value stays null.