| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Exceptions; |
| 6 |
|
| 7 |
/** |
| 8 |
* Exception thrown when validation of request data fails. |
| 9 |
* @see \Metricool\Support\Validation\Validator |
| 10 |
*/ |
| 11 |
class ValidationException extends RestDataException |
| 12 |
{ |
| 13 |
protected int $statusCode = 422; |
| 14 |
|
| 15 |
/** |
| 16 |
* Create a new instance with the given validation errors. |
| 17 |
*/ |
| 18 |
public static function withErrors(array $errors): self |
| 19 |
{ |
| 20 |
$instance = new self(__('Validation failed.', 'metricool')); |
| 21 |
$instance->setData($errors); |
| 22 |
|
| 23 |
return $instance; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get the validation errors, keyed by field name. |
| 28 |
*/ |
| 29 |
public function errors(): array |
| 30 |
{ |
| 31 |
return $this->getData(); |
| 32 |
} |
| 33 |
} |
| 34 |
|