1 | <?php |
||||
2 | |||||
3 | namespace SMartins\JsonHandler; |
||||
4 | |||||
5 | use Illuminate\Validation\ValidationException; |
||||
0 ignored issues
–
show
|
|||||
6 | |||||
7 | trait ValidationHandler |
||||
8 | { |
||||
9 | /** |
||||
10 | * Assign to response attribute the value to ValidationException. |
||||
11 | * |
||||
12 | * @param ValidationException $exception |
||||
13 | */ |
||||
14 | public function validationException(ValidationException $exception) |
||||
15 | { |
||||
16 | $this->jsonApiResponse->setStatus(422); |
||||
0 ignored issues
–
show
|
|||||
17 | $this->jsonApiResponse->setErrors($this->jsonApiFormatErrorMessages($exception)); |
||||
18 | } |
||||
19 | |||||
20 | /** |
||||
21 | * Get formatted errors on standard code, field, message to each field with |
||||
22 | * error. |
||||
23 | * |
||||
24 | * @param ValidationException $exception |
||||
25 | * @return array |
||||
26 | */ |
||||
27 | public function formattedErrors(ValidationException $exception) |
||||
28 | { |
||||
29 | return $this->jsonApiFormatErrorMessages($exception); |
||||
30 | } |
||||
31 | |||||
32 | public function jsonApiFormatErrorMessages(ValidationException $exception) |
||||
33 | { |
||||
34 | $validationMessages = $this->getTreatedMessages($exception); |
||||
35 | $validationFails = $this->getTreatedFails($exception); |
||||
36 | |||||
37 | $errors = []; |
||||
38 | foreach ($validationMessages as $field => $messages) { |
||||
39 | foreach ($messages as $key => $message) { |
||||
40 | $attributes = $this->getValidationAttributes($validationFails, $key, $field); |
||||
41 | $error = [ |
||||
42 | 'status' => 422, |
||||
43 | 'code' => $attributes['code'], |
||||
44 | 'source' => ['parameter' => $field], |
||||
45 | 'title' => $attributes['title'], |
||||
46 | 'detail' => $message, |
||||
47 | ]; |
||||
48 | array_push($errors, $error); |
||||
49 | } |
||||
50 | } |
||||
51 | |||||
52 | return $errors; |
||||
53 | } |
||||
54 | |||||
55 | public function getValidationAttributes(array $validationFails, $key, $field) |
||||
56 | { |
||||
57 | return [ |
||||
58 | 'code' => $this->getValidationCode($validationFails, $key, $field), |
||||
59 | 'title' => $this->getValidationTitle($validationFails, $key, $field), |
||||
60 | ]; |
||||
61 | } |
||||
62 | |||||
63 | public function getValidationTitle(array $validationFails, $key, $field) |
||||
64 | { |
||||
65 | return __('exception::exceptions.validation.title', [ |
||||
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
66 | 'fails' => array_keys($validationFails[$field])[$key], |
||||
67 | 'field' => $field, |
||||
68 | ]); |
||||
69 | } |
||||
70 | |||||
71 | public function getValidationCode(array $validationFails, $key, $field) |
||||
72 | { |
||||
73 | $rule = strtolower(array_keys($validationFails[$field])[$key]); |
||||
74 | |||||
75 | return config($this->configFile.'.codes.validation_fields.'.$field.'.'.$rule); |
||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * Get message based on exception type. If exception is generated by |
||||
80 | * $this->validate() from default Controller methods the exception has the |
||||
81 | * response object. If exception is generated by Validator::make() the |
||||
82 | * messages are getted different. |
||||
83 | * |
||||
84 | * @param Exception $exception |
||||
0 ignored issues
–
show
|
|||||
85 | * @return array |
||||
86 | */ |
||||
87 | public function getTreatedMessages($exception) |
||||
88 | { |
||||
89 | return $this->getMessagesFromValidator($exception); |
||||
90 | } |
||||
91 | |||||
92 | public function getMessagesFromValidator($exception) |
||||
93 | { |
||||
94 | return $exception->validator->messages()->messages(); |
||||
95 | } |
||||
96 | |||||
97 | public function getTreatedFails($exception) |
||||
98 | { |
||||
99 | return $this->getFailsFromValidator($exception); |
||||
100 | } |
||||
101 | |||||
102 | public function getFailsFromValidator($exception) |
||||
103 | { |
||||
104 | return $exception->validator->failed(); |
||||
105 | } |
||||
106 | } |
||||
107 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths