-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathValidationException.php
More file actions
executable file
·122 lines (105 loc) · 2.85 KB
/
Copy pathValidationException.php
File metadata and controls
executable file
·122 lines (105 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Validation;
use Hyperf\Collection\Arr;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ValidatorInterface;
use Hyperf\Server\Exception\ServerException;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use function Hyperf\Tappable\tap;
class ValidationException extends ServerException
{
/**
* The status code to use for the response.
*/
public int $status = 422;
/**
* The path the client should be redirected to.
*/
public ?string $redirectTo = null;
/**
* Create a new exception instance.
*
* @param ValidatorInterface $validator the validator instance
* @param null|ResponseInterface $response the recommended response to send to the client
* @param string $errorBag the name of the error bag
*/
public function __construct(
public ValidatorInterface $validator,
public ?ResponseInterface $response = null,
public string $errorBag = 'default'
) {
parent::__construct('The given data was invalid.');
}
/**
* Create a new validation exception from a plain array of messages.
*
* @return static
*/
public static function withMessages(array $messages)
{
$factory = ApplicationContext::getContainer()->get(ValidatorFactoryInterface::class);
return new static(tap($factory->make([], []), function ($validator) use ($messages) {
foreach ($messages as $key => $value) {
foreach (Arr::wrap($value) as $message) {
$validator->errors()->add($key, $message);
}
}
}));
}
/**
* Get all of the validation error messages.
*/
public function errors(): array
{
return $this->validator->errors()->messages();
}
/**
* Set the HTTP status code to be used for the response.
*
* @return $this
*/
public function status(int $status)
{
$this->status = $status;
return $this;
}
/**
* Set the error bag on the exception.
*
* @return $this
*/
public function errorBag(string $errorBag)
{
$this->errorBag = $errorBag;
return $this;
}
/**
* Set the URL to redirect to on a validation error.
*
* @return $this
*/
public function redirectTo(string $url)
{
$this->redirectTo = $url;
return $this;
}
/**
* Get the underlying response instance.
*
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}