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\Validator
;
14 class ValidatorChain
implements
19 * @var ValidatorPluginManager
28 protected $validators = array();
31 * Array of validation failure messages
35 protected $messages = array();
38 * Return the count of attached validators
42 public function count()
44 return count($this->validators
);
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
;
61 * Set plugin manager instance
63 * @param ValidatorPluginManager $plugins Plugin manager
64 * @return ValidatorChain
66 public function setPluginManager(ValidatorPluginManager
$plugins)
68 $this->plugins
= $plugins;
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);
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,
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)
132 'instance' => $validator,
133 'breakChainOnFailure' => (bool) $breakChainOnFailure,
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);
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);
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
192 public function isValid($value, $context = null)
194 $this->messages
= array();
196 foreach ($this->validators
as $element) {
197 $validator = $element['instance'];
198 if ($validator->isValid($value, $context)) {
202 $messages = $validator->getMessages();
203 $this->messages
= array_replace_recursive($this->messages
, $messages);
204 if ($element['breakChainOnFailure']) {
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;
227 * Returns array of validation failure messages
231 public function getMessages()
233 return $this->messages
;
237 * Get all the validators
241 public function getValidators()
243 return $this->validators
;
247 * Invoke chain as command
249 * @param mixed $value
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).
267 public function __sleep()
269 return array('validators', 'messages');