1 | <?php |
||||
2 | |||||
3 | namespace NovaFlexibleContent\Layouts\LayoutTraits; |
||||
4 | |||||
5 | use Illuminate\Support\Str; |
||||
6 | use Laravel\Nova\Fields\Field; |
||||
7 | use NovaFlexibleContent\Http\FlexibleAttribute; |
||||
8 | use NovaFlexibleContent\Http\ScopedRequest; |
||||
9 | |||||
10 | trait HasFieldsRules |
||||
11 | { |
||||
12 | /** |
||||
13 | * Get validation rules for fields concerned by given request. |
||||
14 | */ |
||||
15 | 6 | public function generateRules(ScopedRequest $request, string $key, ?string $type = null): array |
|||
16 | { |
||||
17 | 6 | return $this->fieldsCollection()->map(fn ($field) => $this->getScopedFieldRules($field, $request, $key, $type)) |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
18 | 6 | ->collapse() |
|||
19 | 6 | ->all(); |
|||
20 | } |
||||
21 | |||||
22 | /** |
||||
23 | * Get validation rules for fields concerned by given request. |
||||
24 | */ |
||||
25 | 6 | protected function getScopedFieldRules(Field $field, ScopedRequest $request, string $key, ?string $type = null): array |
|||
26 | { |
||||
27 | 6 | $type = Str::ucfirst($type); |
|||
28 | 6 | $method = "get{$type}Rules"; |
|||
29 | |||||
30 | 6 | $rules = []; |
|||
31 | 6 | if(method_exists($field, $method)) { |
|||
32 | 6 | $rules = call_user_func([$field, $method], $request); |
|||
33 | } |
||||
34 | |||||
35 | 6 | return collect($rules) |
|||
0 ignored issues
–
show
It seems like
$rules can also be of type array ; however, parameter $value of collect() does only seem to accept Illuminate\Contracts\Support\Arrayable , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
36 | 6 | ->mapWithKeys(function ($validatorRules, $attribute) use ($key, $field, $request) { |
|||
37 | 6 | $key = $request->isFileAttribute($attribute) |
|||
38 | 2 | ? $request->getFileAttribute($attribute) |
|||
39 | 6 | : "{$key}.attributes.{$attribute}"; |
|||
40 | |||||
41 | 6 | return [$key => $this->wrapScopedFieldRules($field, $validatorRules)]; |
|||
42 | 6 | }) |
|||
43 | 6 | ->filter() |
|||
44 | 6 | ->all(); |
|||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Wrap the rules in an array containing field information for later use. |
||||
49 | */ |
||||
50 | 6 | protected function wrapScopedFieldRules(Field $field, array $rules = []): array |
|||
51 | { |
||||
52 | 6 | if (is_a($rules['attribute'] ?? null, FlexibleAttribute::class)) { |
|||
53 | 1 | return $rules; |
|||
54 | } |
||||
55 | |||||
56 | 6 | return [ |
|||
57 | 6 | 'attribute' => FlexibleAttribute::make($field->attribute, $this->inUseKey()), |
|||
0 ignored issues
–
show
It seems like
inUseKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
58 | 6 | 'rules' => $rules, |
|||
59 | 6 | ]; |
|||
60 | } |
||||
61 | } |
||||
62 |