Source of file CollectionInputFilter.php
Size: 7,115 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/InputFilter/CollectionInputFilter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 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 bool */ protected $isRequired = false; /* * @var int */ protected $count = null; /* * @var array */ protected $collectionValues = array(); /* * @var array */ protected $collectionRawValues = array(); /* * @var array */ protected $collectionMessages = 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 if the collection can be empty * * @param bool $isRequired * @return CollectionInputFilter */ public function setIsRequired($isRequired) { $this->isRequired = $isRequired; return $this; } /** * Get if collection can be empty * * @return bool */ public function getIsRequired() { return $this->isRequired; } /** * 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 = $data; } /** * {@inheritdoc} */ public function isValid() { $valid = true; if ($this->getCount() < 1) { if ($this->isRequired) { $valid = false; } } if (count($this->collectionData) < $this->getCount()) { $valid = false; } if (empty($this->collectionData)) { $this->clearValues(); $this->clearRawValues(); return $valid; } $inputs = $this->validationGroup ?: array_keys($this->inputs); foreach ($this->collectionData as $key => $data) { if (!is_array($data)) { $data = array(); } $this->data = $data; $this->populate(); if ($this->validateInputs($inputs, $data)) { $this->collectionValidInputs[$key] = $this->validInputs; } else { $this->collectionInvalidInputs[$key] = $this->invalidInputs; $valid = false; } $values = array(); $rawValues = array(); $messages = 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(); $tmpMessages = $input->getMessages(); if (!empty($tmpMessages)) { $messages[$name] = $tmpMessages; } } $this->collectionValues[$key] = $values; $this->collectionRawValues[$key] = $rawValues; if (!empty($messages)) { $this->collectionMessages[$key] = $messages; } } 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; } /** * Clear collectionValues * * @access public */ public function clearValues() { return $this->collectionValues = array(); } /** * Clear collectionRawValues * * @access public */ public function clearRawValues() { return $this->collectionRawValues = array(); } /** * {@inheritdoc} */ public function getMessages() { return $this->collectionMessages; } } |