These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\ValidationRules\Rules; |
||
4 | |||
5 | use Illuminate\Support\Str; |
||
6 | use Illuminate\Contracts\Validation\Rule; |
||
7 | use Illuminate\Support\Facades\Validator; |
||
8 | |||
9 | class Delimited implements Rule |
||
10 | { |
||
11 | /** @var string|array|\Illuminate\Contracts\Validation\Rule */ |
||
12 | protected $rule; |
||
13 | |||
14 | protected $minimum = null; |
||
15 | |||
16 | protected $maximum = null; |
||
17 | |||
18 | protected $allowDuplicates = false; |
||
19 | |||
20 | protected $message = ''; |
||
21 | |||
22 | protected $separatedBy = ','; |
||
23 | |||
24 | /** @var bool */ |
||
25 | protected $trimItems = true; |
||
26 | |||
27 | /** @var string */ |
||
28 | protected $validationMessageWord = 'item'; |
||
29 | |||
30 | 66 | public function __construct($rule) |
|
31 | { |
||
32 | 66 | $this->rule = $rule; |
|
33 | 66 | } |
|
34 | |||
35 | 12 | public function min(int $minimum) |
|
36 | { |
||
37 | 12 | $this->minimum = $minimum; |
|
38 | |||
39 | 12 | return $this; |
|
40 | } |
||
41 | |||
42 | 6 | public function max(int $maximum) |
|
43 | { |
||
44 | 6 | $this->maximum = $maximum; |
|
45 | |||
46 | 6 | return $this; |
|
47 | } |
||
48 | |||
49 | 6 | public function allowDuplicates(bool $allowed = true) |
|
50 | { |
||
51 | 6 | $this->allowDuplicates = $allowed; |
|
52 | |||
53 | 6 | return $this; |
|
54 | } |
||
55 | |||
56 | 6 | public function separatedBy(string $separator) |
|
57 | { |
||
58 | 6 | $this->separatedBy = $separator; |
|
59 | |||
60 | 6 | return $this; |
|
61 | } |
||
62 | |||
63 | 6 | public function doNotTrimItems() |
|
64 | { |
||
65 | 6 | $this->trimItems = false; |
|
66 | |||
67 | 6 | return true; |
|
68 | } |
||
69 | |||
70 | public function validationMessageWord(string $word) |
||
71 | { |
||
72 | $this->validationMessageWord = $word; |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | 66 | public function passes($attribute, $value) |
|
78 | { |
||
79 | 66 | if ($this->trimItems) { |
|
80 | 60 | $value = trim($value); |
|
81 | } |
||
82 | |||
83 | 66 | $items = collect(explode($this->separatedBy, $value)) |
|
84 | ->filter(function ($item) { |
||
85 | 66 | return strlen((string) $item) > 0; |
|
86 | 66 | }); |
|
87 | |||
88 | 66 | if (! is_null($this->minimum)) { |
|
89 | 12 | if ($items->count() < $this->minimum) { |
|
90 | 6 | $this->message = __('validationRules::messages.delimited.min', [ |
|
91 | 6 | 'minimum' => $this->minimum, |
|
92 | 6 | 'actual' => $items->count(), |
|
93 | 6 | 'item' => Str::plural($this->validationMessageWord, $items->count()), |
|
94 | ]); |
||
95 | |||
96 | 6 | return false; |
|
97 | } |
||
98 | } |
||
99 | |||
100 | 66 | if (! is_null($this->maximum)) { |
|
101 | 6 | if ($items->count() > $this->maximum) { |
|
102 | 6 | $this->message = __('validationRules::messages.delimited.max', [ |
|
103 | 6 | 'maximum' => $this->maximum, |
|
104 | 6 | 'actual' => $items->count(), |
|
105 | 6 | 'item' => Str::plural($this->validationMessageWord, $items->count()), |
|
106 | ]); |
||
107 | |||
108 | 6 | return false; |
|
109 | } |
||
110 | } |
||
111 | |||
112 | 66 | if ($this->trimItems) { |
|
113 | $items = $items->map(function (string $item) { |
||
114 | 60 | return trim($item); |
|
115 | 60 | }); |
|
116 | } |
||
117 | |||
118 | 66 | foreach ($items as $item) { |
|
119 | 66 | [$isValid, $validationMessage] = $this->validate($attribute, $item); |
|
0 ignored issues
–
show
The variable
$validationMessage does not exist. Did you forget to declare it?
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.
Loading history...
|
|||
120 | |||
121 | 66 | if (! $isValid) { |
|
122 | 36 | $this->message = $validationMessage; |
|
123 | |||
124 | 36 | return false; |
|
125 | } |
||
126 | } |
||
127 | |||
128 | 66 | if (! $this->allowDuplicates) { |
|
129 | 60 | if ($items->unique()->count() !== $items->count()) { |
|
130 | 6 | $this->message = __('validationRules::messages.delimited.unique'); |
|
131 | |||
132 | 6 | return false; |
|
133 | } |
||
134 | } |
||
135 | |||
136 | 60 | return true; |
|
137 | } |
||
138 | |||
139 | public function message() |
||
140 | { |
||
141 | return $this->message; |
||
142 | } |
||
143 | |||
144 | 66 | protected function validate(string $attribute, string $item): array |
|
145 | { |
||
146 | 66 | $attribute = Str::after($attribute, '.'); |
|
147 | |||
148 | 66 | $validator = Validator::make([$attribute => $item], [$attribute => $this->rule]); |
|
149 | |||
150 | return [ |
||
151 | 66 | $validator->passes(), |
|
152 | 66 | $validator->getMessageBag()->first($attribute), |
|
153 | ]; |
||
154 | } |
||
155 | } |
||
156 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.