Issues (115)

src/Requests/ResourceFormRequest.php (3 issues)

1
<?php
2
3
namespace VGirol\JsonApi\Requests;
4
5
use Illuminate\Validation\Rule;
6
use Illuminate\Validation\Rules\Unique;
7
use VGirol\JsonApiConstant\Members;
8
9
abstract class ResourceFormRequest extends AbstractFormRequest
10
{
11
    /**
12
     * Undocumented function
13
     *
14
     * @return array
15
     */
16
    protected function getCustomRules(): array
17
    {
18
        $star = $this->isCollection ? '.*' : '';
19
        $rules = [];
20
        foreach ($this->rules() as $customKey => $customRules) {
21
            if (is_string($customRules)) {
22
                $customRules = explode('|', $customRules);
23
            }
24
            if (!is_array($customRules)) {
25
                $customRules = [$customRules];
26
            }
27
            $rules[Members::DATA . $star . '.' . Members::ATTRIBUTES . '.' . $customKey] =
28
                $this->transformRules($customRules);
29
        }
30
31
        return $rules;
32
    }
33
34
    /**
35
     * Undocumented function
36
     *
37
     * @return array
38
     */
39
    protected function getCustomMessages(): array
40
    {
41
        $star = $this->isCollection ? '.*' : '';
42
        $mes = [];
43
44
        foreach ($this->preparedRules() as $key => $rules) {
45
            foreach ($rules as $rule) {
46
                if (($rule instanceof Unique) || (is_string($rule) && (strpos($rule, 'unique') !== false))) {
47
                    $mes[$key . '.unique'] = '(409) ' . trans('validation.unique');
0 ignored issues
show
Are you sure trans('validation.unique') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                    $mes[$key . '.unique'] = '(409) ' . /** @scrutinizer ignore-type */ trans('validation.unique');
Loading history...
48
                }
49
            }
50
        }
51
        $mes[Members::DATA . $star . '.' . Members::TYPE . '.in'] = '(409) ' . trans('validation.in');
0 ignored issues
show
Are you sure trans('validation.in') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $mes[Members::DATA . $star . '.' . Members::TYPE . '.in'] = '(409) ' . /** @scrutinizer ignore-type */ trans('validation.in');
Loading history...
52
53
        return array_merge($mes, $this->messages());
54
    }
55
56
    /**
57
     * Undocumented function
58
     *
59
     * @param array $customRules
60
     *
61
     * @return array
62
     */
63
    private function transformRules(array $customRules): array
64
    {
65
        if (!$this->isUpdate()) {
66
            return $customRules;
67
        }
68
69
        $cRules = [];
70
        foreach ($customRules as $customRule) {
71
            // if ($customRule instanceof IlluminateRule) {
72
            //     $cRules[] = $customRule;
73
            //     continue;
74
            // }
75
76
            // No required
77
            if ($customRule == 'required') {
78
                continue;
79
            }
80
81
            // Unique
82
            if (is_string($customRule) && (strpos($customRule, 'unique') !== false)) {
83
                $customRule = str_replace('unique:', null, $customRule);
84
                $a = explode(',', $customRule);
85
                // $a[0] = table, $a[1] = field
86
                $customRule = Rule::unique($a[0], $a[1]);
87
            }
88
            if ($customRule instanceof Unique) {
89
                $customRule->ignore($this->id, jsonapiAliases()->getModelKeyName($this));
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on VGirol\JsonApi\Requests\ResourceFormRequest. Did you maybe forget to declare it?
Loading history...
90
            }
91
            $cRules[] = $customRule;
92
        }
93
94
        return $cRules;
95
    }
96
}
97