Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / InputFilter / CollectionInputFilter.php
blobf947fef69c30968ce541dbacc18185f9960bff96
1 <?php
2 /**
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
8 */
10 namespace Zend\InputFilter;
12 use Traversable;
14 class CollectionInputFilter extends InputFilter
17 * @var array
19 protected $collectionData;
22 * @var array
24 protected $collectionValidInputs;
27 * @var array
29 protected $collectionInvalidInputs;
32 * @var bool
34 protected $isRequired = false;
37 * @var int
39 protected $count = null;
42 * @var array
44 protected $collectionValues = array();
47 * @var array
49 protected $collectionRawValues = array();
51 /**
52 * @var BaseInputFilter
54 protected $inputFilter;
56 /**
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"',
71 __METHOD__,
72 'Zend\InputFilter\BaseInputFilter',
73 (is_object($inputFilter) ? get_class($inputFilter) : gettype($inputFilter))
74 ));
77 $this->inputFilter = $inputFilter;
78 $this->inputs = $inputFilter->getInputs();
79 return $this;
82 /**
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;
95 /**
96 * Set if the collection can be empty
98 * @param bool $isRequired
99 * @return CollectionInputFilter
101 public function setIsRequired($isRequired)
103 $this->isRequired = $isRequired;
104 return $this;
108 * Get if collection can be empty
110 * @return bool
112 public function getIsRequired()
114 return $this->isRequired;
119 * Set the count of data to validate
121 * @param int $count
122 * @return CollectionInputFilter
124 public function setCount($count)
126 $this->count = $count > 0 ? $count : 0;
127 return $this;
131 * Get the count of data to validate, use the count of data by default
133 * @return int
135 public function getCount()
137 if (null === $this->count) {
138 $this->count = count($this->collectionData);
140 return $this->count;
144 * {@inheritdoc}
146 public function setData($data)
148 $this->collectionData = $data;
152 * {@inheritdoc}
154 public function isValid()
156 $valid = true;
158 if ($this->getCount() < 1) {
159 if ($this->isRequired) {
160 $valid = false;
162 return $valid;
165 if (count($this->collectionData) < $this->getCount()) {
166 $valid = false;
169 $inputs = $this->validationGroup ?: array_keys($this->inputs);
170 foreach ($this->collectionData as $key => $data) {
171 if (!is_array($data)) {
172 $data = array();
174 $this->data = $data;
175 $this->populate();
177 if ($this->validateInputs($inputs)) {
178 $this->collectionValidInputs[$key] = $this->validInputs;
179 } else {
180 $this->collectionInvalidInputs[$key] = $this->invalidInputs;
181 $valid = false;
184 $values = array();
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();
192 continue;
194 $values[$name] = $input->getValue($this->data);
195 $rawValues[$name] = $input->getRawValue();
197 $this->collectionValues[$key] = $values;
198 $this->collectionRawValues[$key] = $rawValues;
201 return $valid;
205 * {@inheritdoc}
207 public function setValidationGroup($name)
209 if ($name === self::VALIDATE_ALL) {
210 $this->validationGroup = null;
211 return $this;
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());
226 * {@inheritdoc}
228 public function getInvalidInput()
230 return (is_array($this->collectionInvalidInputs) ? $this->collectionInvalidInputs : array());
234 * {@inheritdoc}
236 public function getValidInput()
238 return (is_array($this->collectionValidInputs) ? $this->collectionValidInputs : array());
242 * {@inheritdoc}
244 public function getValues()
246 return $this->collectionValues;
250 * {@inheritdoc}
252 public function getRawValues()
254 return $this->collectionRawValues;
258 * {@inheritdoc}
260 public function getMessages()
262 $messages = array();
263 foreach ($this->getInvalidInput() as $key => $inputs) {
264 foreach ($inputs as $name => $input) {
265 $messages[$key][$name] = $input->getMessages();
268 return $messages;