Files
crewli/api/app/FormBuilder/Publishing/MaxOneIdentityKeyPerTargetEntity.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

51 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\FormBuilder\Publishing;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldBinding;
use App\Models\FormBuilder\FormSchema;
/**
* RFC-WS-6 §3 (Q8) — composite identity-key resolution is out of scope
* for v1; this guard enforces single-key per target_entity at publish
* time. Universal: wires into every PurposeGuardProvider.
*/
final class MaxOneIdentityKeyPerTargetEntity implements PublishGuard
{
public function code(): string
{
return 'max_one_identity_key_per_target_entity';
}
public function evaluate(FormSchema $schema): PublishGuardResult
{
$countsByEntity = [];
/** @var FormField $field */
foreach ($schema->fields as $field) {
/** @var FormFieldBinding $binding */
foreach ($field->bindings as $binding) {
if (! (bool) $binding->is_identity_key) {
continue;
}
$entity = (string) $binding->target_entity;
$countsByEntity[$entity] = ($countsByEntity[$entity] ?? 0) + 1;
}
}
foreach ($countsByEntity as $entity => $count) {
if ($count > 1) {
return PublishGuardResult::failed(
guardCode: $this->code(),
messageKey: 'form_builder_publish_guards.max_one_identity_key_per_target_entity',
context: ['entity' => $entity, 'count' => $count],
);
}
}
return PublishGuardResult::passed($this->code());
}
}