Files
crewli/api/app/FormBuilder/Publishing/ConditionalRequirement.php
bert.hausmans 81a8120f98 feat(form-builder): add PublishGuard framework + 9 concrete guards (WS-6)
Per-purpose schema validation composes a PurposeGuardProvider returning
a list of guards. Errors collected (not first-fail) so the builder UI
surfaces every issue per save. ConditionalRequirement composes higher-
order without proliferating one-off classes.

RequiresIdentityKeyBinding checks the is_identity_key flag specifically;
the binding-existence check is handled additively by the existing
assertRequiredBindingsPresent in FormSchemaService.

SchemaHasLinkedEvent checks owner_type='event' + owner_id (FormSchema
uses polymorphic owner; there is no direct event_id column).

i18n messages live in lang/nl/form_builder_publish_guards.php.

Refs: RFC-WS-6.md §3 (Q13), §4 (V1, V3)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 22:55:42 +02:00

53 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\FormBuilder\Publishing;
use App\Models\FormBuilder\FormSchema;
use Closure;
/**
* RFC-WS-6 §3 (Q13) — higher-order composer. Runs the predicate first;
* delegates to the sub-guard only if the predicate returns true.
* Otherwise returns passed().
*/
final readonly class ConditionalRequirement implements PublishGuard
{
/**
* @param Closure(FormSchema): bool $predicate
*/
public function __construct(
private Closure $predicate,
private PublishGuard $subGuard,
private string $code,
) {}
public function code(): string
{
return "conditional:{$this->code}";
}
public function evaluate(FormSchema $schema): PublishGuardResult
{
if (! ($this->predicate)($schema)) {
return PublishGuardResult::passed($this->code());
}
$subResult = $this->subGuard->evaluate($schema);
if ($subResult->passed) {
return PublishGuardResult::passed($this->code());
}
return PublishGuardResult::failed(
guardCode: $this->code(),
messageKey: $subResult->messageKey ?? 'form_builder_publish_guards.conditional',
offendingFormFieldId: $subResult->offendingFormFieldId,
context: array_merge(
$subResult->context,
['delegated_to' => $this->subGuard->code()],
),
);
}
}