2
0

Validate.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Interface
  23. */
  24. require_once 'Zend/Validate/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate implements Zend_Validate_Interface
  32. {
  33. /**
  34. * Validator chain
  35. *
  36. * @var array
  37. */
  38. protected $_validators = array();
  39. /**
  40. * Array of validation failure messages
  41. *
  42. * @var array
  43. */
  44. protected $_messages = array();
  45. /**
  46. * Array of validation failure message codes
  47. *
  48. * @var array
  49. * @deprecated Since 1.5.0
  50. */
  51. protected $_errors = array();
  52. /**
  53. * Adds a validator to the end of the chain
  54. *
  55. * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
  56. * if one exists, will not be executed.
  57. *
  58. * @param Zend_Validate_Interface $validator
  59. * @param boolean $breakChainOnFailure
  60. * @return Zend_Validate Provides a fluent interface
  61. */
  62. public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false)
  63. {
  64. $this->_validators[] = array(
  65. 'instance' => $validator,
  66. 'breakChainOnFailure' => (boolean) $breakChainOnFailure
  67. );
  68. return $this;
  69. }
  70. /**
  71. * Returns true if and only if $value passes all validations in the chain
  72. *
  73. * Validators are run in the order in which they were added to the chain (FIFO).
  74. *
  75. * @param mixed $value
  76. * @return boolean
  77. */
  78. public function isValid($value)
  79. {
  80. $this->_messages = array();
  81. $this->_errors = array();
  82. $result = true;
  83. foreach ($this->_validators as $element) {
  84. $validator = $element['instance'];
  85. if ($validator->isValid($value)) {
  86. continue;
  87. }
  88. $result = false;
  89. $messages = $validator->getMessages();
  90. $this->_messages = array_merge($this->_messages, $messages);
  91. $this->_errors = array_merge($this->_errors, array_keys($messages));
  92. if ($element['breakChainOnFailure']) {
  93. break;
  94. }
  95. }
  96. return $result;
  97. }
  98. /**
  99. * Defined by Zend_Validate_Interface
  100. *
  101. * Returns array of validation failure messages
  102. *
  103. * @return array
  104. */
  105. public function getMessages()
  106. {
  107. return $this->_messages;
  108. }
  109. /**
  110. * Defined by Zend_Validate_Interface
  111. *
  112. * Returns array of validation failure message codes
  113. *
  114. * @return array
  115. * @deprecated Since 1.5.0
  116. */
  117. public function getErrors()
  118. {
  119. return $this->_errors;
  120. }
  121. /**
  122. * @param mixed $value
  123. * @param string $classBaseName
  124. * @param array $args OPTIONAL
  125. * @param mixed $namespaces OPTIONAL
  126. * @return boolean
  127. * @throws Zend_Validate_Exception
  128. */
  129. public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
  130. {
  131. $namespaces = array_merge((array) $namespaces, array('Zend_Validate'));
  132. foreach ($namespaces as $namespace) {
  133. $className = $namespace . '_' . ucfirst($classBaseName);
  134. try {
  135. if (!class_exists($className)) {
  136. require_once 'Zend/Loader.php';
  137. Zend_Loader::loadClass($className);
  138. }
  139. $class = new ReflectionClass($className);
  140. if ($class->implementsInterface('Zend_Validate_Interface')) {
  141. if ($class->hasMethod('__construct')) {
  142. $object = $class->newInstanceArgs($args);
  143. } else {
  144. $object = $class->newInstance();
  145. }
  146. return $object->isValid($value);
  147. }
  148. } catch (Zend_Validate_Exception $ze) {
  149. // if there is an exception while validating throw it
  150. throw $ze;
  151. } catch (Zend_Exception $ze) {
  152. // fallthrough and continue for missing validation classes
  153. }
  154. }
  155. require_once 'Zend/Validate/Exception.php';
  156. throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
  157. }
  158. }