|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Riclep\StoryblokForms\Rules\ConditionallyRequired; |
|
8
|
|
|
|
|
9
|
|
|
class Validators implements ArrayAccess |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Collection All the validation rules |
|
13
|
|
|
*/ |
|
14
|
|
|
public Collection $rules; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Input The field the rules are applied to |
|
18
|
|
|
*/ |
|
19
|
|
|
protected Input $field; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param $validators |
|
23
|
|
|
* @param $field |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($validators, $field) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->field = $field; |
|
28
|
|
|
|
|
29
|
|
|
$this->process($validators); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get all the rules that have been added and push them into an array |
|
34
|
|
|
* for Laravel’s validation |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function validationRules(): array |
|
39
|
|
|
{ |
|
40
|
|
|
$rules = []; |
|
41
|
|
|
|
|
42
|
|
|
$hasRules = array_values($this->rules->map(function ($rule) { |
|
43
|
|
|
return $rule->rule(); |
|
44
|
|
|
})->toArray()); |
|
45
|
|
|
|
|
46
|
|
|
// We need to inject this at Validators level not Validator level so it |
|
47
|
|
|
// has access to all the required data as we’re referencing other fields |
|
48
|
|
|
// and properties |
|
49
|
|
|
if (method_exists($this, 'hasSetting') && $this->field->hasSetting('lsf_conditional')) { |
|
|
|
|
|
|
50
|
|
|
$hasRules[] = new ConditionallyRequired($this->field->settings('lsf_conditional')); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// fields inside a conditionally visible fieldset - added via settings (not conditional field set) |
|
54
|
|
|
if ($this->field->parent() && method_exists($this->field->parent(), 'hasSetting') && $this->field->parent()->hasSetting('lsf_conditional')) { |
|
55
|
|
|
$hasRules[] = new ConditionallyRequired($this->field->parent()->settings('lsf_conditional')); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($hasRules) { |
|
59
|
|
|
$rules[$this->nameToValidationKey()] = $hasRules; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $rules; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get all the error messages for the rules and push them into an array |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function errorMessages(): array |
|
71
|
|
|
{ |
|
72
|
|
|
$messages = []; |
|
73
|
|
|
|
|
74
|
|
|
$this->rules->each(function ($rule) use (&$messages) { |
|
75
|
|
|
if (is_object($rule->rule())) { |
|
76
|
|
|
$messageKey = $this->nameToValidationKey(); |
|
77
|
|
|
|
|
78
|
|
|
$messages = array_merge($messages, [$messageKey => $rule->errorMessage()]); |
|
79
|
|
|
} else { |
|
80
|
|
|
$messageKey = $this->nameToValidationKey() . '.' . $rule->ruleForValidation(); |
|
81
|
|
|
|
|
82
|
|
|
$messages = array_merge($messages, [$messageKey => $rule->errorMessage()]); |
|
83
|
|
|
} |
|
84
|
|
|
})->toArray(); |
|
85
|
|
|
|
|
86
|
|
|
return $messages; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Formats the rule’s name/key correctly for Laravel’s validator |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function nameToValidationKey(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return str_replace([ |
|
97
|
|
|
'[]', |
|
98
|
|
|
'[', |
|
99
|
|
|
']' |
|
100
|
|
|
], [ |
|
101
|
|
|
'.*', |
|
102
|
|
|
'.', |
|
103
|
|
|
'' |
|
104
|
|
|
], $this->field->input_name); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Tidy up the Validator block JSON from Storyblok as it contains |
|
109
|
|
|
* more fields than we require |
|
110
|
|
|
* |
|
111
|
|
|
* @param $validators |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function process($validators): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->rules = collect($validators)->map(fn($validator) => ( |
|
117
|
|
|
new Validator( |
|
118
|
|
|
array_diff_key($validator, array_flip(['_editable', '_uid'])), |
|
119
|
|
|
$this->field |
|
120
|
|
|
) |
|
121
|
|
|
)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* ArrayAccess: Check if an offset exists |
|
126
|
|
|
* |
|
127
|
|
|
* @param $offset |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function offsetExists($offset): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return isset($this->rules[$offset]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* ArrayAccess: Get a rule by its offset |
|
137
|
|
|
* |
|
138
|
|
|
* @param $offset |
|
139
|
|
|
* @return mixed|null |
|
140
|
|
|
*/ |
|
141
|
|
|
public function offsetGet($offset): mixed |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->rules[$offset] ?? null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* ArrayAcess: Set a value |
|
148
|
|
|
* |
|
149
|
|
|
* @param $offset |
|
150
|
|
|
* @param $value |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
public function offsetSet($offset, $value): void |
|
154
|
|
|
{ |
|
155
|
|
|
if (is_null($offset)) { |
|
156
|
|
|
$this->rules[] = $value; |
|
157
|
|
|
} else { |
|
158
|
|
|
$this->rules[$offset] = $value; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* ArrayAccess: Unset an offset |
|
164
|
|
|
* |
|
165
|
|
|
* @param $offset |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
public function offsetUnset($offset): void |
|
169
|
|
|
{ |
|
170
|
|
|
unset($this->rules[$offset]); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
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.