Files
crewli/api/database/factories/FormBuilder/FormFieldFactory.php
bert.hausmans af8a9da038 feat(form-builder): form_field_bindings table + polymorphic owner + cascade observer
WS-5a commit 1 of 4 per ARCH-CONSOLIDATION-ADDENDUM-2026-04-24 Q3.

Creates the relational home for what was form_fields.binding JSON and
form_field_library.default_binding JSON. Owner discriminator is polymorphic
morph (owner_type/owner_id) — the pattern the rest of WS-5 (5b validation_rules,
5d options) will reuse.

Migration backfills rows from both JSON sources in a single transaction and
is genuinely reversible (rollback reconstructs the JSON). Old columns remain
in place until commit 3 has switched all readers.

Pattern B (binding=null) is represented by absence of row. mode enum covers
entity_owned / mirrored only.

Cascade on owner delete via observer — bindings are physical state, not
historical audit. FormFieldBindingScope enforces multi-tenancy via UNION over
both owner chains (form_field → schema → org OR form_field_library → org) —
Q2's declarative tenantScopeStrategy() can't walk morph parents.

Tests: migration forward/back, morph relation, cascade observer, scope
isolation, enum coverage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 18:43:11 +02:00

105 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Factories\FormBuilder;
use App\Enums\FormBuilder\FormFieldBindingMode;
use App\Enums\FormBuilder\FormFieldDisplayWidth;
use App\Enums\FormBuilder\FormFieldType;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldBinding;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/** @extends Factory<FormField> */
final class FormFieldFactory extends Factory
{
protected $model = FormField::class;
/** @return array<string, mixed> */
public function definition(): array
{
$fieldType = fake()->randomElement([
FormFieldType::TEXT,
FormFieldType::TEXTAREA,
FormFieldType::EMAIL,
FormFieldType::NUMBER,
FormFieldType::BOOLEAN,
FormFieldType::SELECT,
]);
$label = fake('nl_NL')->randomElement([
'Voornaam', 'Achternaam', 'E-mail', 'Telefoon', 'Opmerkingen',
'Shirtmaat', 'Allergieën', 'Motivatie', 'Geboortedatum',
]);
return [
'form_schema_id' => FormSchema::factory(),
'form_schema_section_id' => null,
'library_field_id' => null,
'field_type' => $fieldType->value,
'slug' => Str::slug($label).'-'.Str::lower(Str::random(4)),
'label' => $label,
'help_text' => fake()->boolean(30) ? fake('nl_NL')->sentence() : null,
'options' => $fieldType === FormFieldType::SELECT
? ['Optie A', 'Optie B', 'Optie C']
: null,
'validation_rules' => null,
'is_required' => fake()->boolean(40),
'is_filterable' => false,
'is_portal_visible' => true,
'is_admin_only' => false,
'is_unique' => false,
'is_pii' => false,
'display_width' => FormFieldDisplayWidth::FULL,
'binding' => null,
'conditional_logic' => null,
'role_restrictions' => null,
'translations' => null,
'value_storage_hint' => $fieldType->recommendedValueStorageHint(),
'review_required' => false,
'sort_order' => 0,
];
}
public function ofType(FormFieldType $type): static
{
return $this->state(fn () => [
'field_type' => $type->value,
'value_storage_hint' => $type->recommendedValueStorageHint(),
]);
}
public function filterable(): static
{
return $this->state(fn () => ['is_filterable' => true]);
}
/**
* Attach an entity-binding row in `form_field_bindings` after the field
* is persisted. Use this instead of populating the legacy `binding` JSON
* column — which WS-5a will drop in commit 3.
*/
public function withEntityBinding(
string $entity,
string $attribute,
FormFieldBindingMode $mode = FormFieldBindingMode::EntityOwned,
?string $syncDirection = null,
): static {
return $this->afterCreating(function (FormField $field) use ($entity, $attribute, $mode, $syncDirection): void {
FormFieldBinding::factory()
->forField($field)
->state([
'target_entity' => $entity,
'target_attribute' => $attribute,
'mode' => $mode->value,
'sync_direction' => $mode === FormFieldBindingMode::Mirrored
? ($syncDirection ?? 'write_on_submit')
: null,
])
->create();
});
}
}