This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
BaseInputFilter.php
633 lines (558 loc) · 16.8 KB
/
BaseInputFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\InputFilter;
use ArrayAccess;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\InitializableInterface;
class BaseInputFilter implements
InputFilterInterface,
UnknownInputsCapableInterface,
InitializableInterface,
ReplaceableInputInterface,
UnfilteredDataInterface
{
/**
* @var null|array
*/
protected $data;
/**
* @var array|object
*/
protected $unfilteredData = [];
/**
* @var InputInterface[]|InputFilterInterface[]
*/
protected $inputs = [];
/**
* @var InputInterface[]|InputFilterInterface[]
*/
protected $invalidInputs;
/**
* @var null|string[] Input names
*/
protected $validationGroup;
/**
* @var InputInterface[]|InputFilterInterface[]
*/
protected $validInputs;
/**
* This function is automatically called when creating element with factory. It
* allows to perform various operations (add elements...)
*
* @return void
*/
public function init()
{
}
/**
* Countable: number of inputs in this input filter
*
* Only details the number of direct children.
*
* @return int
*/
public function count()
{
return count($this->inputs);
}
/**
* Add an input to the input filter
*
* @param InputInterface|InputFilterInterface $input
* @param null|string $name Name used to retrieve this input
* @throws Exception\InvalidArgumentException
* @return InputFilterInterface
*/
public function add($input, $name = null)
{
if (! $input instanceof InputInterface && ! $input instanceof InputFilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an instance of %s or %s as its first argument; received "%s"',
__METHOD__,
InputInterface::class,
InputFilterInterface::class,
(is_object($input) ? get_class($input) : gettype($input))
));
}
if ($input instanceof InputInterface && (empty($name) || is_int($name))) {
$name = $input->getName();
}
if (isset($this->inputs[$name]) && $this->inputs[$name] instanceof InputInterface) {
// The element already exists, so merge the config. Please note
// that this merges the new input into the original.
$original = $this->inputs[$name];
$original->merge($input);
return $this;
}
$this->inputs[$name] = $input;
return $this;
}
/**
* Replace a named input
*
* @param mixed $input Any of the input types allowed on add() method.
* @param string $name Name of the input to replace
* @throws Exception\InvalidArgumentException If input to replace not exists.
* @return self
*/
public function replace($input, $name)
{
if (! array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no input found matching "%s"',
__METHOD__,
$name
));
}
$this->remove($name);
$this->add($input, $name);
return $this;
}
/**
* Retrieve a named input
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return InputInterface|InputFilterInterface
*/
public function get($name)
{
if (! array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no input found matching "%s"',
__METHOD__,
$name
));
}
return $this->inputs[$name];
}
/**
* Test if an input or input filter by the given name is attached
*
* @param string $name
* @return bool
*/
public function has($name)
{
return array_key_exists($name, $this->inputs);
}
/**
* Remove a named input
*
* @param string $name
* @return InputFilterInterface
*/
public function remove($name)
{
unset($this->inputs[$name]);
return $this;
}
/**
* Set data to use when validating and filtering
*
* @param null|array|Traversable $data null is cast to an empty array.
* @throws Exception\InvalidArgumentException
* @return InputFilterInterface
*/
public function setData($data)
{
// A null value indicates an empty set
if (null === $data) {
$data = [];
}
if ($data instanceof Traversable) {
$data = ArrayUtils::iteratorToArray($data);
}
if (! is_array($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable argument; received %s',
__METHOD__,
(is_object($data) ? get_class($data) : gettype($data))
));
}
$this->setUnfilteredData($data);
$this->data = $data;
$this->populate();
return $this;
}
/**
* Is the data set valid?
*
* @param mixed|null $context
* @throws Exception\RuntimeException
* @return bool
*/
public function isValid($context = null)
{
if (null === $this->data) {
throw new Exception\RuntimeException(sprintf(
'%s: no data present to validate!',
__METHOD__
));
}
$inputs = $this->validationGroup ?: array_keys($this->inputs);
return $this->validateInputs($inputs, $this->data, $context);
}
/**
* Validate a set of inputs against the current data
*
* @param string[] $inputs Array of input names.
* @param array|ArrayAccess $data
* @param mixed|null $context
* @return bool
*/
protected function validateInputs(array $inputs, $data = [], $context = null)
{
$inputContext = $context ?: (array_merge($this->getRawValues(), (array) $data));
$this->validInputs = [];
$this->invalidInputs = [];
$valid = true;
foreach ($inputs as $name) {
$input = $this->inputs[$name];
// Validate an input filter
if ($input instanceof InputFilterInterface) {
if (! $input->isValid($context)) {
$this->invalidInputs[$name] = $input;
$valid = false;
continue;
}
$this->validInputs[$name] = $input;
continue;
}
// If input is not InputInterface then silently continue (BC safe)
if (! $input instanceof InputInterface) {
continue;
}
// If input is optional (not required), and value is not set, then ignore.
if (! array_key_exists($name, $data)
&& ! $input->isRequired()
) {
continue;
}
// Validate an input
if (! $input->isValid($inputContext)) {
// Validation failure
$this->invalidInputs[$name] = $input;
$valid = false;
if ($input->breakOnFailure()) {
return false;
}
continue;
}
$this->validInputs[$name] = $input;
}
return $valid;
}
/**
* Provide a list of one or more elements indicating the complete set to validate
*
* When provided, calls to {@link isValid()} will only validate the provided set.
*
* If the initial value is {@link VALIDATE_ALL}, the current validation group, if
* any, should be cleared.
*
* Implementations should allow passing a single array value, or multiple arguments,
* each specifying a single input.
*
* @param mixed $name
* @throws Exception\InvalidArgumentException
* @return InputFilterInterface
*/
public function setValidationGroup($name)
{
if ($name === self::VALIDATE_ALL) {
$this->validationGroup = null;
foreach ($this->getInputs() as $input) {
if ($input instanceof InputFilterInterface) {
$input->setValidationGroup(self::VALIDATE_ALL);
}
}
return $this;
}
if (is_array($name)) {
$inputs = [];
foreach ($name as $key => $value) {
if (! $this->has($key)) {
$inputs[] = $value;
continue;
}
$inputs[] = $key;
if ($this->inputs[$key] instanceof InputFilterInterface) {
// Recursively populate validation groups for sub input filters
$this->inputs[$key]->setValidationGroup($value);
}
}
} else {
$inputs = func_get_args();
}
if (! empty($inputs)) {
$this->validateValidationGroup($inputs);
$this->validationGroup = $inputs;
}
return $this;
}
/**
* Return a list of inputs that were invalid.
*
* Implementations should return an associative array of name/input pairs
* that failed validation.
*
* @return InputInterface[]
*/
public function getInvalidInput()
{
return is_array($this->invalidInputs) ? $this->invalidInputs : [];
}
/**
* Return a list of inputs that were valid.
*
* Implementations should return an associative array of name/input pairs
* that passed validation.
*
* @return InputInterface[]
*/
public function getValidInput()
{
return is_array($this->validInputs) ? $this->validInputs : [];
}
/**
* Retrieve a value from a named input
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return mixed
*/
public function getValue($name)
{
if (! array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a valid input name; "%s" was not found in the filter',
__METHOD__,
$name
));
}
$input = $this->inputs[$name];
if ($input instanceof InputFilterInterface) {
return $input->getValues();
}
return $input->getValue();
}
/**
* Return a list of filtered values
*
* List should be an associative array, with the values filtered. If
* validation failed, this should raise an exception.
*
* @return array
*/
public function getValues()
{
$inputs = $this->validationGroup ?: array_keys($this->inputs);
$values = [];
foreach ($inputs as $name) {
$input = $this->inputs[$name];
if ($input instanceof InputFilterInterface) {
$values[$name] = $input->getValues();
continue;
}
$values[$name] = $input->getValue();
}
return $values;
}
/**
* Retrieve a raw (unfiltered) value from a named input
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return mixed
*/
public function getRawValue($name)
{
if (! array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a valid input name; "%s" was not found in the filter',
__METHOD__,
$name
));
}
$input = $this->inputs[$name];
if ($input instanceof InputFilterInterface) {
return $input->getRawValues();
}
return $input->getRawValue();
}
/**
* Return a list of unfiltered values
*
* List should be an associative array of named input/value pairs,
* with the values unfiltered.
*
* @return array
*/
public function getRawValues()
{
$values = [];
foreach ($this->inputs as $name => $input) {
if ($input instanceof InputFilterInterface) {
$values[$name] = $input->getRawValues();
continue;
}
$values[$name] = $input->getRawValue();
}
return $values;
}
/**
* Return a list of validation failure messages
*
* Should return an associative array of named input/message list pairs.
* Pairs should only be returned for inputs that failed validation.
*
* @return array
*/
public function getMessages()
{
$messages = [];
foreach ($this->getInvalidInput() as $name => $input) {
$messages[$name] = $input->getMessages();
}
return $messages;
}
/**
* Ensure all names of a validation group exist as input in the filter
*
* @param string[] $inputs Input names
* @return void
* @throws Exception\InvalidArgumentException
*/
protected function validateValidationGroup(array $inputs)
{
foreach ($inputs as $name) {
if (! array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'setValidationGroup() expects a list of valid input names; "%s" was not found',
$name
));
}
}
}
/**
* Populate the values of all attached inputs
*
* @return void
*/
protected function populate()
{
foreach (array_keys($this->inputs) as $name) {
$input = $this->inputs[$name];
if ($input instanceof CollectionInputFilter) {
$input->clearValues();
$input->clearRawValues();
}
if (! array_key_exists($name, $this->data)) {
// No value; clear value in this input
if ($input instanceof InputFilterInterface) {
$input->setData([]);
continue;
}
if ($input instanceof Input) {
$input->resetValue();
continue;
}
$input->setValue(null);
continue;
}
$value = $this->data[$name];
if ($input instanceof InputFilterInterface) {
// Fixes #159
if (! is_array($value) && ! $value instanceof Traversable) {
$value = [];
}
$input->setData($value);
continue;
}
$input->setValue($value);
}
}
/**
* Is the data set has unknown input ?
*
* @throws Exception\RuntimeException
* @return bool
*/
public function hasUnknown()
{
return $this->getUnknown() ? true : false;
}
/**
* Return the unknown input
*
* @throws Exception\RuntimeException
* @return array
*/
public function getUnknown()
{
if (null === $this->data) {
throw new Exception\RuntimeException(sprintf(
'%s: no data present!',
__METHOD__
));
}
$data = array_keys($this->data);
$inputs = array_keys($this->inputs);
$diff = array_diff($data, $inputs);
$unknownInputs = [];
$intersect = array_intersect($diff, $data);
if (! empty($intersect)) {
foreach ($intersect as $key) {
$unknownInputs[$key] = $this->data[$key];
}
}
return $unknownInputs;
}
/**
* Get an array of all inputs
*
* @return InputInterface[]|InputFilterInterface[]
*/
public function getInputs()
{
return $this->inputs;
}
/**
* Merges the inputs from an InputFilter into the current one
*
* @param BaseInputFilter $inputFilter
*
* @return self
*/
public function merge(BaseInputFilter $inputFilter)
{
foreach ($inputFilter->getInputs() as $name => $input) {
$this->add($input, $name);
}
return $this;
}
/**
* @return array|object
*/
public function getUnfilteredData()
{
return $this->unfilteredData;
}
/**
* @param array|object $data
* @return $this
*/
public function setUnfilteredData($data)
{
$this->unfilteredData = $data;
return $this;
}
}