3
3
namespace SSD \LaravelValidation ;
4
4
5
5
use Closure ;
6
- use Illuminate \Support \Str ;
7
6
use Illuminate \Contracts \Container \Container ;
8
7
use Illuminate \Contracts \Translation \Translator ;
9
- use Illuminate \Validation \PresenceVerifierInterface ;
10
8
use Illuminate \Contracts \Validation \Factory as FactoryContract ;
9
+ use Illuminate \Support \Str ;
10
+ use Illuminate \Validation \PresenceVerifierInterface ;
11
11
12
12
class Factory implements FactoryContract
13
13
{
@@ -28,45 +28,52 @@ class Factory implements FactoryContract
28
28
/**
29
29
* The IoC container instance.
30
30
*
31
- * @var \Illuminate\Contracts\Container\Container
31
+ * @var \Illuminate\Contracts\Container\Container|null
32
32
*/
33
33
protected $ container ;
34
34
35
35
/**
36
36
* All of the custom validator extensions.
37
37
*
38
- * @var array
38
+ * @var array<string, \Closure|string>
39
39
*/
40
40
protected $ extensions = [];
41
41
42
42
/**
43
43
* All of the custom implicit validator extensions.
44
44
*
45
- * @var array
45
+ * @var array<string, \Closure|string>
46
46
*/
47
47
protected $ implicitExtensions = [];
48
48
49
49
/**
50
50
* All of the custom dependent validator extensions.
51
51
*
52
- * @var array
52
+ * @var array<string, \Closure|string>
53
53
*/
54
54
protected $ dependentExtensions = [];
55
55
56
56
/**
57
57
* All of the custom validator message replacers.
58
58
*
59
- * @var array
59
+ * @var array<string, \Closure|string>
60
60
*/
61
61
protected $ replacers = [];
62
62
63
63
/**
64
64
* All of the fallback messages for custom rules.
65
65
*
66
- * @var array
66
+ * @var array<string, string>
67
67
*/
68
68
protected $ fallbackMessages = [];
69
69
70
+ /**
71
+ * Indicates that unvalidated array keys should be excluded, even if the parent array was validated.
72
+ *
73
+ * @var bool
74
+ */
75
+ protected $ excludeUnvalidatedArrayKeys = true ;
76
+
70
77
/**
71
78
* The Validator resolver instance.
72
79
*
@@ -77,11 +84,9 @@ class Factory implements FactoryContract
77
84
/**
78
85
* Create a new Validator factory instance.
79
86
*
80
- * @param \Illuminate\Contracts\Translation\Translator $translator
81
- * @param \Illuminate\Contracts\Container\Container $container
82
87
* @return void
83
88
*/
84
- public function __construct (Translator $ translator , Container $ container = null )
89
+ public function __construct (Translator $ translator , ? Container $ container = null )
85
90
{
86
91
$ this ->container = $ container ;
87
92
$ this ->translator = $ translator ;
@@ -90,32 +95,30 @@ public function __construct(Translator $translator, Container $container = null)
90
95
/**
91
96
* Create a new Validator instance.
92
97
*
93
- * @param array $data
94
- * @param array $rules
95
- * @param array $messages
96
- * @param array $customAttributes
97
- * @return \Illuminate\Validation\Validator
98
+ * @return \Illuminate\Contracts\Validation\Validator
98
99
*/
99
- public function make (array $ data , array $ rules , array $ messages = [], array $ customAttributes = [])
100
+ public function make (array $ data , array $ rules , array $ messages = [], array $ attributes = [])
100
101
{
101
102
$ validator = $ this ->resolve (
102
- $ data , $ rules , $ messages , $ customAttributes
103
+ $ data , $ rules , $ messages , $ attributes
103
104
);
104
105
105
106
// The presence verifier is responsible for checking the unique and exists data
106
107
// for the validator. It is behind an interface so that multiple versions of
107
108
// it may be written besides database. We'll inject it into the validator.
108
- if (!is_null ($ this ->verifier )) {
109
+ if (! is_null ($ this ->verifier )) {
109
110
$ validator ->setPresenceVerifier ($ this ->verifier );
110
111
}
111
112
112
113
// Next we'll set the IoC container instance of the validator, which is used to
113
114
// resolve out class based validator extensions. If it is not set then these
114
115
// types of extensions will not be possible on these validation instances.
115
- if (!is_null ($ this ->container )) {
116
+ if (! is_null ($ this ->container )) {
116
117
$ validator ->setContainer ($ this ->container );
117
118
}
118
119
120
+ $ validator ->excludeUnvalidatedArrayKeys = $ this ->excludeUnvalidatedArrayKeys ;
121
+
119
122
$ this ->addExtensions ($ validator );
120
123
121
124
return $ validator ;
@@ -124,41 +127,32 @@ public function make(array $data, array $rules, array $messages = [], array $cus
124
127
/**
125
128
* Validate the given data against the provided rules.
126
129
*
127
- * @param array $data
128
- * @param array $rules
129
- * @param array $messages
130
- * @param array $customAttributes
131
130
* @return void
132
131
*
133
132
* @throws \Illuminate\Validation\ValidationException
134
133
*/
135
- public function validate (array $ data , array $ rules , array $ messages = [], array $ customAttributes = [])
134
+ public function validate (array $ data , array $ rules , array $ messages = [], array $ attributes = [])
136
135
{
137
- $ this ->make ($ data , $ rules , $ messages , $ customAttributes )->validate ();
136
+ $ this ->make ($ data , $ rules , $ messages , $ attributes )->validate ();
138
137
}
139
138
140
139
/**
141
140
* Resolve a new Validator instance.
142
141
*
143
- * @param array $data
144
- * @param array $rules
145
- * @param array $messages
146
- * @param array $customAttributes
147
- * @return \Illuminate\Validation\Validator
142
+ * @return \Illuminate\Contracts\Validation\Validator
148
143
*/
149
- protected function resolve (array $ data , array $ rules , array $ messages , array $ customAttributes )
144
+ protected function resolve (array $ data , array $ rules , array $ messages , array $ attributes )
150
145
{
151
146
if (is_null ($ this ->resolver )) {
152
- return new Validator ($ this ->translator , $ data , $ rules , $ messages , $ customAttributes );
147
+ return new Validator ($ this ->translator , $ data , $ rules , $ messages , $ attributes );
153
148
}
154
149
155
- return call_user_func ($ this ->resolver , $ this ->translator , $ data , $ rules , $ messages , $ customAttributes );
150
+ return call_user_func ($ this ->resolver , $ this ->translator , $ data , $ rules , $ messages , $ attributes );
156
151
}
157
152
158
153
/**
159
154
* Add the extensions to a validator instance.
160
155
*
161
- * @param \SSD\LaravelValidation\Validator $validator
162
156
* @return void
163
157
*/
164
158
protected function addExtensions (Validator $ validator )
@@ -180,9 +174,9 @@ protected function addExtensions(Validator $validator)
180
174
/**
181
175
* Register a custom validator extension.
182
176
*
183
- * @param string $rule
184
- * @param \Closure|string $extension
185
- * @param string $message
177
+ * @param string $rule
178
+ * @param \Closure|string $extension
179
+ * @param string $message
186
180
* @return void
187
181
*/
188
182
public function extend ($ rule , $ extension , $ message = null )
@@ -197,9 +191,9 @@ public function extend($rule, $extension, $message = null)
197
191
/**
198
192
* Register a custom implicit validator extension.
199
193
*
200
- * @param string $rule
201
- * @param \Closure|string $extension
202
- * @param string $message
194
+ * @param string $rule
195
+ * @param \Closure|string $extension
196
+ * @param string $message
203
197
* @return void
204
198
*/
205
199
public function extendImplicit ($ rule , $ extension , $ message = null )
@@ -214,9 +208,9 @@ public function extendImplicit($rule, $extension, $message = null)
214
208
/**
215
209
* Register a custom dependent validator extension.
216
210
*
217
- * @param string $rule
218
- * @param \Closure|string $extension
219
- * @param string $message
211
+ * @param string $rule
212
+ * @param \Closure|string $extension
213
+ * @param string $message
220
214
* @return void
221
215
*/
222
216
public function extendDependent ($ rule , $ extension , $ message = null )
@@ -231,19 +225,38 @@ public function extendDependent($rule, $extension, $message = null)
231
225
/**
232
226
* Register a custom validator message replacer.
233
227
*
234
- * @param string $rule
235
- * @param \Closure|string $replacer
228
+ * @param string $rule
229
+ * @param \Closure|string $replacer
236
230
* @return void
237
231
*/
238
232
public function replacer ($ rule , $ replacer )
239
233
{
240
234
$ this ->replacers [$ rule ] = $ replacer ;
241
235
}
242
236
237
+ /**
238
+ * Indicate that unvalidated array keys should be included in validated data when the parent array is validated.
239
+ *
240
+ * @return void
241
+ */
242
+ public function includeUnvalidatedArrayKeys ()
243
+ {
244
+ $ this ->excludeUnvalidatedArrayKeys = false ;
245
+ }
246
+
247
+ /**
248
+ * Indicate that unvalidated array keys should be excluded from the validated data, even if the parent array was validated.
249
+ *
250
+ * @return void
251
+ */
252
+ public function excludeUnvalidatedArrayKeys ()
253
+ {
254
+ $ this ->excludeUnvalidatedArrayKeys = true ;
255
+ }
256
+
243
257
/**
244
258
* Set the Validator instance resolver.
245
259
*
246
- * @param \Closure $resolver
247
260
* @return void
248
261
*/
249
262
public function resolver (Closure $ resolver )
@@ -274,11 +287,32 @@ public function getPresenceVerifier()
274
287
/**
275
288
* Set the Presence Verifier implementation.
276
289
*
277
- * @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
278
290
* @return void
279
291
*/
280
292
public function setPresenceVerifier (PresenceVerifierInterface $ presenceVerifier )
281
293
{
282
294
$ this ->verifier = $ presenceVerifier ;
283
295
}
284
- }
296
+
297
+ /**
298
+ * Get the container instance used by the validation factory.
299
+ *
300
+ * @return \Illuminate\Contracts\Container\Container|null
301
+ */
302
+ public function getContainer ()
303
+ {
304
+ return $ this ->container ;
305
+ }
306
+
307
+ /**
308
+ * Set the container instance used by the validation factory.
309
+ *
310
+ * @return $this
311
+ */
312
+ public function setContainer (Container $ container )
313
+ {
314
+ $ this ->container = $ container ;
315
+
316
+ return $ this ;
317
+ }
318
+ }
0 commit comments