/library/Zend/Validator/ValidatorChain.php

https://github.com/pborreli/zf2 · PHP · 233 lines · 99 code · 19 blank · 115 comment · 3 complexity · fea1e2bca373b11e8d7c51331a5e6dca MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Validator
  9. */
  10. namespace Zend\Validator;
  11. use Countable;
  12. /**
  13. * @category Zend
  14. * @package Zend_Validator
  15. */
  16. class ValidatorChain implements
  17. Countable,
  18. ValidatorInterface
  19. {
  20. /**
  21. * @var ValidatorPluginManager
  22. */
  23. protected $plugins;
  24. /**
  25. * Validator chain
  26. *
  27. * @var array
  28. */
  29. protected $validators = array();
  30. /**
  31. * Array of validation failure messages
  32. *
  33. * @var array
  34. */
  35. protected $messages = array();
  36. /**
  37. * Return the count of attached validators
  38. *
  39. * @return int
  40. */
  41. public function count()
  42. {
  43. return count($this->validators);
  44. }
  45. /**
  46. * Get plugin manager instance
  47. *
  48. * @return ValidatorPluginManager
  49. */
  50. public function getPluginManager()
  51. {
  52. if (!$this->plugins) {
  53. $this->setPluginManager(new ValidatorPluginManager());
  54. }
  55. return $this->plugins;
  56. }
  57. /**
  58. * Set plugin manager instance
  59. *
  60. * @param ValidatorPluginManager $plugins Plugin manager
  61. * @return ValidatorChain
  62. */
  63. public function setPluginManager(ValidatorPluginManager $plugins)
  64. {
  65. $this->plugins = $plugins;
  66. return $this;
  67. }
  68. /**
  69. * Retrieve a validator by name
  70. *
  71. * @param string $name Name of validator to return
  72. * @param null|array $options Options to pass to validator constructor (if not already instantiated)
  73. * @return ValidatorInterface
  74. */
  75. public function plugin($name, array $options = null)
  76. {
  77. $plugins = $this->getPluginManager();
  78. return $plugins->get($name, $options);
  79. }
  80. /**
  81. * Adds a validator to the end of the chain
  82. *
  83. * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
  84. * if one exists, will not be executed.
  85. *
  86. * @param ValidatorInterface $validator
  87. * @param boolean $breakChainOnFailure
  88. * @return ValidatorChain Provides a fluent interface
  89. */
  90. public function addValidator(ValidatorInterface $validator, $breakChainOnFailure = false)
  91. {
  92. $this->validators[] = array(
  93. 'instance' => $validator,
  94. 'breakChainOnFailure' => (boolean)$breakChainOnFailure
  95. );
  96. return $this;
  97. }
  98. /**
  99. * Adds a validator to the beginning of the chain
  100. *
  101. * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
  102. * if one exists, will not be executed.
  103. *
  104. * @param ValidatorInterface $validator
  105. * @param boolean $breakChainOnFailure
  106. * @return ValidatorChain Provides a fluent interface
  107. */
  108. public function prependValidator(ValidatorInterface $validator, $breakChainOnFailure = false)
  109. {
  110. array_unshift($this->validators,
  111. array(
  112. 'instance' => $validator,
  113. 'breakChainOnFailure' => (boolean)$breakChainOnFailure
  114. )
  115. );
  116. return $this;
  117. }
  118. /**
  119. * Use the plugin manager to add a validator by name
  120. *
  121. * @param string $name
  122. * @param array $options
  123. * @param bool $breakChainOnFailure
  124. * @return ValidatorChain
  125. */
  126. public function addByName($name, $options = array(), $breakChainOnFailure = false)
  127. {
  128. $validator = $this->plugin($name, $options);
  129. $this->addValidator($validator, $breakChainOnFailure);
  130. return $this;
  131. }
  132. /**
  133. * Use the plugin manager to prepend a validator by name
  134. *
  135. * @param string $name
  136. * @param array $options
  137. * @param bool $breakChainOnFailure
  138. * @return ValidatorChain
  139. */
  140. public function prependByName($name, $options = array(), $breakChainOnFailure = false)
  141. {
  142. $validator = $this->plugin($name, $options);
  143. $this->prependValidator($validator, $breakChainOnFailure);
  144. return $this;
  145. }
  146. /**
  147. * Returns true if and only if $value passes all validations in the chain
  148. *
  149. * Validators are run in the order in which they were added to the chain (FIFO).
  150. *
  151. * @param mixed $value
  152. * @param mixed $context Extra "context" to provide the validator
  153. * @return boolean
  154. */
  155. public function isValid($value, $context = null)
  156. {
  157. $this->messages = array();
  158. $result = true;
  159. foreach ($this->validators as $element) {
  160. $validator = $element['instance'];
  161. if ($validator->isValid($value, $context)) {
  162. continue;
  163. }
  164. $result = false;
  165. $messages = $validator->getMessages();
  166. $this->messages = array_replace_recursive($this->messages, $messages);
  167. if ($element['breakChainOnFailure']) {
  168. break;
  169. }
  170. }
  171. return $result;
  172. }
  173. /**
  174. * Merge the validator chain with the one given in parameter
  175. *
  176. * @param ValidatorChain $validatorChain
  177. * @return ValidatorChain
  178. */
  179. public function merge(ValidatorChain $validatorChain)
  180. {
  181. foreach ($validatorChain->validators as $validator) {
  182. $this->validators[] = $validator;
  183. }
  184. return $this;
  185. }
  186. /**
  187. * Returns array of validation failure messages
  188. *
  189. * @return array
  190. */
  191. public function getMessages()
  192. {
  193. return $this->messages;
  194. }
  195. /**
  196. * Get all the validators
  197. *
  198. * @return array
  199. */
  200. public function getValidators()
  201. {
  202. return $this->validators;
  203. }
  204. /**
  205. * Invoke chain as command
  206. *
  207. * @param mixed $value
  208. * @return boolean
  209. */
  210. public function __invoke($value)
  211. {
  212. return $this->isValid($value);
  213. }
  214. }