Files
crewli/api/app/FormBuilder/Purposes/PurposeRegistry.php
bert.hausmans e93207765b feat(form-builder): introduce purpose registry and definition value object
Lands the v1.0 purpose registry (WS-2 of the consolidation sprint) as a
first-class concept: a `PurposeDefinition` value object, a
`PurposeRegistry` service keyed by slug, and a declarative
`config/form_builder/purposes.php` registry with exactly the seven
purposes from ARCH-CONSOLIDATION §6.4.

Also rebuilds the morph-map in `AppServiceProvider::boot` into three
labelled blocks: (1) domain subject types derived from
`PurposeRegistry::allSubjectTypes()`, (2) non-purpose domain types
hardcoded with comments (form_schemas owner_types, activity-log
subjects), (3) framework types (spatie/activitylog; Sanctum stays
absent per addendum Q4).

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

86 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\FormBuilder\Purposes;
use App\Enums\FormBuilder\FormSubmissionMode;
use App\FormBuilder\Purposes\Exceptions\PurposeNotFoundException;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
final class PurposeRegistry
{
/** @var array<string, PurposeDefinition>|null */
private ?array $cache = null;
public function __construct(private readonly ConfigRepository $config) {}
/** @return array<string, PurposeDefinition> keyed by slug */
public function all(): array
{
if ($this->cache !== null) {
return $this->cache;
}
/** @var array<string, array<string, mixed>> $raw */
$raw = (array) $this->config->get('form_builder.purposes', []);
$definitions = [];
foreach ($raw as $slug => $attrs) {
$mode = $attrs['default_submission_mode'] ?? null;
if (! $mode instanceof FormSubmissionMode) {
throw new \InvalidArgumentException(
"Purpose '{$slug}' has invalid default_submission_mode; expected FormSubmissionMode enum case."
);
}
$definitions[(string) $slug] = new PurposeDefinition(
slug: (string) $slug,
label: (string) ($attrs['label'] ?? ''),
subjectType: (string) ($attrs['subject_type'] ?? ''),
defaultSubmissionMode: $mode,
allowsPublicAccess: (bool) ($attrs['allows_public_access'] ?? false),
requiredBindings: array_values((array) ($attrs['required_bindings'] ?? [])),
);
}
return $this->cache = $definitions;
}
public function get(string $slug): PurposeDefinition
{
$all = $this->all();
if (! isset($all[$slug])) {
throw PurposeNotFoundException::forSlug($slug);
}
return $all[$slug];
}
public function has(string $slug): bool
{
return isset($this->all()[$slug]);
}
/** @return list<string> unique sorted list of subject_type aliases */
public function allSubjectTypes(): array
{
$types = array_values(array_unique(array_map(
static fn (PurposeDefinition $p): string => $p->subjectType,
$this->all(),
)));
sort($types);
return $types;
}
/** @return list<string> slugs with allows_public_access === true */
public function publicAccessibleSlugs(): array
{
return array_values(array_keys(array_filter(
$this->all(),
static fn (PurposeDefinition $p): bool => $p->allowsPublicAccess,
)));
}
}