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>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?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 (Q13) — given that the binding's existence is enforced
|
||||
* upstream by FormSchemaService::assertRequiredBindingsPresent(), this
|
||||
* guard adds the `is_identity_key=true` flag check.
|
||||
*/
|
||||
final readonly class RequiresIdentityKeyBinding implements PublishGuard
|
||||
{
|
||||
public function __construct(
|
||||
private string $entity,
|
||||
private string $attribute,
|
||||
) {}
|
||||
|
||||
public function code(): string
|
||||
{
|
||||
return "requires_identity_key_binding:{$this->entity}:{$this->attribute}";
|
||||
}
|
||||
|
||||
public function evaluate(FormSchema $schema): PublishGuardResult
|
||||
{
|
||||
/** @var FormField $field */
|
||||
foreach ($schema->fields as $field) {
|
||||
/** @var FormFieldBinding $binding */
|
||||
foreach ($field->bindings as $binding) {
|
||||
if ($binding->target_entity === $this->entity
|
||||
&& $binding->target_attribute === $this->attribute
|
||||
&& (bool) $binding->is_identity_key
|
||||
) {
|
||||
return PublishGuardResult::passed($this->code());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return PublishGuardResult::failed(
|
||||
guardCode: $this->code(),
|
||||
messageKey: 'form_builder_publish_guards.requires_identity_key_binding',
|
||||
context: ['entity' => $this->entity, 'attribute' => $this->attribute],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user