*/ 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, 'default_is_required' => false, 'default_is_filterable' => false, 'translations' => null, 'description' => fake('nl_NL')->sentence(), 'is_active' => true, ]; } /** * Attach option rows in `form_field_options` after the library entry * is persisted. Replaces populating the legacy `options` JSON column * (WS-5d commit 2). * * @param list}> $values */ public function withOptions(array $values): static { return $this->afterCreating(function (FormFieldLibrary $library) use ($values): void { $specs = []; foreach (array_values($values) as $i => $entry) { if (is_string($entry)) { $specs[] = ['value' => $entry, 'label' => $entry, 'sort_order' => $i]; continue; } $specs[] = [ 'value' => (string) $entry['value'], 'label' => (string) $entry['label'], 'sort_order' => $entry['sort_order'] ?? $i, 'translations' => $entry['translations'] ?? null, ]; } app(FormFieldOptionService::class)->replaceOptions($library, $specs); }); } 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(); }); } }