|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMartins\Exceptions\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use SMartins\Exceptions\JsonApi\Error; |
|
6
|
|
|
use SMartins\Exceptions\JsonApi\Source; |
|
7
|
|
|
use SMartins\Exceptions\JsonApi\ErrorCollection; |
|
8
|
|
|
|
|
9
|
|
|
class ValidationHandler extends AbstractHandler |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritDoc} |
|
13
|
|
|
*/ |
|
14
|
|
|
public function handle() |
|
15
|
|
|
{ |
|
16
|
|
|
$errors = (new ErrorCollection)->setStatusCode(400); |
|
17
|
|
|
|
|
18
|
|
|
$failedFieldsRules = $this->getFailedFieldsRules(); |
|
19
|
|
|
|
|
20
|
|
|
foreach ($this->getFailedFieldsMessages() as $field => $messages) { |
|
21
|
|
|
foreach ($messages as $key => $message) { |
|
22
|
|
|
$code = $this->getValidationCode($failedFieldsRules, $key, $field); |
|
23
|
|
|
$title = $this->getValidationTitle($failedFieldsRules, $key, $field); |
|
24
|
|
|
|
|
25
|
|
|
$error = (new Error)->setStatus(422) |
|
26
|
|
|
->setSource((new Source())->setPointer($field)) |
|
27
|
|
|
->setTitle($title ?? $this->getDefaultTitle()) |
|
28
|
|
|
->setDetail($message); |
|
29
|
|
|
|
|
30
|
|
|
if (! is_null($code)) { |
|
31
|
|
|
$error->setCode($code); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$errors->push($error); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $errors; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the title of response based on rules and field getting from translations. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $failedFieldsRules |
|
45
|
|
|
* @param string $key |
|
46
|
|
|
* @param string $field |
|
47
|
|
|
* @return string|null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getValidationTitle(array $failedFieldsRules, string $key, string $field) |
|
50
|
|
|
{ |
|
51
|
|
|
$title = __('exception::exceptions.validation.title', [ |
|
52
|
|
|
'fails' => array_keys($failedFieldsRules[$field])[$key], |
|
53
|
|
|
'field' => $field, |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
return is_array($title) ? $title[0] : $title; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the code of validation error from config. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $failedFieldsRules |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @param string $field |
|
65
|
|
|
* @return string|null |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getValidationCode(array $failedFieldsRules, string $key, string $field) |
|
68
|
|
|
{ |
|
69
|
|
|
$rule = strtolower(array_keys($failedFieldsRules[$field])[$key]); |
|
70
|
|
|
|
|
71
|
|
|
return config('json-exception-handler.codes.validation_fields.'.$field.'.'.$rule); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get message based on exception type. If exception is generated by |
|
76
|
|
|
* $this->validate() from default Controller methods the exception has the |
|
77
|
|
|
* response object. If exception is generated by Validator::make() the |
|
78
|
|
|
* messages are get different. |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getFailedFieldsMessages(): array |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->exception->validator->messages()->messages(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the rules failed on fields. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getFailedFieldsRules(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->exception->validator->failed(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|