feat(form-builder): add BindingTypeRegistry as single source of truth for target shapes (WS-6)

Config-driven mapping from (target_entity, target_attribute) to storage
shape (scalar/collection/relation), PHP type, and identity-key
eligibility. Replaces any name-suffix matching (e.g. _tags, _skills) —
those are convention-not-contract and reject by design.

Used by publish guards now and (in session 2) by FormBindingApplicator.

Refs: RFC-WS-6.md §4 (V1)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 22:41:25 +02:00
parent b2e9ef8824
commit 0dd991c688
7 changed files with 356 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\FormBuilder\Bindings;
use App\Enums\FormBuilder\BindingTargetType;
/**
* RFC-WS-6 §4 (V1) single config row from
* config/form_builder/binding_targets.php, materialised by
* {@see BindingTypeRegistry::resolve()}.
*/
final readonly class BindingTargetMeta
{
public function __construct(
public BindingTargetType $type,
public string $php,
public bool $identityKeyEligible,
) {}
}

View File

@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace App\FormBuilder\Bindings;
use App\Enums\FormBuilder\BindingTargetType;
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
use App\Exceptions\FormBuilder\InvalidBindingTargetException;
use App\Exceptions\FormBuilder\UnknownBindingTargetException;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
/**
* RFC-WS-6 §4 (V1) single source of truth for the storage shape of
* binding-target attributes. Consumed by publish guards
* (AppendStrategyRequiresCollectionTarget) and (in session 2) by
* FormBindingApplicator.
*
* NOT name-suffix matching (e.g. "ends with _tags"); convention-not-
* contract is rejected by design.
*/
final class BindingTypeRegistry
{
/**
* @var array<string, array<string, BindingTargetMeta>>|null
*/
private ?array $cache = null;
public function __construct(private readonly ConfigRepository $config) {}
/**
* @throws UnknownBindingTargetException
*/
public function resolve(string $entity, string $attribute): BindingTargetMeta
{
$entries = $this->resolveTable();
if (! isset($entries[$entity][$attribute])) {
throw new UnknownBindingTargetException($entity, $attribute);
}
return $entries[$entity][$attribute];
}
public function isKnown(string $entity, string $attribute): bool
{
$entries = $this->resolveTable();
return isset($entries[$entity][$attribute]);
}
public function isIdentityKeyEligible(string $entity, string $attribute): bool
{
return $this->isKnown($entity, $attribute)
&& $this->resolve($entity, $attribute)->identityKeyEligible;
}
/**
* @return list<string>
*/
public function entities(): array
{
return array_keys($this->resolveTable());
}
/**
* @return list<string>
*/
public function attributesFor(string $entity): array
{
$entries = $this->resolveTable();
if (! isset($entries[$entity])) {
return [];
}
return array_keys($entries[$entity]);
}
/**
* @throws UnknownBindingTargetException when (entity, attribute) is unknown
* @throws InvalidBindingTargetException when Append is paired with a non-COLLECTION target
*/
public function validateAppendStrategy(
string $entity,
string $attribute,
FormFieldBindingMergeStrategy $strategy,
): void {
if ($strategy !== FormFieldBindingMergeStrategy::Append) {
return;
}
$meta = $this->resolve($entity, $attribute);
if ($meta->type !== BindingTargetType::COLLECTION) {
throw new InvalidBindingTargetException(
entity: $entity,
attribute: $attribute,
reason: "merge_strategy=append requires a COLLECTION target; got {$meta->type->value}",
);
}
}
/**
* @return array<string, array<string, BindingTargetMeta>>
*/
private function resolveTable(): array
{
if ($this->cache !== null) {
return $this->cache;
}
/** @var array<string, array<string, array{type: string, php: string, identity_key_eligible: bool}>> $raw */
$raw = $this->config->get('form_builder.binding_targets', []);
$built = [];
foreach ($raw as $entity => $attributes) {
foreach ($attributes as $attribute => $row) {
$built[$entity][$attribute] = new BindingTargetMeta(
type: BindingTargetType::from($row['type']),
php: $row['php'],
identityKeyEligible: $row['identity_key_eligible'],
);
}
}
$this->cache = $built;
return $built;
}
}