Lege das Datenmodell und den Anfrageservice der Kommunikationsverwaltung an
Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit unveränderlichen Fassungen, Anfrage-Einstiege und eingegangene Anfragen. Eine gemeinsame Regelauswertung bedient Besucher, Vorschau und Anfrageservice; Werte nicht mehr relevanter Felder werden verworfen, Anliegen und Auswahlwerte serverseitig geprüft, doppeltes Absenden bleibt eine Anfrage. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e2d64a4a9
commit
2ee1fc9866
30 files changed
+3380
No files matched your search
@@ -0,0 +1,452 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Communication;
|
||||
|
||||
/**
|
||||
* Übersetzt den bearbeitbaren Entwurf einer Anfragevorlage in eine vollständig aufgelöste Definition (Fassung) und prüft
|
||||
* dabei die Konfiguration: Verweise, doppelte Verwendung, Regeln und deren Reihenfolge. Kein Skript, keine freien
|
||||
* Ausdrücke: Regeln sind Daten aus einer festen Menge von Operatoren und Wirkungen (ADR-60).
|
||||
*
|
||||
* Entwurf: {texts, issues[Kennungen oberster Anliegen], steps[{key,title,intro,items[{kind,...}]}], rules[...]}
|
||||
* Reihenfolge ist Dokumentreihenfolge: Schritte, darin Bausteine, darin Felder. Eine Bedingung darf nur auf Felder
|
||||
* verweisen, die vor ihrem Ziel stehen; damit sind Zyklen ausgeschlossen.
|
||||
*/
|
||||
final class TemplateCompiler
|
||||
{
|
||||
public const EFFECTS = [
|
||||
'show' => 'Anzeigen, wenn …',
|
||||
'hide' => 'Ausblenden, wenn …',
|
||||
'require' => 'Pflichtfeld, wenn …',
|
||||
'skip' => 'Schritt überspringen, wenn …',
|
||||
'preset' => 'Vorbelegen, wenn …',
|
||||
];
|
||||
|
||||
public const OPERATORS = [
|
||||
'eq' => 'ist',
|
||||
'neq' => 'ist nicht',
|
||||
'in' => 'ist eines von',
|
||||
'not_in' => 'ist keines von',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $draft
|
||||
* @return array{definition: ?array<string, mixed>, errors: list<string>}
|
||||
*/
|
||||
public function compile(array $draft): array
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
$fieldsByKey = InquiryField::query()->offered()->get()->keyBy('key');
|
||||
$groupsByKey = InquiryFieldGroup::query()->offered()->get()->keyBy('key');
|
||||
$tree = $this->issueTree((array) ($draft['issues'] ?? []));
|
||||
|
||||
$steps = [];
|
||||
$fields = [];
|
||||
$usedFields = [];
|
||||
$usedGroups = [];
|
||||
$hasIssue = false;
|
||||
$position = [];
|
||||
$order = 0;
|
||||
$stepKeys = [];
|
||||
|
||||
foreach (array_values((array) ($draft['steps'] ?? [])) as $index => $step) {
|
||||
$number = $index + 1;
|
||||
$step = is_array($step) ? $step : [];
|
||||
$key = (string) ($step['key'] ?? '');
|
||||
$title = trim((string) ($step['title'] ?? ''));
|
||||
|
||||
if (! Keys::valid($key) && ! preg_match('/^s[a-z0-9]{3,12}$/', $key)) {
|
||||
$key = 's'.substr(md5($number.'|'.$title), 0, 6);
|
||||
}
|
||||
|
||||
if (in_array($key, $stepKeys, true)) {
|
||||
$errors[] = "Schritt $number: Die Kennung des Schritts kommt doppelt vor.";
|
||||
}
|
||||
|
||||
$stepKeys[] = $key;
|
||||
|
||||
if ($title === '') {
|
||||
$title = "Schritt $number";
|
||||
}
|
||||
|
||||
$position["step:$key"] = $order++;
|
||||
$items = [];
|
||||
|
||||
foreach (array_values((array) ($step['items'] ?? [])) as $item) {
|
||||
$item = is_array($item) ? $item : [];
|
||||
$kind = (string) ($item['kind'] ?? '');
|
||||
|
||||
if ($kind === 'issue') {
|
||||
if ($hasIssue) {
|
||||
$errors[] = "Schritt $number („{$title}“): Die Auswahl des Anliegens darf nur einmal vorkommen.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$hasIssue = true;
|
||||
$position['field:anliegen'] = $order++;
|
||||
$position['field:unteranliegen'] = $order++;
|
||||
$items[] = ['kind' => 'issue'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($kind === 'group') {
|
||||
$groupKey = (string) ($item['group'] ?? '');
|
||||
$group = $groupsByKey[$groupKey] ?? null;
|
||||
|
||||
if ($group === null) {
|
||||
$errors[] = "Schritt $number („{$title}“): Eine Feldgruppe ist nicht mehr verfügbar (gelöscht, deaktiviert oder archiviert).";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($usedGroups[$groupKey])) {
|
||||
$errors[] = "Die Feldgruppe „{$group->name}“ ist mehrfach eingebunden.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$usedGroups[$groupKey] = true;
|
||||
$position["group:$groupKey"] = $order++;
|
||||
$rows = [];
|
||||
|
||||
foreach ($group->itemList() as $row) {
|
||||
$field = $fieldsByKey[$row['field']] ?? null;
|
||||
|
||||
if ($field === null) {
|
||||
$errors[] = "Die Feldgruppe „{$group->name}“ verwendet ein Feld, das nicht mehr verfügbar ist.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($usedFields[$field->key])) {
|
||||
$errors[] = "Das Feld „{$field->name}“ kommt mehrfach in der Vorlage vor.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$usedFields[$field->key] = true;
|
||||
$fields[$field->key] = $field->snapshot();
|
||||
$position['field:'.$field->key] = $order++;
|
||||
$rows[] = ['field' => $field->key, 'required' => $row['required']];
|
||||
}
|
||||
|
||||
$items[] = [
|
||||
'kind' => 'group',
|
||||
'key' => $group->key,
|
||||
'label' => $group->label ?: $group->name,
|
||||
'description' => $group->description,
|
||||
'fields' => $rows,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($kind === 'field') {
|
||||
$field = $fieldsByKey[(string) ($item['field'] ?? '')] ?? null;
|
||||
|
||||
if ($field === null) {
|
||||
$errors[] = "Schritt $number („{$title}“): Ein Einzelfeld ist nicht mehr verfügbar.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($usedFields[$field->key])) {
|
||||
$errors[] = "Das Feld „{$field->name}“ kommt mehrfach in der Vorlage vor.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$usedFields[$field->key] = true;
|
||||
$fields[$field->key] = $field->snapshot();
|
||||
$position['field:'.$field->key] = $order++;
|
||||
$items[] = [
|
||||
'kind' => 'group',
|
||||
'key' => null,
|
||||
'label' => null,
|
||||
'description' => null,
|
||||
'fields' => [['field' => $field->key, 'required' => (bool) ($item['required'] ?? false)]],
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$errors[] = "Schritt $number („{$title}“): Ein Baustein hat keinen gültigen Typ.";
|
||||
}
|
||||
|
||||
if ($items === []) {
|
||||
$errors[] = "Schritt $number („{$title}“) enthält nichts.";
|
||||
}
|
||||
|
||||
$steps[] = [
|
||||
'key' => $key,
|
||||
'title' => mb_substr($title, 0, 120),
|
||||
'intro' => $this->text($step['intro'] ?? null, 600),
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
|
||||
if ($steps === []) {
|
||||
$errors[] = 'Die Vorlage hat noch keinen Schritt.';
|
||||
}
|
||||
|
||||
$purposes = [];
|
||||
foreach ($fields as $field) {
|
||||
if ($field['purpose'] !== null) {
|
||||
$purposes[$field['purpose']][] = $field['key'];
|
||||
}
|
||||
}
|
||||
|
||||
if (! isset($purposes[FieldPurpose::Email->value])) {
|
||||
$errors[] = 'Die Vorlage braucht ein Feld mit der Verwendung „E-Mail-Adresse für Rückfragen“, sonst lässt sich eine Anfrage nicht beantworten.';
|
||||
}
|
||||
|
||||
foreach ($purposes as $purpose => $keys) {
|
||||
if (count($keys) > 1) {
|
||||
$errors[] = 'Die Verwendung „'.FieldPurpose::from($purpose)->label().'“ ist mehreren Feldern zugeordnet.';
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasIssue && $tree === []) {
|
||||
$errors[] = 'Die Auswahl des Anliegens hat kein verfügbares Anliegen. Bitte Anliegen anlegen oder aktivieren.';
|
||||
}
|
||||
|
||||
$rules = $this->rules((array) ($draft['rules'] ?? []), $fields, $tree, $hasIssue, $position, $stepKeys, $usedGroups, $errors);
|
||||
|
||||
$definition = [
|
||||
'v' => 1,
|
||||
'texts' => FormTexts::merge(FormTexts::clean((array) ($draft['texts'] ?? []))),
|
||||
'has_issue' => $hasIssue,
|
||||
'issues' => $hasIssue ? $tree : [],
|
||||
'fields' => $fields,
|
||||
'steps' => $steps,
|
||||
'rules' => $rules,
|
||||
];
|
||||
|
||||
return ['definition' => $errors === [] ? $definition : null, 'errors' => array_values(array_unique($errors))];
|
||||
}
|
||||
|
||||
/**
|
||||
* Vollständige Auflösung ohne Fehlerfreiheit: für die Vorschau eines unfertigen Entwurfs.
|
||||
*
|
||||
* @param array<string, mixed> $draft
|
||||
* @return array{definition: ?array<string, mixed>, errors: list<string>}
|
||||
*/
|
||||
public function preview(array $draft): array
|
||||
{
|
||||
return $this->compile($draft);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $only
|
||||
* @return list<array{key: string, label: string, description: ?string, children: list<array{key: string, label: string, description: ?string}>}>
|
||||
*/
|
||||
private function issueTree(array $only): array
|
||||
{
|
||||
$tree = InquiryIssue::tree();
|
||||
|
||||
if ($only === []) {
|
||||
return $tree;
|
||||
}
|
||||
|
||||
return array_values(array_filter($tree, fn (array $issue): bool => in_array($issue['key'], $only, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed> $rules
|
||||
* @param array<string, array<string, mixed>> $fields
|
||||
* @param list<array<string, mixed>> $tree
|
||||
* @param array<string, int> $position
|
||||
* @param list<string> $stepKeys
|
||||
* @param array<string, true> $usedGroups
|
||||
* @param list<string> $errors
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function rules(array $rules, array $fields, array $tree, bool $hasIssue, array $position, array $stepKeys, array $usedGroups, array &$errors): array
|
||||
{
|
||||
$normalized = [];
|
||||
$issueKeys = array_column($tree, 'key');
|
||||
$subKeys = [];
|
||||
foreach ($tree as $issue) {
|
||||
foreach ($issue['children'] as $child) {
|
||||
$subKeys[] = $child['key'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_values($rules) as $index => $rule) {
|
||||
$number = $index + 1;
|
||||
$rule = is_array($rule) ? $rule : [];
|
||||
$effect = (string) ($rule['effect'] ?? '');
|
||||
$target = (string) ($rule['target'] ?? '');
|
||||
|
||||
if (! isset(self::EFFECTS[$effect])) {
|
||||
$errors[] = "Regel $number: Die Wirkung ist ungültig.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
[$targetKind] = array_pad(explode(':', $target, 2), 2, '');
|
||||
|
||||
if (! isset($position[$target])) {
|
||||
$errors[] = "Regel $number: Das Ziel „{$target}“ gibt es in dieser Vorlage nicht.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$kindOk = match ($effect) {
|
||||
'show', 'hide' => in_array($targetKind, ['group', 'field'], true),
|
||||
'require', 'preset' => $targetKind === 'field',
|
||||
'skip' => $targetKind === 'step',
|
||||
default => false,
|
||||
};
|
||||
|
||||
if (! $kindOk) {
|
||||
$errors[] = "Regel $number: Die Wirkung „".self::EFFECTS[$effect].'“ passt nicht zum Ziel.';
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($target, ['field:anliegen', 'field:unteranliegen'], true)) {
|
||||
$errors[] = "Regel $number: Die Auswahl des Anliegens lässt sich nicht durch eine Regel steuern.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$presetValue = null;
|
||||
if ($effect === 'preset') {
|
||||
$field = $fields[substr($target, 6)] ?? null;
|
||||
$presetValue = $rule['value'] ?? null;
|
||||
|
||||
if ($field === null || ! in_array($field['type'], ['select', 'boolean'], true)) {
|
||||
$errors[] = "Regel $number: Vorbelegt werden können nur Einfachauswahl- und Ja/Nein-Felder.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$valid = array_column($field['options'] ?: [['key' => 'ja'], ['key' => 'nein']], 'key');
|
||||
if ($field['type'] === 'boolean') {
|
||||
$valid = ['ja', 'nein'];
|
||||
}
|
||||
|
||||
if (! is_string($presetValue) || ! in_array($presetValue, $valid, true)) {
|
||||
$errors[] = "Regel $number: Der Wert zum Vorbelegen ist kein Auswahlwert des Feldes.";
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$match = ($rule['match'] ?? 'all') === 'any' ? 'any' : 'all';
|
||||
$conditions = [];
|
||||
|
||||
foreach (array_values((array) ($rule['conditions'] ?? [])) as $c => $condition) {
|
||||
$condition = is_array($condition) ? $condition : [];
|
||||
$source = (string) ($condition['source'] ?? '');
|
||||
$op = (string) ($condition['op'] ?? 'eq');
|
||||
$values = array_values(array_filter((array) ($condition['values'] ?? []), fn (mixed $v): bool => is_string($v) && $v !== ''));
|
||||
|
||||
if (! isset(self::OPERATORS[$op])) {
|
||||
$errors[] = "Regel $number, Bedingung ".($c + 1).': Der Vergleich ist ungültig.';
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($op, ['eq', 'neq'], true)) {
|
||||
$values = array_slice($values, 0, 1);
|
||||
}
|
||||
|
||||
if ($values === []) {
|
||||
$errors[] = "Regel $number, Bedingung ".($c + 1).': Es fehlt ein Vergleichswert.';
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$sourceKey = "field:$source";
|
||||
|
||||
if ($source === 'anliegen' || $source === 'unteranliegen') {
|
||||
if (! $hasIssue) {
|
||||
$errors[] = "Regel $number: Die Vorlage hat keine Auswahl des Anliegens.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$valid = $source === 'anliegen' ? $issueKeys : $subKeys;
|
||||
} else {
|
||||
$field = $fields[$source] ?? null;
|
||||
|
||||
if ($field === null || ! isset($position[$sourceKey])) {
|
||||
$errors[] = "Regel $number: Die Bedingung bezieht sich auf ein Feld, das es in der Vorlage nicht gibt.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! FieldType::from($field['type'])->canControl()) {
|
||||
$errors[] = "Regel $number: Bedingungen sind nur für Auswahlfelder und Ja/Nein-Felder möglich („{$field['label']}“).";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$valid = $field['type'] === 'boolean' ? ['ja', 'nein'] : array_column($field['options'], 'key');
|
||||
}
|
||||
|
||||
if (array_diff($values, $valid) !== []) {
|
||||
$errors[] = "Regel $number: Ein Vergleichswert ist kein Auswahlwert von „{$source}“.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($position[$sourceKey] >= $position[$target]) {
|
||||
$errors[] = "Regel $number: Die Bedingung „{$source}“ muss in der Vorlage vor ihrem Ziel stehen, sonst kann sie noch nicht ausgewertet werden.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$conditions[] = ['source' => $source, 'op' => $op, 'values' => $values];
|
||||
}
|
||||
|
||||
if ($conditions === [] || count($conditions) !== count((array) ($rule['conditions'] ?? []))) {
|
||||
if ($conditions === []) {
|
||||
$errors[] = "Regel $number hat keine gültige Bedingung.";
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalized[] = [
|
||||
'effect' => $effect,
|
||||
'target' => $target,
|
||||
'match' => $match,
|
||||
'conditions' => $conditions,
|
||||
'value' => $presetValue,
|
||||
];
|
||||
}
|
||||
|
||||
// Widersprüche: gleiche Bedingung zeigt und blendet dasselbe Ziel aus.
|
||||
$seen = [];
|
||||
foreach ($normalized as $rule) {
|
||||
if (! in_array($rule['effect'], ['show', 'hide'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$signature = json_encode([$rule['target'], $rule['match'], $rule['conditions']]);
|
||||
$opposite = $rule['effect'] === 'show' ? 'hide' : 'show';
|
||||
|
||||
if (isset($seen[$opposite][$signature])) {
|
||||
$errors[] = 'Widerspruch: Das Ziel „'.$rule['target'].'“ wird unter derselben Bedingung angezeigt und ausgeblendet.';
|
||||
}
|
||||
|
||||
$seen[$rule['effect']][$signature] = true;
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
private function text(mixed $value, int $max): ?string
|
||||
{
|
||||
if (! is_string($value) || trim($value) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return mb_substr(trim($value), 0, $max);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user