85 lines
2.0 KiB
PHP
85 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Framework\Validation;
|
|
|
|
final class ValidationResult
|
|
{
|
|
/** @var array<string, string[]> */
|
|
private array $errors = [];
|
|
|
|
/**
|
|
* Fügt eine Fehlermeldung für ein Feld hinzu
|
|
*/
|
|
public function addError(string $field, string $message): void
|
|
{
|
|
$this->errors[$field][] = $message;
|
|
}
|
|
|
|
/**
|
|
* Fügt mehrere Fehlermeldungen für ein Feld hinzu
|
|
*
|
|
* @param string $field Feldname
|
|
* @param array<string> $messages Liste von Fehlermeldungen
|
|
*/
|
|
public function addErrors(string $field, array $messages): void
|
|
{
|
|
foreach ($messages as $message) {
|
|
$this->addError($field, $message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob Fehler vorhanden sind
|
|
*/
|
|
public function hasErrors(): bool
|
|
{
|
|
return !empty($this->errors);
|
|
}
|
|
|
|
/**
|
|
* Gibt alle Fehlermeldungen für ein bestimmtes Feld zurück
|
|
*
|
|
* @param string $field Feldname
|
|
* @return array<string> Liste der Fehlermeldungen für das Feld
|
|
*/
|
|
public function getFieldErrors(string $field): array
|
|
{
|
|
return $this->errors[$field] ?? [];
|
|
}
|
|
|
|
/**
|
|
* Gibt alle Fehlermeldungen als flache Liste zurück
|
|
*
|
|
* @return array<string> Liste aller Fehlermeldungen
|
|
*/
|
|
public function getAllErrorMessages(): array
|
|
{
|
|
$messages = [];
|
|
foreach ($this->errors as $fieldErrors) {
|
|
foreach ($fieldErrors as $error) {
|
|
$messages[] = $error;
|
|
}
|
|
}
|
|
return $messages;
|
|
}
|
|
|
|
/**
|
|
* Kombiniert zwei Validierungsergebnisse
|
|
*/
|
|
public function merge(ValidationResult $other): self
|
|
{
|
|
foreach ($other->errors as $field => $messages) {
|
|
foreach ($messages as $message) {
|
|
$this->addError($field, $message);
|
|
}
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
public function getAll():array
|
|
{
|
|
return $this->errors;
|
|
}
|
|
}
|