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 / Input.php
blobda84df9e1744af2a00849f4bee3cc89aab201805
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 Zend\Filter\FilterChain;
13 use Zend\Validator\NotEmpty;
14 use Zend\Validator\ValidatorChain;
16 class Input implements InputInterface, EmptyContextInterface
18 /**
19 * @var bool
21 protected $allowEmpty = false;
23 /**
24 * @var bool
26 protected $continueIfEmpty = false;
28 /**
29 * @var bool
31 protected $breakOnFailure = false;
33 /**
34 * @var string|null
36 protected $errorMessage;
38 /**
39 * @var FilterChain
41 protected $filterChain;
43 /**
44 * @var string
46 protected $name;
48 /**
49 * @var bool
51 protected $notEmptyValidator = false;
53 /**
54 * @var bool
56 protected $required = true;
58 /**
59 * @var ValidatorChain
61 protected $validatorChain;
63 /**
64 * @var mixed
66 protected $value;
68 /**
69 * @var mixed
71 protected $fallbackValue;
73 public function __construct($name = null)
75 $this->name = $name;
78 /**
79 * @param bool $allowEmpty
80 * @return Input
82 public function setAllowEmpty($allowEmpty)
84 $this->allowEmpty = (bool) $allowEmpty;
85 return $this;
88 /**
89 * @param bool $breakOnFailure
90 * @return Input
92 public function setBreakOnFailure($breakOnFailure)
94 $this->breakOnFailure = (bool) $breakOnFailure;
95 return $this;
98 /**
99 * @param bool $continueIfEmpty
100 * @return \Zend\InputFilter\Input
102 public function setContinueIfEmpty($continueIfEmpty)
104 $this->continueIfEmpty = (bool) $continueIfEmpty;
105 return $this;
109 * @param string|null $errorMessage
110 * @return Input
112 public function setErrorMessage($errorMessage)
114 $this->errorMessage = (null === $errorMessage) ? null : (string) $errorMessage;
115 return $this;
119 * @param FilterChain $filterChain
120 * @return Input
122 public function setFilterChain(FilterChain $filterChain)
124 $this->filterChain = $filterChain;
125 return $this;
129 * @param string $name
130 * @return Input
132 public function setName($name)
134 $this->name = (string) $name;
135 return $this;
139 * @param bool $required
140 * @return Input
142 public function setRequired($required)
144 $this->required = (bool) $required;
145 $this->setAllowEmpty(!$required);
146 return $this;
150 * @param ValidatorChain $validatorChain
151 * @return Input
153 public function setValidatorChain(ValidatorChain $validatorChain)
155 $this->validatorChain = $validatorChain;
156 return $this;
160 * @param mixed $value
161 * @return Input
163 public function setValue($value)
165 $this->value = $value;
166 return $this;
170 * @param mixed $value
171 * @return Input
173 public function setFallbackValue($value)
175 $this->fallbackValue = $value;
176 return $this;
180 * @return bool
182 public function allowEmpty()
184 return $this->allowEmpty;
188 * @return bool
190 public function breakOnFailure()
192 return $this->breakOnFailure;
196 * @return bool
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;
223 * @return string
225 public function getName()
227 return $this->name;
231 * @return mixed
233 public function getRawValue()
235 return $this->value;
239 * @return bool
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;
258 * @return mixed
260 public function getValue()
262 $filter = $this->getFilterChain();
263 return $filter->filter($this->value);
267 * @return mixed
269 public function getFallbackValue()
271 return $this->fallbackValue;
275 * @param InputInterface $input
276 * @return 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);
293 return $this;
297 * @param mixed $context Extra "context" to provide the validator
298 * @return bool
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);
313 $result = true;
316 return $result;
320 * @return array
322 public function getMessages()
324 if (null !== $this->errorMessage) {
325 return (array) $this->errorMessage;
328 if ($this->getFallbackValue()) {
329 return array();
332 $validator = $this->getValidatorChain();
333 return $validator->getMessages();
337 * @return void
339 protected function injectNotEmptyValidator()
341 if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) {
342 return;
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;
351 return;
355 $chain->prependByName('NotEmpty', array(), true);
356 $this->notEmptyValidator = true;