Validate.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-2009 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-2009 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. * Default Namespaces
  47. *
  48. * @var array
  49. */
  50. protected static $_defaultNamespaces = array();
  51. /**
  52. * Array of validation failure message codes
  53. *
  54. * @var array
  55. * @deprecated Since 1.5.0
  56. */
  57. protected $_errors = array();
  58. /**
  59. * Adds a validator to the end of the chain
  60. *
  61. * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
  62. * if one exists, will not be executed.
  63. *
  64. * @param Zend_Validate_Interface $validator
  65. * @param boolean $breakChainOnFailure
  66. * @return Zend_Validate Provides a fluent interface
  67. */
  68. public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false)
  69. {
  70. $this->_validators[] = array(
  71. 'instance' => $validator,
  72. 'breakChainOnFailure' => (boolean) $breakChainOnFailure
  73. );
  74. return $this;
  75. }
  76. /**
  77. * Returns true if and only if $value passes all validations in the chain
  78. *
  79. * Validators are run in the order in which they were added to the chain (FIFO).
  80. *
  81. * @param mixed $value
  82. * @return boolean
  83. */
  84. public function isValid($value)
  85. {
  86. $this->_messages = array();
  87. $this->_errors = array();
  88. $result = true;
  89. foreach ($this->_validators as $element) {
  90. $validator = $element['instance'];
  91. if ($validator->isValid($value)) {
  92. continue;
  93. }
  94. $result = false;
  95. $messages = $validator->getMessages();
  96. $this->_messages = array_merge($this->_messages, $messages);
  97. $this->_errors = array_merge($this->_errors, array_keys($messages));
  98. if ($element['breakChainOnFailure']) {
  99. break;
  100. }
  101. }
  102. return $result;
  103. }
  104. /**
  105. * Defined by Zend_Validate_Interface
  106. *
  107. * Returns array of validation failure messages
  108. *
  109. * @return array
  110. */
  111. public function getMessages()
  112. {
  113. return $this->_messages;
  114. }
  115. /**
  116. * Defined by Zend_Validate_Interface
  117. *
  118. * Returns array of validation failure message codes
  119. *
  120. * @return array
  121. * @deprecated Since 1.5.0
  122. */
  123. public function getErrors()
  124. {
  125. return $this->_errors;
  126. }
  127. /**
  128. * Returns the set default namespaces
  129. *
  130. * @return array
  131. */
  132. public static function getDefaultNamespaces()
  133. {
  134. return self::$_defaultNamespaces;
  135. }
  136. /**
  137. * Sets new default namespaces
  138. *
  139. * @param array|string $namespace
  140. * @return null
  141. */
  142. public static function setDefaultNamespaces($namespace)
  143. {
  144. if (!is_array($namespace)) {
  145. $namespace = array((string) $namespace);
  146. }
  147. self::$_defaultNamespaces = $namespace;
  148. }
  149. /**
  150. * Adds a new default namespace
  151. *
  152. * @param array|string $namespace
  153. * @return null
  154. */
  155. public static function addDefaultNamespaces($namespace)
  156. {
  157. if (!is_array($namespace)) {
  158. $namespace = array((string) $namespace);
  159. }
  160. self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
  161. }
  162. /**
  163. * Returns true when defaultNamespaces are set
  164. *
  165. * @return boolean
  166. */
  167. public static function hasDefaultNamespaces()
  168. {
  169. return (!empty(self::$_defaultNamespaces));
  170. }
  171. /**
  172. * @param mixed $value
  173. * @param string $classBaseName
  174. * @param array $args OPTIONAL
  175. * @param mixed $namespaces OPTIONAL
  176. * @return boolean
  177. * @throws Zend_Validate_Exception
  178. */
  179. public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
  180. {
  181. $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate'));
  182. foreach ($namespaces as $namespace) {
  183. $className = $namespace . '_' . ucfirst($classBaseName);
  184. try {
  185. if (!class_exists($className)) {
  186. require_once 'Zend/Loader.php';
  187. Zend_Loader::loadClass($className);
  188. }
  189. $class = new ReflectionClass($className);
  190. if ($class->implementsInterface('Zend_Validate_Interface')) {
  191. if ($class->hasMethod('__construct')) {
  192. $keys = array_keys($args);
  193. $numeric = false;
  194. foreach($keys as $key) {
  195. if (is_numeric($key)) {
  196. $numeric = true;
  197. break;
  198. }
  199. }
  200. if ($numeric) {
  201. $object = $class->newInstanceArgs($args);
  202. } else {
  203. $object = $class->newInstance($args);
  204. }
  205. } else {
  206. $object = $class->newInstance();
  207. }
  208. return $object->isValid($value);
  209. }
  210. } catch (Zend_Validate_Exception $ze) {
  211. // if there is an exception while validating throw it
  212. throw $ze;
  213. } catch (Zend_Exception $ze) {
  214. // fallthrough and continue for missing validation classes
  215. }
  216. }
  217. require_once 'Zend/Validate/Exception.php';
  218. throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
  219. }
  220. /**
  221. * Returns the maximum allowed message length
  222. *
  223. * @return integer
  224. */
  225. public static function getMessageLength()
  226. {
  227. require_once 'Zend/Validate/Abstract.php';
  228. return Zend_Validate_Abstract::getMessageLength();
  229. }
  230. /**
  231. * Sets the maximum allowed message length
  232. *
  233. * @param integer $length
  234. */
  235. public static function setMessageLength($length = -1)
  236. {
  237. require_once 'Zend/Validate/Abstract.php';
  238. Zend_Validate_Abstract::setMessageLength($length);
  239. }
  240. }