1 | <?php |
||
2 | |||
3 | namespace Riclep\StoryblokForms; |
||
4 | |||
5 | use ArrayAccess; |
||
6 | |||
7 | class Validators implements ArrayAccess |
||
8 | { |
||
9 | /** |
||
10 | * @var array All the validation rules |
||
11 | */ |
||
12 | public $rules; |
||
13 | |||
14 | /** |
||
15 | * @var Input The field the rules are applied to |
||
16 | */ |
||
17 | protected $field; |
||
18 | |||
19 | /** |
||
20 | * @param $validators |
||
21 | * @param $field |
||
22 | */ |
||
23 | public function __construct($validators, $field) |
||
24 | { |
||
25 | $this->field = $field; |
||
26 | |||
27 | $this->process($validators); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get all the rules that have been added and push them into an array |
||
32 | * for Laravel’s validation |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | public function validationRules() { |
||
37 | $rules = []; |
||
38 | |||
39 | $hasRules = array_values($this->rules->map(function ($rule) { |
||
40 | return $rule->rule(); |
||
41 | })->toArray()); |
||
42 | |||
43 | if ($hasRules) { |
||
0 ignored issues
–
show
|
|||
44 | $rules[$this->nameToValidationKey()] = $hasRules; |
||
45 | } |
||
46 | |||
47 | return $rules; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Get all the error messages for the rules and push them into an array |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | public function errorMessages() { |
||
56 | $messages = []; |
||
57 | |||
58 | $this->rules->each(function ($rule) use (&$messages) { |
||
59 | if (is_object($rule->rule())) { |
||
60 | $messageKey = $this->nameToValidationKey(); |
||
61 | |||
62 | $messages = array_merge($messages, [$messageKey => $rule->errorMessage()]); |
||
63 | } else { |
||
64 | $messageKey = $this->nameToValidationKey() . '.' . $rule->rule(); |
||
65 | |||
66 | $messages = array_merge($messages, [$messageKey => $rule->errorMessage()]); |
||
67 | } |
||
68 | })->toArray(); |
||
69 | |||
70 | return $messages; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Formats the rule’s name/key correctly for Laravel’s validator |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | protected function nameToValidationKey() |
||
79 | { |
||
80 | $validationKey = str_replace([ |
||
81 | '[]', |
||
82 | '[', |
||
83 | ']' |
||
84 | ], [ |
||
85 | '.*', |
||
86 | '.', |
||
87 | '' |
||
88 | ], $this->field->input_name); |
||
89 | |||
90 | return $validationKey; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Tidy up the Validator JSON from Storyblok as it contains |
||
95 | * more than we require |
||
96 | * |
||
97 | * @param $validators |
||
98 | * @return void |
||
99 | */ |
||
100 | protected function process($validators) { |
||
101 | $this->rules = collect($validators)->map(function ($validator) { |
||
102 | return (new Validator( |
||
103 | array_diff_key($validator, array_flip(['_editable', '_uid'])) |
||
104 | , $this->field)); |
||
105 | }); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param $offset |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function offsetExists($offset) |
||
113 | { |
||
114 | return isset($this->rules[$offset]); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param $offset |
||
119 | * @return mixed|null |
||
120 | */ |
||
121 | public function offsetGet($offset) |
||
122 | { |
||
123 | return $this->rules[$offset] ?? null; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param $offset |
||
128 | * @param $value |
||
129 | * @return void |
||
130 | */ |
||
131 | public function offsetSet($offset, $value) |
||
132 | { |
||
133 | if (is_null($offset)) { |
||
134 | $this->rules[] = $value; |
||
135 | } else { |
||
136 | $this->rules[$offset] = $value; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param $offset |
||
142 | * @return void |
||
143 | */ |
||
144 | public function offsetUnset($offset) |
||
145 | { |
||
146 | unset($this->rules[$offset]); |
||
147 | } |
||
148 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.