Source of file Input.php
Size: 7,932 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/InputFilter/Input.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 | <?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 Zend\Filter\FilterChain; use Zend\Validator\NotEmpty; use Zend\Validator\ValidatorChain; class Input implements InputInterface, EmptyContextInterface { /** * @var bool */ protected $allowEmpty = false; /** * @var bool */ protected $continueIfEmpty = false; /** * @var bool */ protected $breakOnFailure = false; /** * @var string|null */ protected $errorMessage; /** * @var FilterChain */ protected $filterChain; /** * @var string */ protected $name; /** * @var bool */ protected $notEmptyValidator = false; /** * @var bool */ protected $required = true; /** * @var ValidatorChain */ protected $validatorChain; /** * @var mixed */ protected $value; /** * @var mixed */ protected $fallbackValue; /** * @var bool */ protected $hasFallback = false; public function __construct($name = null) { $this->name = $name; } /** * @param bool $allowEmpty * @return Input */ public function setAllowEmpty($allowEmpty) { $this->allowEmpty = (bool) $allowEmpty; return $this; } /** * @param bool $breakOnFailure * @return Input */ public function setBreakOnFailure($breakOnFailure) { $this->breakOnFailure = (bool) $breakOnFailure; return $this; } /** * @param bool $continueIfEmpty * @return \Zend\InputFilter\Input */ public function setContinueIfEmpty($continueIfEmpty) { $this->continueIfEmpty = (bool) $continueIfEmpty; return $this; } /** * @param string|null $errorMessage * @return Input */ public function setErrorMessage($errorMessage) { $this->errorMessage = (null === $errorMessage) ? null : (string) $errorMessage; return $this; } /** * @param FilterChain $filterChain * @return Input */ public function setFilterChain(FilterChain $filterChain) { $this->filterChain = $filterChain; return $this; } /** * @param string $name * @return Input */ public function setName($name) { $this->name = (string) $name; return $this; } /** * @param bool $required * @return Input */ public function setRequired($required) { $this->required = (bool) $required; $this->setAllowEmpty(!$required); return $this; } /** * @param ValidatorChain $validatorChain * @return Input */ public function setValidatorChain(ValidatorChain $validatorChain) { $this->validatorChain = $validatorChain; return $this; } /** * @param mixed $value * @return Input */ public function setValue($value) { $this->value = $value; return $this; } /** * @param mixed $value * @return Input */ public function setFallbackValue($value) { $this->fallbackValue = $value; $this->hasFallback = true; return $this; } /** * @return bool */ public function allowEmpty() { return $this->allowEmpty; } /** * @return bool */ public function breakOnFailure() { return $this->breakOnFailure; } /** * @return bool */ public function continueIfEmpty() { return $this->continueIfEmpty; } /** * @return string|null */ public function getErrorMessage() { return $this->errorMessage; } /** * @return FilterChain */ public function getFilterChain() { if (!$this->filterChain) { $this->setFilterChain(new FilterChain()); } return $this->filterChain; } /** * @return string */ public function getName() { return $this->name; } /** * @return mixed */ public function getRawValue() { return $this->value; } /** * @return bool */ public function isRequired() { return $this->required; } /** * @return ValidatorChain */ public function getValidatorChain() { if (!$this->validatorChain) { $this->setValidatorChain(new ValidatorChain()); } return $this->validatorChain; } /** * @return mixed */ public function getValue() { $filter = $this->getFilterChain(); return $filter->filter($this->value); } /** * @return mixed */ public function getFallbackValue() { return $this->fallbackValue; } /** * @return bool */ public function hasFallback() { return $this->hasFallback; } public function clearFallbackValue() { $this->hasFallback = false; $this->fallbackValue = null; } /** * @param InputInterface $input * @return Input */ public function merge(InputInterface $input) { $this->setAllowEmpty($input->allowEmpty()); $this->setBreakOnFailure($input->breakOnFailure()); $this->setContinueIfEmpty($input->continueIfEmpty()); $this->setErrorMessage($input->getErrorMessage()); $this->setName($input->getName()); $this->setRequired($input->isRequired()); $this->setValue($input->getRawValue()); $filterChain = $input->getFilterChain(); $this->getFilterChain()->merge($filterChain); $validatorChain = $input->getValidatorChain(); $this->getValidatorChain()->merge($validatorChain); return $this; } /** * @param mixed $context Extra "context" to provide the validator * @return bool */ public function isValid($context = null) { // Empty value needs further validation if continueIfEmpty is set // so don't inject NotEmpty validator which would always // mark that as false if (!$this->continueIfEmpty()) { $this->injectNotEmptyValidator(); } $validator = $this->getValidatorChain(); $value = $this->getValue(); $result = $validator->isValid($value, $context); if (!$result && $this->hasFallback()) { $this->setValue($this->getFallbackValue()); $result = true; } return $result; } /** * @return array */ public function getMessages() { if (null !== $this->errorMessage) { return (array) $this->errorMessage; } if ($this->hasFallback()) { return array(); } $validator = $this->getValidatorChain(); return $validator->getMessages(); } /** * @return void */ protected function injectNotEmptyValidator() { if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) { return; } $chain = $this->getValidatorChain(); // Check if NotEmpty validator is already in chain $validators = $chain->getValidators(); foreach ($validators as $validator) { if ($validator['instance'] instanceof NotEmpty) { $this->notEmptyValidator = true; return; } } $chain->prependByName('NotEmpty', array(), true); $this->notEmptyValidator = true; } } |