1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaFlexibleContent\Http; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
use Illuminate\Support\Facades\Lang; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use NovaFlexibleContent\Flexible; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
trait TransformsFlexibleErrors |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Checks whether the given response's flexible errors can and should be transformed |
15
|
|
|
* |
16
|
|
|
* @param \Symfony\Component\HttpFoundation\Response $response |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
protected function shouldTransformFlexibleErrors(Response $response) |
20
|
|
|
{ |
21
|
|
|
return $response->getStatusCode() === Response::HTTP_UNPROCESSABLE_ENTITY |
22
|
|
|
&& is_a($response, JsonResponse::class); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Updates given response's errors for the concerned flexible fields |
27
|
|
|
* |
28
|
|
|
* @param Response $response |
29
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
30
|
|
|
*/ |
31
|
|
|
protected function transformFlexibleErrors(Response $response) |
32
|
|
|
{ |
33
|
|
|
$response->setData( |
|
|
|
|
34
|
|
|
$this->updateResponseErrors($response->original) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
return $response; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Run response errors parsing if necessary |
42
|
|
|
* |
43
|
|
|
* @param array $data |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
protected function updateResponseErrors($data) |
47
|
|
|
{ |
48
|
|
|
if (!($data['errors'] ?? null)) { |
49
|
|
|
return $data; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$data['errors'] = $this->getTransformedErrors($data['errors']); |
53
|
|
|
|
54
|
|
|
return $data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Transforms the original errors array in a nested |
59
|
|
|
* array structure. |
60
|
|
|
* |
61
|
|
|
* @param array $errors |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
protected function getTransformedErrors($errors): array |
65
|
|
|
{ |
66
|
|
|
$parsed = []; |
67
|
|
|
|
68
|
|
|
foreach ($errors as $key => $messages) { |
69
|
|
|
$attribute = Flexible::getValidationKey($key); |
70
|
|
|
|
71
|
|
|
if (!$attribute) { |
72
|
|
|
$parsed[$key] = $messages; |
73
|
|
|
|
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$parsed[$attribute->original] = $this->transformMessages($messages, $key, $attribute); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $parsed; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Update human error messages with correct field names |
85
|
|
|
* |
86
|
|
|
* @param array $messages |
87
|
|
|
* @param string $key |
88
|
|
|
* @param \NovaFlexibleContent\Http\FlexibleAttribute $attribute |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function transformMessages($messages, $key, $attribute) |
92
|
|
|
{ |
93
|
|
|
$search = str_replace('_', ' ', Str::snake($key)); |
94
|
|
|
$attribute = str_replace('_', ' ', Str::snake($attribute->name)); |
95
|
|
|
|
96
|
|
|
// We translate the attribute if it exists |
97
|
|
|
if (Lang::has('validation.attributes.'.$attribute)) { |
98
|
|
|
$attribute = trans('validation.attributes.'.$attribute); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return array_map(function ($message) use ($search, $attribute) { |
102
|
|
|
return str_replace( |
103
|
|
|
[$search, Str::upper($search), Str::ucfirst($search)], |
104
|
|
|
[$attribute, Str::upper($attribute), Str::ucfirst($attribute)], |
105
|
|
|
$message |
106
|
|
|
); |
107
|
|
|
}, $messages); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.