WS-5c commit 2 of 4 — the service layer, backfill migration, and
read-path switch. Per addendum Q3, conditional_logic applies to
FormField only — no library mirror and no copyLogic on
FormFieldService::insertFromLibrary.
FormFieldConditionalLogicService owns every write:
- logicFor(field): depth-limited eager-load of the tree
- replaceLogic(field, tree): transactional structure + operator +
field_slug validation + cycle check + activity-log emit
(field.conditional_logic_replaced)
- toJsonShape(root): reconstructs the canonical ARCH §8
`{show_when: {...}}` shape — single source of truth for the
snapshot writer + API resources
- assertSpecsValid(tree): public boundary guard for the FormRequest
strict validator (WS-5c commit 3 wires this up)
- assertNoCycles(field, tree): contract preserved from
FormFieldService::assertNoConditionalCycle, implementation now
reads the relational adjacency.
Backfill migration translates pre-WS-5c conditional_logic JSON to
rows. Strict dispatch: unknown operators / unknown top-level keys /
malformed groups FAIL the migration — Phase A seed-scan confirmed
the catalogue parity, so any drift is a data bug to fix at source,
not silently absorb. Rollback rebuilds canonical JSON and clears
the relational tree.
FormFieldService.create/update route `conditional_logic` through
the new service (matching the extract-and-delegate pattern from
WS-5a bindings and WS-5b validation rules). Snapshot writer + both
resources (FormFieldResource, PublicFormSchemaResource) read via
`toJsonShape(rootConditionalLogicGroup())` — byte-for-byte parity
with the pre-WS-5c JSON contract.
InvalidConditionalLogicSpecException handled in FormFieldController
as 422, same as FrozenSchemaException / CyclicDependencyException.
Tests: 20 new under tests/Feature/FormBuilder/ConditionalLogic/
(service, cycle detection, backfill forward+rollback+failure cases,
snapshot + resource parity). FormFieldApiTest cyclic rejection test
rewritten to use the new factory state. Rollback step counts in
WS-5a/b migration tests bumped +1 for the new backfill migration.
Baseline 1122 → 1142 green (3032 → 3085 assertions).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
142 lines
5.0 KiB
PHP
142 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Resources\FormBuilder;
|
|
|
|
use App\Enums\FormBuilder\FormFieldType;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\PersonTag;
|
|
use App\Services\FormBuilder\FormFieldBindingService;
|
|
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
|
use App\Services\FormBuilder\FormFieldConfigService;
|
|
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
|
use App\Services\FormBuilder\FormLocaleResolver;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/**
|
|
* @mixin FormField
|
|
*/
|
|
final class FormFieldResource extends JsonResource
|
|
{
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
$locale = app(FormLocaleResolver::class)->resolve(
|
|
$this->resource->schema,
|
|
$request->user(),
|
|
);
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'form_schema_id' => $this->form_schema_id,
|
|
'form_schema_section_id' => $this->form_schema_section_id,
|
|
'library_field_id' => $this->library_field_id,
|
|
'field_type' => $this->field_type,
|
|
'slug' => $this->slug,
|
|
'label' => $this->resolvedLabel($locale),
|
|
'help_text' => $this->resolvedHelpText($locale),
|
|
'section' => $this->section,
|
|
'options' => $this->normalizedOptions($locale),
|
|
'available_tags' => $this->when(
|
|
$this->field_type === FormFieldType::TAG_PICKER->value,
|
|
fn () => $this->availableTags(),
|
|
),
|
|
'validation_rules' => app(FormFieldValidationRuleService::class)->toJsonShape(
|
|
$this->resource->validationRules,
|
|
),
|
|
'configs' => app(FormFieldConfigService::class)->toJsonShape(
|
|
$this->resource->configs,
|
|
),
|
|
'is_required' => (bool) $this->is_required,
|
|
'is_filterable' => (bool) $this->is_filterable,
|
|
'is_portal_visible' => (bool) $this->is_portal_visible,
|
|
'is_admin_only' => (bool) $this->is_admin_only,
|
|
'is_unique' => (bool) $this->is_unique,
|
|
'is_pii' => (bool) $this->is_pii,
|
|
'display_width' => $this->display_width instanceof \BackedEnum ? $this->display_width->value : $this->display_width,
|
|
'binding' => app(FormFieldBindingService::class)->toJsonShape(
|
|
$this->resource->bindings->first(),
|
|
),
|
|
'conditional_logic' => app(FormFieldConditionalLogicService::class)->toJsonShape(
|
|
$this->resource->rootConditionalLogicGroup(),
|
|
),
|
|
'role_restrictions' => $this->role_restrictions,
|
|
'translations' => $this->translations,
|
|
'value_storage_hint' => $this->value_storage_hint instanceof \BackedEnum ? $this->value_storage_hint->value : $this->value_storage_hint,
|
|
'review_required' => (bool) $this->review_required,
|
|
'sort_order' => (int) $this->sort_order,
|
|
];
|
|
}
|
|
|
|
private function resolvedLabel(string $locale): string
|
|
{
|
|
$translations = $this->translations ?? [];
|
|
if (isset($translations[$locale]['label']) && $translations[$locale]['label'] !== '') {
|
|
return (string) $translations[$locale]['label'];
|
|
}
|
|
|
|
return (string) $this->label;
|
|
}
|
|
|
|
private function resolvedHelpText(string $locale): ?string
|
|
{
|
|
$translations = $this->translations ?? [];
|
|
if (isset($translations[$locale]['help_text'])) {
|
|
return (string) $translations[$locale]['help_text'];
|
|
}
|
|
|
|
return $this->help_text;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, mixed>|null
|
|
*/
|
|
private function normalizedOptions(string $locale): ?array
|
|
{
|
|
$options = $this->options;
|
|
if (! is_array($options)) {
|
|
return null;
|
|
}
|
|
$translations = $this->translations ?? [];
|
|
if (isset($translations[$locale]['options']) && is_array($translations[$locale]['options'])) {
|
|
return array_values($translations[$locale]['options']);
|
|
}
|
|
|
|
return array_values($options);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, string>>
|
|
*/
|
|
private function availableTags(): array
|
|
{
|
|
$organisationId = $this->resource->schema?->organisation_id;
|
|
if ($organisationId === null) {
|
|
return [];
|
|
}
|
|
|
|
$configs = app(FormFieldConfigService::class)->toJsonShape($this->resource->configs);
|
|
$categoryFilter = (array) ($configs['tag_categories']['categories'] ?? []);
|
|
|
|
$query = PersonTag::withoutGlobalScopes()
|
|
->where('organisation_id', $organisationId)
|
|
->where('is_active', true);
|
|
|
|
if ($categoryFilter !== []) {
|
|
$query->whereIn('category', $categoryFilter);
|
|
}
|
|
|
|
return $query->get(['id', 'name', 'category'])
|
|
->map(fn ($t) => [
|
|
'id' => (string) $t->id,
|
|
'name' => (string) $t->name,
|
|
'category' => (string) $t->category,
|
|
])
|
|
->all();
|
|
}
|
|
}
|