Files
laraundfelix/app/Domain/Communication/TemplateDraft.php
T
LarsandClaude Sonnet 5.5 c6a8167601 Baue die Verwaltung Kommunikation, die öffentliche Anfrageschnittstelle und das CMS-Widget Anfrage
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>
2026-11-27 17:41:55 +01:00

76 lines
2.9 KiB
PHP

<?php
namespace App\Domain\Communication;
/**
* Bereinigt den Entwurf einer Anfragevorlage aus dem Formular der Verwaltung: Listen mit fortlaufenden Schlüsseln
* (Repeater liefern Zufallsschlüssel), nur bekannte Felder, Texte begrenzt. Eine inhaltliche Prüfung geschieht erst beim
* Veröffentlichen (TemplateCompiler); ein unfertiger Entwurf darf gespeichert werden.
*/
final class TemplateDraft
{
/**
* @param array<string, mixed> $draft
* @return array<string, mixed>
*/
public static function clean(array $draft): array
{
$steps = [];
foreach (array_values((array) ($draft['steps'] ?? [])) as $step) {
$step = is_array($step) ? $step : [];
$items = [];
foreach (array_values((array) ($step['items'] ?? [])) as $item) {
$item = is_array($item) ? $item : [];
$kind = (string) ($item['kind'] ?? '');
$items[] = match ($kind) {
'issue' => ['kind' => 'issue'],
'group' => ['kind' => 'group', 'group' => (string) ($item['group'] ?? '')],
'field' => ['kind' => 'field', 'field' => (string) ($item['field'] ?? ''), 'required' => (bool) ($item['required'] ?? false)],
default => ['kind' => $kind],
};
}
$steps[] = [
'key' => (string) ($step['key'] ?? ''),
'title' => mb_substr(trim((string) ($step['title'] ?? '')), 0, 120),
'intro' => filled($step['intro'] ?? null) ? mb_substr(trim((string) $step['intro']), 0, 600) : null,
'items' => $items,
];
}
$rules = [];
foreach (array_values((array) ($draft['rules'] ?? [])) as $rule) {
$rule = is_array($rule) ? $rule : [];
$conditions = [];
foreach (array_values((array) ($rule['conditions'] ?? [])) as $condition) {
$condition = is_array($condition) ? $condition : [];
$conditions[] = [
'source' => (string) ($condition['source'] ?? ''),
'op' => (string) ($condition['op'] ?? 'eq'),
'values' => array_values(array_filter((array) ($condition['values'] ?? []), 'is_string')),
];
}
$rules[] = [
'effect' => (string) ($rule['effect'] ?? ''),
'target' => (string) ($rule['target'] ?? ''),
'match' => ($rule['match'] ?? 'all') === 'any' ? 'any' : 'all',
'conditions' => $conditions,
'value' => filled($rule['value'] ?? null) ? (string) $rule['value'] : null,
];
}
return [
'texts' => FormTexts::clean((array) ($draft['texts'] ?? [])),
'issues' => array_values(array_filter((array) ($draft['issues'] ?? []), 'is_string')),
'steps' => $steps,
'rules' => $rules,
];
}
}