Files
laraundfelix/app/Domain/Communication/FormEvaluator.php
T
LarsandClaude Sonnet 5.5 2ee1fc9866 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>
2026-11-27 17:26:42 +01:00

211 lines
7.1 KiB
PHP

<?php
namespace App\Domain\Communication;
/**
* Die einzige Auswertung der Bedingungen (ADR-60). Browser, Vorschau und Anfrageservice fragen dieselbe Funktion; der
* Browser entscheidet nichts allein.
*
* Die Auswertung läuft einmal in Dokumentreihenfolge. Eine Bedingung sieht nur Werte von Feldern, die vor ihr stehen und
* selbst sichtbar sind; Werte ausgeblendeter Felder zählen als nicht vorhanden. Daraus folgt: keine Zyklen, und
* nicht mehr relevante Werte erreichen nie die Speicherung.
*/
final class FormEvaluator
{
/**
* @param array<string, mixed> $form wirksames Formular (FormFactory::effective)
* @param array<string, mixed> $values Eingaben des Besuchers, nach Feldkennung
* @return array{steps: array<string, bool>, groups: array<string, bool>, fields: array<string, bool>, required: array<string, bool>, presets: array<string, string>, issue: ?string, subissue: ?string, issue_active: bool}
*/
public function evaluate(array $form, array $values): array
{
$state = ['steps' => [], 'groups' => [], 'fields' => [], 'required' => [], 'presets' => [], 'issue' => null, 'subissue' => null, 'issue_active' => false];
$context = [];
$byTarget = [];
foreach ((array) ($form['rules'] ?? []) as $rule) {
$byTarget[$rule['target']][] = $rule;
}
foreach ((array) $form['steps'] as $step) {
$stepVisible = ! $this->any($byTarget['step:'.$step['key']] ?? [], 'skip', $context);
$state['steps'][$step['key']] = $stepVisible;
foreach ($step['items'] as $item) {
if ($item['kind'] === 'issue') {
$this->issue($form, $values, $stepVisible, $state, $context);
continue;
}
$groupVisible = $stepVisible && ($item['key'] === null || $this->visible($byTarget['group:'.$item['key']] ?? [], $context));
if ($item['key'] !== null) {
$state['groups'][$item['key']] = $groupVisible;
}
foreach ($item['fields'] as $row) {
$key = $row['field'];
$rules = $byTarget["field:$key"] ?? [];
$visible = $groupVisible && $this->visible($rules, $context);
$state['fields'][$key] = $visible;
$state['required'][$key] = $visible && ($row['required'] || $this->any($rules, 'require', $context));
if (! $visible) {
continue;
}
$context[$key] = FieldValue::forRules($form['fields'][$key], $values[$key] ?? null);
foreach ($rules as $rule) {
if ($rule['effect'] === 'preset' && $this->matches($rule, $context)) {
$state['presets'][$key] ??= (string) $rule['value'];
}
}
}
}
}
return $state;
}
/**
* @param array<string, mixed> $form
* @param array<string, mixed> $values
* @param array<string, mixed> $state
* @param array<string, string|array<int, string>|null> $context
*/
private function issue(array $form, array $values, bool $stepVisible, array &$state, array &$context): void
{
$state['fields']['anliegen'] = false;
$state['fields']['unteranliegen'] = false;
$state['required']['anliegen'] = false;
$state['required']['unteranliegen'] = false;
if (! $stepVisible) {
return;
}
$state['issue_active'] = true;
$tops = (array) $form['issues'];
$selected = null;
if (count($tops) === 1) {
$selected = $tops[0];
} else {
$state['fields']['anliegen'] = $tops !== [];
$state['required']['anliegen'] = $tops !== [];
$wanted = $values['anliegen'] ?? null;
foreach ($tops as $top) {
if ($top['key'] === $wanted) {
$selected = $top;
}
}
}
$context['anliegen'] = $selected['key'] ?? null;
$state['issue'] = $selected['key'] ?? null;
if ($selected === null) {
return;
}
$children = (array) $selected['children'];
$child = null;
if (count($children) === 1) {
$child = $children[0];
} elseif (count($children) > 1) {
$state['fields']['unteranliegen'] = true;
$state['required']['unteranliegen'] = true;
$wanted = $values['unteranliegen'] ?? null;
foreach ($children as $candidate) {
if ($candidate['key'] === $wanted) {
$child = $candidate;
}
}
}
$context['unteranliegen'] = $child['key'] ?? null;
$state['subissue'] = $child['key'] ?? null;
}
/**
* Sichtbar, wenn keine „Anzeigen“-Regel besteht oder eine zutrifft, und keine „Ausblenden“-Regel zutrifft.
*
* @param list<array<string, mixed>> $rules
* @param array<string, mixed> $context
*/
private function visible(array $rules, array $context): bool
{
$show = array_filter($rules, fn (array $rule): bool => $rule['effect'] === 'show');
if ($show !== [] && ! $this->any($show, 'show', $context)) {
return false;
}
return ! $this->any($rules, 'hide', $context);
}
/**
* @param list<array<string, mixed>> $rules
* @param array<string, mixed> $context
*/
private function any(array $rules, string $effect, array $context): bool
{
foreach ($rules as $rule) {
if ($rule['effect'] === $effect && $this->matches($rule, $context)) {
return true;
}
}
return false;
}
/**
* @param array<string, mixed> $rule
* @param array<string, mixed> $context
*/
private function matches(array $rule, array $context): bool
{
$results = [];
foreach ($rule['conditions'] as $condition) {
$results[] = $this->condition($condition, $context);
}
if ($results === []) {
return false;
}
return $rule['match'] === 'any' ? in_array(true, $results, true) : ! in_array(false, $results, true);
}
/**
* Eine Bedingung auf einem nicht sichtbaren Feld ist nie erfüllt, auch nicht „ist nicht“.
*
* @param array<string, mixed> $condition
* @param array<string, mixed> $context
*/
private function condition(array $condition, array $context): bool
{
if (! array_key_exists($condition['source'], $context)) {
return false;
}
$actual = $context[$condition['source']];
$wanted = (array) $condition['values'];
$hit = is_array($actual)
? array_intersect($actual, $wanted) !== []
: ($actual !== null && in_array($actual, $wanted, true));
return match ($condition['op']) {
'eq', 'in' => $hit,
'neq', 'not_in' => ! $hit,
default => false,
};
}
}