Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit Fassungen, Anfrage-Einstiege, Posteingang mit Status und Notizen, Einstellungen mit Empfängern. Das Widget speichert nur die Kennung eines veröffentlichten Einstiegs; Formular, Bedingungen und Annahme kommen zur Laufzeit vom Server (Kompaktformular und mehrstufiger Dialog aus derselben Beschreibung). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Communication;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Stabile technische Kennungen für Anliegen, Felder, Gruppen und Auswahlwerte. Eine Kennung entsteht einmal aus der
|
|
* Bezeichnung und ändert sich nie; die öffentliche Bezeichnung darf sich ändern (ADR-60).
|
|
*/
|
|
final class Keys
|
|
{
|
|
public const PATTERN = '/^[a-z][a-z0-9_]{1,59}$/';
|
|
|
|
/** Kennungen der eingebauten Anliegen-Felder; Felder des Katalogs dürfen sie nicht belegen. */
|
|
public const RESERVED = ['anliegen', 'unteranliegen'];
|
|
|
|
public static function valid(mixed $key): bool
|
|
{
|
|
return is_string($key) && preg_match(self::PATTERN, $key) === 1;
|
|
}
|
|
|
|
/**
|
|
* Erzeugt aus einer Bezeichnung eine freie Kennung. $taken enthält bereits vergebene Kennungen.
|
|
*
|
|
* @param list<string> $taken
|
|
*/
|
|
public static function from(string $label, array $taken = []): string
|
|
{
|
|
$base = Str::slug($label, '_', 'de');
|
|
$base = preg_replace('/^[^a-z]+/', '', $base) ?? '';
|
|
$base = substr($base, 0, 50);
|
|
|
|
if (strlen($base) < 2) {
|
|
$base = 'eintrag';
|
|
}
|
|
|
|
$taken = array_merge($taken, self::RESERVED);
|
|
$key = $base;
|
|
$n = 2;
|
|
|
|
while (in_array($key, $taken, true)) {
|
|
$key = $base.'_'.$n++;
|
|
}
|
|
|
|
return $key;
|
|
}
|
|
|
|
/**
|
|
* Ergänzt fehlende oder doppelte Kennungen von Auswahlwerten. Bestehende, gültige Kennungen bleiben unverändert,
|
|
* damit gespeicherte Antworten und Bedingungen gültig bleiben, auch wenn eine Bezeichnung umbenannt wird.
|
|
*
|
|
* @param array<int|string, mixed> $options
|
|
* @return list<array{key: string, label: string}>
|
|
*/
|
|
public static function withKeys(array $options): array
|
|
{
|
|
$result = [];
|
|
$taken = [];
|
|
|
|
foreach ($options as $option) {
|
|
$key = is_array($option) ? ($option['key'] ?? null) : null;
|
|
|
|
if (self::valid($key) && ! in_array($key, $taken, true)) {
|
|
$taken[] = $key;
|
|
}
|
|
}
|
|
|
|
foreach ($options as $option) {
|
|
$option = is_array($option) ? $option : [];
|
|
$label = trim((string) ($option['label'] ?? ''));
|
|
|
|
if ($label === '') {
|
|
continue;
|
|
}
|
|
|
|
$key = $option['key'] ?? null;
|
|
|
|
if (! self::valid($key) || in_array($key, array_column($result, 'key'), true)) {
|
|
$key = self::from($label, array_merge($taken, array_column($result, 'key')));
|
|
$taken[] = $key;
|
|
}
|
|
|
|
$result[] = ['key' => $key, 'label' => mb_substr($label, 0, 120)];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|