-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathValidationHandler.php
More file actions
109 lines (92 loc) · 3.03 KB
/
Copy pathValidationHandler.php
File metadata and controls
109 lines (92 loc) · 3.03 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
<?php
namespace SMartins\Exceptions\Handlers;
use SMartins\Exceptions\JsonApi\Error;
use SMartins\Exceptions\JsonApi\ErrorCollection;
use SMartins\Exceptions\JsonApi\Source;
class ValidationHandler extends AbstractHandler
{
/**
* {@inheritdoc}
*/
public function handle()
{
$errors = (new ErrorCollection)->setStatusCode(422);
$failedFieldsRules = $this->getFailedFieldsRules();
foreach ($this->getFailedFieldsMessages() as $field => $messages) {
foreach ($messages as $key => $message) {
$code = $this->getValidationCode($failedFieldsRules, $key, $field);
$title = $this->getValidationTitle($failedFieldsRules, $key, $field);
$error = (new Error)->setStatus(422)
->setSource((new Source())->setPointer($field))
->setTitle($title ?? $this->getDefaultTitle())
->setDetail($message);
if (! is_null($code)) {
$error->setCode($code);
}
$errors->push($error);
}
}
return $errors;
}
/**
* Get the title of response based on rules and field getting from translations.
*
* @param array $failedFieldsRules
* @param string $key
* @param string $field
* @return string|null
*/
public function getValidationTitle(array $failedFieldsRules, string $key, string $field)
{
if (empty($failedFieldsRules)) {
$fails = '';
} else {
$fails = strtolower(array_keys($failedFieldsRules[$field])[$key]);
}
$title = __('exception::exceptions.validation.title', [
'fails' => $fails,
'field' => $field,
]);
if (is_array($title)) {
$title = $title[0];
}
return ucfirst(trim($title));
}
/**
* Get the code of validation error from config.
*
* @param array $failedFieldsRules
* @param string $key
* @param string $field
* @return string|null
*/
public function getValidationCode(array $failedFieldsRules, string $key, string $field)
{
if (empty($failedFieldsRules)) {
return config('json-exception-handler.codes.validation');
}
$rule = strtolower(array_keys($failedFieldsRules[$field])[$key]);
return config('json-exception-handler.codes.validation_fields.' . $field . '.' . $rule);
}
/**
* Get message based on exception type. If exception is generated by
* $this->validate() from default Controller methods the exception has the
* response object. If exception is generated by Validator::make() the
* messages are get different.
*
* @return array
*/
public function getFailedFieldsMessages(): array
{
return $this->exception->validator->messages()->messages();
}
/**
* Get the rules failed on fields.
*
* @return array
*/
public function getFailedFieldsRules(): array
{
return $this->exception->validator->failed();
}
}