*/ final class FormFieldLibraryFactory extends Factory { protected $model = FormFieldLibrary::class; /** @return array */ 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, 'validation_rules' => 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 $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(); }); } }