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 / Validator / ValidatorChain.php
blobd3aa1980644c3afad038784eb3cb16882d893ae7
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\Validator;
12 use Countable;
14 class ValidatorChain implements
15 Countable,
16 ValidatorInterface
18 /**
19 * @var ValidatorPluginManager
21 protected $plugins;
23 /**
24 * Validator chain
26 * @var array
28 protected $validators = array();
30 /**
31 * Array of validation failure messages
33 * @var array
35 protected $messages = array();
37 /**
38 * Return the count of attached validators
40 * @return int
42 public function count()
44 return count($this->validators);
47 /**
48 * Get plugin manager instance
50 * @return ValidatorPluginManager
52 public function getPluginManager()
54 if (!$this->plugins) {
55 $this->setPluginManager(new ValidatorPluginManager());
57 return $this->plugins;
60 /**
61 * Set plugin manager instance
63 * @param ValidatorPluginManager $plugins Plugin manager
64 * @return ValidatorChain
66 public function setPluginManager(ValidatorPluginManager $plugins)
68 $this->plugins = $plugins;
69 return $this;
72 /**
73 * Retrieve a validator by name
75 * @param string $name Name of validator to return
76 * @param null|array $options Options to pass to validator constructor (if not already instantiated)
77 * @return ValidatorInterface
79 public function plugin($name, array $options = null)
81 $plugins = $this->getPluginManager();
82 return $plugins->get($name, $options);
85 /**
86 * Attach a validator to the end of the chain
88 * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
89 * if one exists, will not be executed.
91 * @param ValidatorInterface $validator
92 * @param bool $breakChainOnFailure
93 * @return ValidatorChain Provides a fluent interface
95 public function attach(ValidatorInterface $validator, $breakChainOnFailure = false)
97 $this->validators[] = array(
98 'instance' => $validator,
99 'breakChainOnFailure' => (bool) $breakChainOnFailure,
101 return $this;
105 * Proxy to attach() to keep BC
107 * @deprecated Please use attach()
108 * @param ValidatorInterface $validator
109 * @param bool $breakChainOnFailure
110 * @return ValidatorChain Provides a fluent interface
112 public function addValidator(ValidatorInterface $validator, $breakChainOnFailure = false)
114 return $this->attach($validator, $breakChainOnFailure);
118 * Adds a validator to the beginning of the chain
120 * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
121 * if one exists, will not be executed.
123 * @param ValidatorInterface $validator
124 * @param bool $breakChainOnFailure
125 * @return ValidatorChain Provides a fluent interface
127 public function prependValidator(ValidatorInterface $validator, $breakChainOnFailure = false)
129 array_unshift(
130 $this->validators,
131 array(
132 'instance' => $validator,
133 'breakChainOnFailure' => (bool) $breakChainOnFailure,
136 return $this;
140 * Use the plugin manager to add a validator by name
142 * @param string $name
143 * @param array $options
144 * @param bool $breakChainOnFailure
145 * @return ValidatorChain
147 public function attachByName($name, $options = array(), $breakChainOnFailure = false)
149 $validator = $this->plugin($name, $options);
150 $this->attach($validator, $breakChainOnFailure);
151 return $this;
155 * Proxy to attachByName() to keep BC
157 * @deprecated Please use attachByName()
158 * @param string $name
159 * @param array $options
160 * @param bool $breakChainOnFailure
161 * @return ValidatorChain
163 public function addByName($name, $options = array(), $breakChainOnFailure = false)
165 return $this->attachByName($name, $options, $breakChainOnFailure);
169 * Use the plugin manager to prepend a validator by name
171 * @param string $name
172 * @param array $options
173 * @param bool $breakChainOnFailure
174 * @return ValidatorChain
176 public function prependByName($name, $options = array(), $breakChainOnFailure = false)
178 $validator = $this->plugin($name, $options);
179 $this->prependValidator($validator, $breakChainOnFailure);
180 return $this;
184 * Returns true if and only if $value passes all validations in the chain
186 * Validators are run in the order in which they were added to the chain (FIFO).
188 * @param mixed $value
189 * @param mixed $context Extra "context" to provide the validator
190 * @return bool
192 public function isValid($value, $context = null)
194 $this->messages = array();
195 $result = true;
196 foreach ($this->validators as $element) {
197 $validator = $element['instance'];
198 if ($validator->isValid($value, $context)) {
199 continue;
201 $result = false;
202 $messages = $validator->getMessages();
203 $this->messages = array_replace_recursive($this->messages, $messages);
204 if ($element['breakChainOnFailure']) {
205 break;
208 return $result;
212 * Merge the validator chain with the one given in parameter
214 * @param ValidatorChain $validatorChain
215 * @return ValidatorChain
217 public function merge(ValidatorChain $validatorChain)
219 foreach ($validatorChain->validators as $validator) {
220 $this->validators[] = $validator;
223 return $this;
227 * Returns array of validation failure messages
229 * @return array
231 public function getMessages()
233 return $this->messages;
237 * Get all the validators
239 * @return array
241 public function getValidators()
243 return $this->validators;
247 * Invoke chain as command
249 * @param mixed $value
250 * @return bool
252 public function __invoke($value)
254 return $this->isValid($value);
258 * Prepare validator chain for serialization
260 * Plugin manager (property 'plugins') cannot
261 * be serialized. On wakeup the property remains unset
262 * and next invokation to getPluginManager() sets
263 * the default plugin manager instance (ValidatorPluginManager).
265 * @return array
267 public function __sleep()
269 return array('validators', 'messages');