1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
class Validators implements ArrayAccess |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var |
12
|
|
|
*/ |
13
|
|
|
public $rules; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
protected $field; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $validators |
22
|
|
|
* @param $field |
23
|
|
|
*/ |
24
|
|
|
public function __construct($validators, $field) |
25
|
|
|
{ |
26
|
|
|
$this->process($validators); |
27
|
|
|
|
28
|
|
|
$this->field = $field; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function validationRules() { |
35
|
|
|
$rules = []; |
36
|
|
|
|
37
|
|
|
$hasRules = array_values($this->rules->map(function ($rule) { |
38
|
|
|
return $rule->rule(); |
39
|
|
|
})->toArray()); |
40
|
|
|
|
41
|
|
|
if ($hasRules) { |
|
|
|
|
42
|
|
|
$rules[$this->nameToValidationKey()] = $hasRules; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $rules; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function errorMessages() { |
52
|
|
|
$messages = []; |
53
|
|
|
|
54
|
|
|
$this->rules->each(function ($rule) use (&$messages) { |
55
|
|
|
if ($rule->errorMessage()) { |
56
|
|
|
$messageKey = $this->nameToValidationKey() . '.' . $rule->rule(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$messages = array_merge($messages, [$messageKey => $rule->errorMessage()]); |
59
|
|
|
} |
60
|
|
|
})->toArray(); |
61
|
|
|
|
62
|
|
|
return $messages; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array|string|string[] |
67
|
|
|
*/ |
68
|
|
|
protected function nameToValidationKey(): string|array |
69
|
|
|
{ |
70
|
|
|
$validationKey = str_replace([ |
71
|
|
|
'[]', |
72
|
|
|
'[', |
73
|
|
|
']' |
74
|
|
|
], [ |
75
|
|
|
'.*', |
76
|
|
|
'.', |
77
|
|
|
'' |
78
|
|
|
], $this->field->name); |
79
|
|
|
|
80
|
|
|
return $validationKey; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $validators |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function process($validators) { |
88
|
|
|
$this->rules = collect($validators)->transform(function ($validator) { |
89
|
|
|
return (new Validator(array_diff_key($validator, array_flip(['_editable', '_uid'])))); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $offset |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function offsetExists($offset) |
98
|
|
|
{ |
99
|
|
|
return isset($this->rules[$offset]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $offset |
104
|
|
|
* @return mixed|null |
105
|
|
|
*/ |
106
|
|
|
public function offsetGet($offset) |
107
|
|
|
{ |
108
|
|
|
return $this->rules[$offset] ?? null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $offset |
113
|
|
|
* @param $value |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function offsetSet($offset, $value) |
117
|
|
|
{ |
118
|
|
|
if (is_null($offset)) { |
119
|
|
|
$this->rules[] = $value; |
120
|
|
|
} else { |
121
|
|
|
$this->rules[$offset] = $value; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $offset |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function offsetUnset($offset) |
130
|
|
|
{ |
131
|
|
|
unset($this->rules[$offset]); |
132
|
|
|
} |
133
|
|
|
} |
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.