93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Database\Factories\FormBuilder;
|
|
|
|
use App\Enums\FormBuilder\FormFieldBindingMode;
|
|
use App\Enums\FormBuilder\FormFieldType;
|
|
use App\Enums\FormBuilder\FormFieldValidationRuleType;
|
|
use App\Models\FormBuilder\FormFieldBinding;
|
|
use App\Models\FormBuilder\FormFieldLibrary;
|
|
use App\Models\FormBuilder\FormFieldValidationRule;
|
|
use App\Models\Organisation;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Support\Str;
|
|
|
|
/** @extends Factory<FormFieldLibrary> */
|
|
final class FormFieldLibraryFactory extends Factory
|
|
{
|
|
protected $model = FormFieldLibrary::class;
|
|
|
|
/** @return array<string, mixed> */
|
|
public function definition(): array
|
|
{
|
|
$name = fake('nl_NL')->randomElement([
|
|
'Shirtmaat (standaard)', 'Dieet (standaard)',
|
|
'Noodcontact (standaard)', 'Motivatie (standaard)',
|
|
]);
|
|
|
|
return [
|
|
'organisation_id' => Organisation::factory(),
|
|
'name' => $name,
|
|
'slug' => Str::slug($name).'-'.Str::lower(Str::random(4)),
|
|
'field_type' => FormFieldType::TEXT->value,
|
|
'label' => fake('nl_NL')->words(2, true),
|
|
'help_text' => null,
|
|
'options' => null,
|
|
'default_is_required' => false,
|
|
'default_is_filterable' => false,
|
|
'translations' => null,
|
|
'description' => fake('nl_NL')->sentence(),
|
|
'is_active' => true,
|
|
];
|
|
}
|
|
|
|
public function system(): static
|
|
{
|
|
return $this->state(fn () => ['is_system' => true]);
|
|
}
|
|
|
|
/**
|
|
* Attach a binding row in `form_field_bindings` after the library entry
|
|
* is persisted. Replaces the legacy `default_binding` JSON column.
|
|
*/
|
|
public function withDefaultBinding(
|
|
string $entity,
|
|
string $attribute,
|
|
FormFieldBindingMode $mode = FormFieldBindingMode::EntityOwned,
|
|
?string $syncDirection = null,
|
|
): static {
|
|
return $this->afterCreating(function (FormFieldLibrary $library) use ($entity, $attribute, $mode, $syncDirection): void {
|
|
FormFieldBinding::factory()
|
|
->forLibrary($library)
|
|
->state([
|
|
'target_entity' => $entity,
|
|
'target_attribute' => $attribute,
|
|
'mode' => $mode->value,
|
|
'sync_direction' => $mode === FormFieldBindingMode::Mirrored
|
|
? ($syncDirection ?? 'write_on_submit')
|
|
: null,
|
|
])
|
|
->create();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Attach a validation-rule row in `form_field_validation_rules` after
|
|
* the library entry is persisted. Replaces populating the legacy
|
|
* `validation_rules` JSON column — which WS-5b commit 5 drops.
|
|
*
|
|
* @param array<string, mixed> $parameters
|
|
*/
|
|
public function withValidationRule(FormFieldValidationRuleType $type, array $parameters): static
|
|
{
|
|
return $this->afterCreating(function (FormFieldLibrary $library) use ($type, $parameters): void {
|
|
FormFieldValidationRule::factory()
|
|
->forLibrary($library)
|
|
->ofType($type, $parameters)
|
|
->create();
|
|
});
|
|
}
|
|
}
|