$form wirksames Formular (FormFactory::effective) * @param array $values Eingaben des Besuchers, nach Feldkennung * @return array{steps: array, groups: array, fields: array, required: array, presets: array, 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) { // Ein Bereich lässt sich wie eine Gruppe anzeigen oder ausblenden; „überspringen“ bleibt für ältere Vorlagen gültig. $stepRules = $byTarget['step:'.$step['key']] ?? []; $stepVisible = ! $this->any($stepRules, 'skip', $context) && $this->visible($stepRules, $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 $form * @param array $values * @param array $state * @param array|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> $rules * @param array $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> $rules * @param array $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 $rule * @param array $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 $condition * @param array $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, }; } }