chore: complete update
This commit is contained in:
84
src/Framework/Validation/ValidationResult.php
Normal file
84
src/Framework/Validation/ValidationResult.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user