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>
287 lines
9.6 KiB
PHP
287 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Communication;
|
|
|
|
use Carbon\Carbon;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Prüft und bereinigt einen einzelnen Antwortwert nach dem Feldtyp. Die einzige Stelle, an der Werte eines Besuchers
|
|
* in gespeicherte Werte übergehen. Eingabe ist die Feldbeschreibung aus der Fassung (siehe InquiryField::snapshot()).
|
|
*/
|
|
final class FieldValue
|
|
{
|
|
public const TEXT_MAX = 200;
|
|
|
|
public const TEXTAREA_MAX = 4000;
|
|
|
|
/**
|
|
* Rohwert für Bedingungen: ungültige oder fehlende Werte zählen als nicht gesetzt (null).
|
|
*
|
|
* @param array<string, mixed> $field
|
|
*/
|
|
public static function forRules(array $field, mixed $raw): string|array|null
|
|
{
|
|
$type = FieldType::tryFrom((string) ($field['type'] ?? ''));
|
|
|
|
return match ($type) {
|
|
FieldType::Select => self::option($field, $raw),
|
|
FieldType::Multiselect => self::options($field, $raw) ?: null,
|
|
FieldType::Boolean => self::yesNo($raw),
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $field
|
|
* @return array{0: mixed, 1: ?string} bereinigter Wert (null = nicht beantwortet) und Fehlermeldung
|
|
*/
|
|
public static function check(array $field, mixed $raw, bool $required): array
|
|
{
|
|
$type = FieldType::tryFrom((string) ($field['type'] ?? ''));
|
|
$label = (string) ($field['label'] ?? 'Dieses Feld');
|
|
$settings = (array) ($field['settings'] ?? []);
|
|
$missing = 'Bitte füllen Sie dieses Feld aus.';
|
|
|
|
if ($type === null) {
|
|
return [null, 'Dieses Feld ist nicht verfügbar.'];
|
|
}
|
|
|
|
if (is_string($raw)) {
|
|
$raw = str_replace(["\r\n", "\r"], "\n", $raw);
|
|
$raw = preg_replace('/[^\P{C}\n\t]/u', '', $raw) ?? '';
|
|
$raw = trim($raw);
|
|
}
|
|
|
|
$empty = $raw === null || $raw === '' || $raw === [] || (is_array($raw) && $type === FieldType::Period && trim((string) ($raw['von'] ?? '')) === '' && trim((string) ($raw['bis'] ?? '')) === '');
|
|
|
|
if ($empty) {
|
|
return [null, $required ? $missing : null];
|
|
}
|
|
|
|
switch ($type) {
|
|
case FieldType::Text:
|
|
case FieldType::Textarea:
|
|
if (! is_string($raw)) {
|
|
return [null, 'Bitte geben Sie einen Text ein.'];
|
|
}
|
|
|
|
$max = (int) ($settings['max_length'] ?? ($type === FieldType::Text ? self::TEXT_MAX : self::TEXTAREA_MAX));
|
|
$max = max(1, min($max, $type === FieldType::Text ? 500 : 8000));
|
|
|
|
if ($type === FieldType::Text) {
|
|
$raw = preg_replace('/\s*\n\s*/', ' ', $raw) ?? $raw;
|
|
}
|
|
|
|
if (mb_strlen($raw) > $max) {
|
|
return [null, "Bitte höchstens $max Zeichen."];
|
|
}
|
|
|
|
return [$raw, null];
|
|
|
|
case FieldType::Email:
|
|
if (! is_string($raw) || mb_strlen($raw) > 200 || filter_var($raw, FILTER_VALIDATE_EMAIL) === false) {
|
|
return [null, 'Bitte geben Sie eine gültige E-Mail-Adresse ein.'];
|
|
}
|
|
|
|
return [$raw, null];
|
|
|
|
case FieldType::Phone:
|
|
if (! is_string($raw) || preg_match('/^\+?[0-9][0-9 ()\/.\-]{3,28}[0-9]$/', $raw) !== 1) {
|
|
return [null, 'Bitte geben Sie eine gültige Telefonnummer ein.'];
|
|
}
|
|
|
|
return [$raw, null];
|
|
|
|
case FieldType::Number:
|
|
if (is_string($raw)) {
|
|
$raw = str_replace(',', '.', $raw);
|
|
}
|
|
|
|
if (! is_numeric($raw) || is_nan((float) $raw) || is_infinite((float) $raw)) {
|
|
return [null, 'Bitte geben Sie eine Zahl ein.'];
|
|
}
|
|
|
|
$number = $raw + 0;
|
|
|
|
if (isset($settings['min']) && $settings['min'] !== '' && $number < (float) $settings['min']) {
|
|
return [null, 'Bitte mindestens '.self::plain($settings['min']).'.'];
|
|
}
|
|
|
|
if (isset($settings['max']) && $settings['max'] !== '' && $number > (float) $settings['max']) {
|
|
return [null, 'Bitte höchstens '.self::plain($settings['max']).'.'];
|
|
}
|
|
|
|
return [$number, null];
|
|
|
|
case FieldType::Select:
|
|
$option = self::option($field, $raw);
|
|
|
|
return $option === null ? [null, 'Bitte wählen Sie einen der angebotenen Werte.'] : [$option, null];
|
|
|
|
case FieldType::Multiselect:
|
|
if (! is_array($raw)) {
|
|
return [null, 'Bitte wählen Sie aus den angebotenen Werten.'];
|
|
}
|
|
|
|
$values = self::options($field, $raw);
|
|
|
|
if ($values === [] || count($values) !== count(array_unique(array_filter($raw, 'is_string')))) {
|
|
return [null, 'Bitte wählen Sie aus den angebotenen Werten.'];
|
|
}
|
|
|
|
return [$values, null];
|
|
|
|
case FieldType::Boolean:
|
|
$answer = self::yesNo($raw);
|
|
|
|
return $answer === null ? [null, 'Bitte antworten Sie mit Ja oder Nein.'] : [$answer, null];
|
|
|
|
case FieldType::Date:
|
|
$date = self::date($raw);
|
|
|
|
return $date === null ? [null, 'Bitte geben Sie ein gültiges Datum ein.'] : [$date, null];
|
|
|
|
case FieldType::Period:
|
|
if (! is_array($raw)) {
|
|
return [null, 'Bitte geben Sie einen Zeitraum an.'];
|
|
}
|
|
|
|
$from = trim((string) ($raw['von'] ?? '')) === '' ? null : self::date($raw['von']);
|
|
$to = trim((string) ($raw['bis'] ?? '')) === '' ? null : self::date($raw['bis']);
|
|
|
|
if ((trim((string) ($raw['von'] ?? '')) !== '' && $from === null) || (trim((string) ($raw['bis'] ?? '')) !== '' && $to === null)) {
|
|
return [null, 'Bitte geben Sie gültige Daten ein.'];
|
|
}
|
|
|
|
if ($required && ($from === null || $to === null)) {
|
|
return [null, 'Bitte geben Sie Beginn und Ende an.'];
|
|
}
|
|
|
|
if ($from !== null && $to !== null && $to < $from) {
|
|
return [null, 'Das Ende liegt vor dem Beginn.'];
|
|
}
|
|
|
|
return [['von' => $from, 'bis' => $to], null];
|
|
}
|
|
|
|
return [null, "$label ist nicht verfügbar."];
|
|
}
|
|
|
|
/**
|
|
* Lesbare Darstellung eines bereinigten Wertes, wie sie in der Anfrage gespeichert wird.
|
|
*
|
|
* @param array<string, mixed> $field
|
|
*/
|
|
public static function display(array $field, mixed $value): string
|
|
{
|
|
$type = FieldType::tryFrom((string) ($field['type'] ?? ''));
|
|
$labels = [];
|
|
|
|
foreach ((array) ($field['options'] ?? []) as $option) {
|
|
$labels[$option['key']] = $option['label'];
|
|
}
|
|
|
|
return match ($type) {
|
|
FieldType::Select => (string) ($labels[$value] ?? $value),
|
|
FieldType::Multiselect => implode(', ', array_map(fn (string $key): string => (string) ($labels[$key] ?? $key), (array) $value)),
|
|
FieldType::Boolean => $value === 'ja' ? 'Ja' : 'Nein',
|
|
FieldType::Date => Carbon::parse((string) $value)->format('d.m.Y'),
|
|
FieldType::Period => self::periodText((array) $value),
|
|
default => (string) $value,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $field
|
|
*/
|
|
private static function option(array $field, mixed $raw): ?string
|
|
{
|
|
if (! is_string($raw)) {
|
|
return null;
|
|
}
|
|
|
|
foreach ((array) ($field['options'] ?? []) as $option) {
|
|
if (($option['key'] ?? null) === $raw) {
|
|
return $raw;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $field
|
|
* @return list<string>
|
|
*/
|
|
private static function options(array $field, mixed $raw): array
|
|
{
|
|
if (! is_array($raw)) {
|
|
return [];
|
|
}
|
|
|
|
$values = [];
|
|
|
|
foreach ($raw as $item) {
|
|
$option = self::option($field, $item);
|
|
|
|
if ($option === null) {
|
|
return [];
|
|
}
|
|
|
|
$values[$option] = $option;
|
|
}
|
|
|
|
return array_values($values);
|
|
}
|
|
|
|
private static function yesNo(mixed $raw): ?string
|
|
{
|
|
return match (true) {
|
|
$raw === true, $raw === 'ja', $raw === '1', $raw === 'true', $raw === 1 => 'ja',
|
|
$raw === false, $raw === 'nein', $raw === '0', $raw === 'false', $raw === 0 => 'nein',
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
private static function date(mixed $raw): ?string
|
|
{
|
|
if (! is_string($raw) || preg_match('/^\d{4}-\d{2}-\d{2}$/', $raw) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$date = Carbon::createFromFormat('!Y-m-d', $raw);
|
|
} catch (Throwable) {
|
|
return null;
|
|
}
|
|
|
|
if ($date === false || $date->format('Y-m-d') !== $raw || $date->year < 1900 || $date->year > 2200) {
|
|
return null;
|
|
}
|
|
|
|
return $raw;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $value
|
|
*/
|
|
private static function periodText(array $value): string
|
|
{
|
|
$from = isset($value['von']) ? Carbon::parse($value['von'])->format('d.m.Y') : null;
|
|
$to = isset($value['bis']) ? Carbon::parse($value['bis'])->format('d.m.Y') : null;
|
|
|
|
return match (true) {
|
|
$from !== null && $to !== null => "$from bis $to",
|
|
$from !== null => "ab $from",
|
|
$to !== null => "bis $to",
|
|
default => '',
|
|
};
|
|
}
|
|
|
|
private static function plain(mixed $number): string
|
|
{
|
|
return rtrim(rtrim(number_format((float) $number, 4, ',', ''), '0'), ',');
|
|
}
|
|
}
|