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>
76 lines
2.8 KiB
PHP
76 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\FormBuilder\Publishing;
|
|
|
|
use App\Enums\FormBuilder\BindingTargetType;
|
|
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
|
|
use App\Exceptions\FormBuilder\UnknownBindingTargetException;
|
|
use App\FormBuilder\Bindings\BindingTypeRegistry;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormFieldBinding;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
|
|
/**
|
|
* RFC-WS-6 §4 (V1) — Append is collection-only. Targets unknown to
|
|
* BindingTypeRegistry fail with a distinct context payload so admins
|
|
* see the actionable distinction.
|
|
*/
|
|
final readonly class AppendStrategyRequiresCollectionTarget implements PublishGuard
|
|
{
|
|
public function __construct(private BindingTypeRegistry $registry) {}
|
|
|
|
public function code(): string
|
|
{
|
|
return 'append_strategy_requires_collection_target';
|
|
}
|
|
|
|
public function evaluate(FormSchema $schema): PublishGuardResult
|
|
{
|
|
/** @var FormField $field */
|
|
foreach ($schema->fields as $field) {
|
|
/** @var FormFieldBinding $binding */
|
|
foreach ($field->bindings as $binding) {
|
|
if ($binding->merge_strategy !== FormFieldBindingMergeStrategy::Append) {
|
|
continue;
|
|
}
|
|
|
|
$entity = (string) $binding->target_entity;
|
|
$attribute = (string) $binding->target_attribute;
|
|
|
|
try {
|
|
$meta = $this->registry->resolve($entity, $attribute);
|
|
} catch (UnknownBindingTargetException) {
|
|
return PublishGuardResult::failed(
|
|
guardCode: $this->code(),
|
|
messageKey: 'form_builder_publish_guards.append_strategy_requires_collection_target',
|
|
offendingFormFieldId: (string) $field->id,
|
|
context: [
|
|
'reason' => 'unknown_target',
|
|
'entity' => $entity,
|
|
'attribute' => $attribute,
|
|
],
|
|
);
|
|
}
|
|
|
|
if ($meta->type !== BindingTargetType::COLLECTION) {
|
|
return PublishGuardResult::failed(
|
|
guardCode: $this->code(),
|
|
messageKey: 'form_builder_publish_guards.append_strategy_requires_collection_target',
|
|
offendingFormFieldId: (string) $field->id,
|
|
context: [
|
|
'reason' => 'scalar_target',
|
|
'entity' => $entity,
|
|
'attribute' => $attribute,
|
|
'target_type' => $meta->type->value,
|
|
],
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
return PublishGuardResult::passed($this->code());
|
|
}
|
|
}
|