3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\InputFilter
;
14 class CollectionInputFilter
extends InputFilter
19 protected $collectionData;
24 protected $collectionValidInputs;
29 protected $collectionInvalidInputs;
34 protected $isRequired = false;
39 protected $count = null;
44 protected $collectionValues = array();
49 protected $collectionRawValues = array();
52 * @var BaseInputFilter
54 protected $inputFilter;
57 * Set the input filter to use when looping the data
59 * @param BaseInputFilter|array|Traversable $inputFilter
60 * @return CollectionInputFilter
62 public function setInputFilter($inputFilter)
64 if (is_array($inputFilter) ||
$inputFilter instanceof Traversable
) {
65 $inputFilter = $this->getFactory()->createInputFilter($inputFilter);
68 if (!$inputFilter instanceof BaseInputFilter
) {
69 throw new Exception\
RuntimeException(sprintf(
70 '%s expects an instance of %s; received "%s"',
72 'Zend\InputFilter\BaseInputFilter',
73 (is_object($inputFilter) ?
get_class($inputFilter) : gettype($inputFilter))
77 $this->inputFilter
= $inputFilter;
78 $this->inputs
= $inputFilter->getInputs();
83 * Get the input filter used when looping the data
85 * @return BaseInputFilter
87 public function getInputFilter()
89 if (null === $this->inputFilter
) {
90 $this->setInputFilter(new InputFilter());
92 return $this->inputFilter
;
96 * Set if the collection can be empty
98 * @param bool $isRequired
99 * @return CollectionInputFilter
101 public function setIsRequired($isRequired)
103 $this->isRequired
= $isRequired;
108 * Get if collection can be empty
112 public function getIsRequired()
114 return $this->isRequired
;
119 * Set the count of data to validate
122 * @return CollectionInputFilter
124 public function setCount($count)
126 $this->count
= $count > 0 ?
$count : 0;
131 * Get the count of data to validate, use the count of data by default
135 public function getCount()
137 if (null === $this->count
) {
138 $this->count
= count($this->collectionData
);
146 public function setData($data)
148 $this->collectionData
= $data;
154 public function isValid()
158 if ($this->getCount() < 1) {
159 if ($this->isRequired
) {
165 if (count($this->collectionData
) < $this->getCount()) {
169 $inputs = $this->validationGroup ?
: array_keys($this->inputs
);
170 foreach ($this->collectionData
as $key => $data) {
171 if (!is_array($data)) {
177 if ($this->validateInputs($inputs)) {
178 $this->collectionValidInputs
[$key] = $this->validInputs
;
180 $this->collectionInvalidInputs
[$key] = $this->invalidInputs
;
185 $rawValues = array();
186 foreach ($inputs as $name) {
187 $input = $this->inputs
[$name];
189 if ($input instanceof InputFilterInterface
) {
190 $values[$name] = $input->getValues();
191 $rawValues[$name] = $input->getRawValues();
194 $values[$name] = $input->getValue($this->data
);
195 $rawValues[$name] = $input->getRawValue();
197 $this->collectionValues
[$key] = $values;
198 $this->collectionRawValues
[$key] = $rawValues;
207 public function setValidationGroup($name)
209 if ($name === self
::VALIDATE_ALL
) {
210 $this->validationGroup
= null;
214 if (is_array($name)) {
215 // Best effort check if the validation group was set by a form for BC
216 if (count($name) == count($this->collectionData
) && is_array(reset($name))) {
217 return parent
::setValidationGroup(reset($name));
219 return parent
::setValidationGroup($name);
222 return parent
::setValidationGroup(func_get_args());
228 public function getInvalidInput()
230 return (is_array($this->collectionInvalidInputs
) ?
$this->collectionInvalidInputs
: array());
236 public function getValidInput()
238 return (is_array($this->collectionValidInputs
) ?
$this->collectionValidInputs
: array());
244 public function getValues()
246 return $this->collectionValues
;
252 public function getRawValues()
254 return $this->collectionRawValues
;
260 public function getMessages()
263 foreach ($this->getInvalidInput() as $key => $inputs) {
264 foreach ($inputs as $name => $input) {
265 $messages[$key][$name] = $input->getMessages();