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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zendframework/zendframework#3879 from davidwindell…
…/feature-collection-input-filter Feature CollectionInputFilter
- Loading branch information
109 parents
3ebc62e
+
bfc0ac0
+
0337874
+
ac4e5a4
+
74ad4e1
+
8037ddd
+
c91b8cb
+
442a604
+
ce86130
+
6a17c00
+
3be35a1
+
b101693
+
38a8855
+
2503264
+
1330305
+
c63994b
+
0cebe75
+
88437dd
+
335a7e0
+
49f818c
+
94c6248
+
babc2b3
+
c5537ad
+
a278174
+
ee91e56
+
f730475
+
e954d31
+
aa83987
+
4272527
+
760e014
+
8f9644f
+
89ddc85
+
ef5a070
+
af19258
+
363ebe6
+
774ea64
+
db99c7e
+
fb3727c
+
d8ea42f
+
a5c0289
+
9730940
+
ebc1a56
+
1d8cf16
+
09b3859
+
efe96d1
+
acf177c
+
7a7f261
+
df4c86b
+
4d14dd3
+
d0b7057
+
c858228
+
8855a3f
+
956acc6
+
085a083
+
99b3987
+
59e9d17
+
edd16d1
+
17ad876
+
401ed50
+
f43d222
+
73ebfdd
+
da15c07
+
4652c73
+
6a7b288
+
93b11b3
+
d67242e
+
be0deed
+
c16475f
+
68c3d64
+
537f455
+
e79de85
+
0cbbdec
+
3e0855f
+
a7a27db
+
a851ed1
+
86bf6e9
+
9fcf3f0
+
c6deb01
+
811568b
+
7222979
+
61e594a
+
3e10b67
+
ecabb9a
+
8cdd2bc
+
779ae47
+
5ef112f
+
7ea9722
+
e951b83
+
37c7404
+
e730f97
+
43752c5
+
ba32611
+
7c15be9
+
e3a9518
+
cc9ba24
+
5fbd138
+
8bab1da
+
fa276b4
+
3615cdf
+
663ba3c
+
f0e4074
+
c39cf5f
+
bfa79e5
+
3951384
+
428584d
+
9dccbe3
+
c5082d3
+
01834cc
+
ff6c032
commit 828b3d1
Showing
6 changed files
with
656 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\InputFilter; | ||
|
||
use Traversable; | ||
|
||
class CollectionInputFilter extends InputFilter | ||
{ | ||
/* | ||
* @var array | ||
*/ | ||
protected $collectionData; | ||
|
||
/* | ||
* @var array | ||
*/ | ||
protected $collectionValidInputs; | ||
|
||
/* | ||
* @var array | ||
*/ | ||
protected $collectionInvalidInputs; | ||
|
||
/* | ||
* @var int | ||
*/ | ||
protected $count = null; | ||
|
||
/* | ||
* @var array | ||
*/ | ||
protected $collectionValues = array(); | ||
|
||
/* | ||
* @var array | ||
*/ | ||
protected $collectionRawValues = array(); | ||
|
||
/** | ||
* @var BaseInputFilter | ||
*/ | ||
protected $inputFilter; | ||
|
||
/** | ||
* Set the input filter to use when looping the data | ||
* | ||
* @param BaseInputFilter|array|Traversable $inputFilter | ||
* @return CollectionInputFilter | ||
*/ | ||
public function setInputFilter($inputFilter) | ||
{ | ||
if (is_array($inputFilter) || $inputFilter instanceof Traversable) { | ||
$inputFilter = $this->getFactory()->createInputFilter($inputFilter); | ||
} | ||
|
||
if (!$inputFilter instanceof BaseInputFilter) { | ||
throw new Exception\RuntimeException(sprintf( | ||
'%s expects an instance of %s; received "%s"', | ||
__METHOD__, | ||
'Zend\InputFilter\BaseInputFilter', | ||
(is_object($inputFilter) ? get_class($inputFilter) : gettype($inputFilter)) | ||
)); | ||
} | ||
|
||
$this->inputFilter = $inputFilter; | ||
$this->inputs = $inputFilter->getInputs(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get the input filter used when looping the data | ||
* | ||
* @return BaseInputFilter | ||
*/ | ||
public function getInputFilter() | ||
{ | ||
if (null === $this->inputFilter) { | ||
$this->setInputFilter(new InputFilter()); | ||
} | ||
return $this->inputFilter; | ||
} | ||
|
||
/** | ||
* Set the count of data to validate | ||
* | ||
* @param int $count | ||
* @return CollectionInputFilter | ||
*/ | ||
public function setCount($count) | ||
{ | ||
$this->count = $count > 0 ? $count : 0; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get the count of data to validate, use the count of data by default | ||
* | ||
* @return int | ||
*/ | ||
public function getCount() | ||
{ | ||
if (null === $this->count) { | ||
$this->count = count($this->collectionData); | ||
} | ||
return $this->count; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setData($data) | ||
{ | ||
$this->collectionData = array_values($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isValid() | ||
{ | ||
$valid = true; | ||
|
||
if ($this->getCount() < 1) { | ||
return $valid; | ||
} | ||
|
||
$inputCollection = array_fill(0, $this->getCount() , $this->validationGroup ?: array_keys($this->inputs)); | ||
|
||
foreach ($inputCollection as $key => $inputs) { | ||
$this->data = array(); | ||
if (isset($this->collectionData[$key])) { | ||
$this->data = $this->collectionData[$key]; | ||
} | ||
$this->populate(); | ||
|
||
if ($this->validateInputs($inputs)) { | ||
$this->collectionValidInputs[$key] = $this->validInputs; | ||
} else { | ||
$this->collectionInvalidInputs[$key] = $this->invalidInputs; | ||
$valid = false; | ||
} | ||
|
||
$values = array(); | ||
$rawValues = array(); | ||
foreach ($inputs as $name) { | ||
$input = $this->inputs[$name]; | ||
|
||
if ($input instanceof InputFilterInterface) { | ||
$values[$name] = $input->getValues(); | ||
$rawValues[$name] = $input->getRawValues(); | ||
continue; | ||
} | ||
$values[$name] = $input->getValue($this->data); | ||
$rawValues[$name] = $input->getRawValue(); | ||
} | ||
$this->collectionValues[$key] = $values; | ||
$this->collectionRawValues[$key] = $rawValues; | ||
} | ||
|
||
return $valid; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setValidationGroup($name) | ||
{ | ||
if ($name === self::VALIDATE_ALL) { | ||
$this->validationGroup = null; | ||
return $this; | ||
} | ||
|
||
if (is_array($name)) { | ||
// Best effort check if the validation group was set by a form for BC | ||
if (count($name) == count($this->collectionData) && is_array(reset($name))) { | ||
return parent::setValidationGroup(reset($name)); | ||
} | ||
return parent::setValidationGroup($name); | ||
} | ||
|
||
return parent::setValidationGroup(func_get_args()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getInvalidInput() | ||
{ | ||
return (is_array($this->collectionInvalidInputs) ? $this->collectionInvalidInputs : array()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValidInput() | ||
{ | ||
return (is_array($this->collectionValidInputs) ? $this->collectionValidInputs : array()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValues() | ||
{ | ||
return $this->collectionValues; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRawValues() | ||
{ | ||
return $this->collectionRawValues; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMessages() | ||
{ | ||
$messages = array(); | ||
foreach ($this->getInvalidInput() as $key => $inputs) { | ||
foreach ($inputs as $name => $input) { | ||
$messages[$key][$name] = $input->getMessages(); | ||
} | ||
} | ||
return $messages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.