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
;
12 use Zend\Filter\FilterChain
;
13 use Zend\Validator\NotEmpty
;
14 use Zend\Validator\ValidatorChain
;
16 class Input
implements InputInterface
, EmptyContextInterface
21 protected $allowEmpty = false;
26 protected $continueIfEmpty = false;
31 protected $breakOnFailure = false;
36 protected $errorMessage;
41 protected $filterChain;
51 protected $notEmptyValidator = false;
56 protected $required = true;
61 protected $validatorChain;
71 protected $fallbackValue;
73 public function __construct($name = null)
79 * @param bool $allowEmpty
82 public function setAllowEmpty($allowEmpty)
84 $this->allowEmpty
= (bool) $allowEmpty;
89 * @param bool $breakOnFailure
92 public function setBreakOnFailure($breakOnFailure)
94 $this->breakOnFailure
= (bool) $breakOnFailure;
99 * @param bool $continueIfEmpty
100 * @return \Zend\InputFilter\Input
102 public function setContinueIfEmpty($continueIfEmpty)
104 $this->continueIfEmpty
= (bool) $continueIfEmpty;
109 * @param string|null $errorMessage
112 public function setErrorMessage($errorMessage)
114 $this->errorMessage
= (null === $errorMessage) ?
null : (string) $errorMessage;
119 * @param FilterChain $filterChain
122 public function setFilterChain(FilterChain
$filterChain)
124 $this->filterChain
= $filterChain;
129 * @param string $name
132 public function setName($name)
134 $this->name
= (string) $name;
139 * @param bool $required
142 public function setRequired($required)
144 $this->required
= (bool) $required;
145 $this->setAllowEmpty(!$required);
150 * @param ValidatorChain $validatorChain
153 public function setValidatorChain(ValidatorChain
$validatorChain)
155 $this->validatorChain
= $validatorChain;
160 * @param mixed $value
163 public function setValue($value)
165 $this->value
= $value;
170 * @param mixed $value
173 public function setFallbackValue($value)
175 $this->fallbackValue
= $value;
182 public function allowEmpty()
184 return $this->allowEmpty
;
190 public function breakOnFailure()
192 return $this->breakOnFailure
;
198 public function continueIfEmpty()
200 return $this->continueIfEmpty
;
204 * @return string|null
206 public function getErrorMessage()
208 return $this->errorMessage
;
212 * @return FilterChain
214 public function getFilterChain()
216 if (!$this->filterChain
) {
217 $this->setFilterChain(new FilterChain());
219 return $this->filterChain
;
225 public function getName()
233 public function getRawValue()
241 public function isRequired()
243 return $this->required
;
247 * @return ValidatorChain
249 public function getValidatorChain()
251 if (!$this->validatorChain
) {
252 $this->setValidatorChain(new ValidatorChain());
254 return $this->validatorChain
;
260 public function getValue()
262 $filter = $this->getFilterChain();
263 return $filter->filter($this->value
);
269 public function getFallbackValue()
271 return $this->fallbackValue
;
275 * @param InputInterface $input
278 public function merge(InputInterface
$input)
280 $this->setAllowEmpty($input->allowEmpty());
281 $this->setBreakOnFailure($input->breakOnFailure());
282 $this->setContinueIfEmpty($input->continueIfEmpty());
283 $this->setErrorMessage($input->getErrorMessage());
284 $this->setName($input->getName());
285 $this->setRequired($input->isRequired());
286 $this->setValue($input->getRawValue());
288 $filterChain = $input->getFilterChain();
289 $this->getFilterChain()->merge($filterChain);
291 $validatorChain = $input->getValidatorChain();
292 $this->getValidatorChain()->merge($validatorChain);
297 * @param mixed $context Extra "context" to provide the validator
300 public function isValid($context = null)
302 // Empty value needs further validation if continueIfEmpty is set
303 // so don't inject NotEmpty validator which would always
304 // mark that as false
305 if (!$this->continueIfEmpty()) {
306 $this->injectNotEmptyValidator();
308 $validator = $this->getValidatorChain();
309 $value = $this->getValue();
310 $result = $validator->isValid($value, $context);
311 if (!$result && $fallbackValue = $this->getFallbackValue()) {
312 $this->setValue($fallbackValue);
322 public function getMessages()
324 if (null !== $this->errorMessage
) {
325 return (array) $this->errorMessage
;
328 if ($this->getFallbackValue()) {
332 $validator = $this->getValidatorChain();
333 return $validator->getMessages();
339 protected function injectNotEmptyValidator()
341 if ((!$this->isRequired() && $this->allowEmpty()) ||
$this->notEmptyValidator
) {
344 $chain = $this->getValidatorChain();
346 // Check if NotEmpty validator is already in chain
347 $validators = $chain->getValidators();
348 foreach ($validators as $validator) {
349 if ($validator['instance'] instanceof NotEmpty
) {
350 $this->notEmptyValidator
= true;
355 $chain->prependByName('NotEmpty', array(), true);
356 $this->notEmptyValidator
= true;