| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Exceptions; |
| 6 |
|
| 7 |
/** |
| 8 |
* Validation Exception |
| 9 |
* |
| 10 |
* Thrown when input validation fails |
| 11 |
*/ |
| 12 |
class ValidationException extends YatraException |
| 13 |
{ |
| 14 |
protected string $errorCode = 'validation_error'; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var array Validation errors by field |
| 18 |
*/ |
| 19 |
protected array $errors = []; |
| 20 |
|
| 21 |
public function __construct(string $message = 'Validation failed', array $errors = [], int $code = 400, ?\Exception $previous = null) |
| 22 |
{ |
| 23 |
$this->errors = $errors; |
| 24 |
parent::__construct($message, $code, $previous, ['validation_errors' => $errors]); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get validation errors |
| 29 |
*/ |
| 30 |
public function getErrors(): array |
| 31 |
{ |
| 32 |
return $this->errors; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Add validation error for a field |
| 37 |
*/ |
| 38 |
public function addError(string $field, string $message): self |
| 39 |
{ |
| 40 |
$this->errors[$field][] = $message; |
| 41 |
$this->context['validation_errors'] = $this->errors; |
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check if field has errors |
| 47 |
*/ |
| 48 |
public function hasError(string $field): bool |
| 49 |
{ |
| 50 |
return isset($this->errors[$field]) && !empty($this->errors[$field]); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get errors for specific field |
| 55 |
*/ |
| 56 |
public function getFieldErrors(string $field): array |
| 57 |
{ |
| 58 |
return $this->errors[$field] ?? []; |
| 59 |
} |
| 60 |
} |
| 61 |
|