PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Validate.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 290 lines | 132 code | 24 blank | 134 comment | 12 complexity | a653bdfab752a606ea15b9ee819ebc16 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Validate.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. $className = ucfirst($classBaseName);
  183. try {
  184. if (!class_exists($className, false)) {
  185. require_once 'Zend/Loader.php';
  186. foreach($namespaces as $namespace) {
  187. $class = $namespace . '_' . $className;
  188. $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
  189. if (Zend_Loader::isReadable($file)) {
  190. Zend_Loader::loadClass($class);
  191. $className = $class;
  192. break;
  193. }
  194. }
  195. }
  196. $class = new ReflectionClass($className);
  197. if ($class->implementsInterface('Zend_Validate_Interface')) {
  198. if ($class->hasMethod('__construct')) {
  199. $keys = array_keys($args);
  200. $numeric = false;
  201. foreach($keys as $key) {
  202. if (is_numeric($key)) {
  203. $numeric = true;
  204. break;
  205. }
  206. }
  207. if ($numeric) {
  208. $object = $class->newInstanceArgs($args);
  209. } else {
  210. $object = $class->newInstance($args);
  211. }
  212. } else {
  213. $object = $class->newInstance();
  214. }
  215. return $object->isValid($value);
  216. }
  217. } catch (Zend_Validate_Exception $ze) {
  218. // if there is an exception while validating throw it
  219. throw $ze;
  220. } catch (Exception $e) {
  221. // fallthrough and continue for missing validation classes
  222. }
  223. require_once 'Zend/Validate/Exception.php';
  224. throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
  225. }
  226. /**
  227. * Returns the maximum allowed message length
  228. *
  229. * @return integer
  230. */
  231. public static function getMessageLength()
  232. {
  233. require_once 'Zend/Validate/Abstract.php';
  234. return Zend_Validate_Abstract::getMessageLength();
  235. }
  236. /**
  237. * Sets the maximum allowed message length
  238. *
  239. * @param integer $length
  240. */
  241. public static function setMessageLength($length = -1)
  242. {
  243. require_once 'Zend/Validate/Abstract.php';
  244. Zend_Validate_Abstract::setMessageLength($length);
  245. }
  246. /**
  247. * Returns the default translation object
  248. *
  249. * @return Zend_Translate_Adapter|null
  250. */
  251. public static function getDefaultTranslator($translator = null)
  252. {
  253. require_once 'Zend/Validate/Abstract.php';
  254. return Zend_Validate_Abstract::getDefaultTranslator();
  255. }
  256. /**
  257. * Sets a default translation object for all validation objects
  258. *
  259. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  260. */
  261. public static function setDefaultTranslator($translator = null)
  262. {
  263. require_once 'Zend/Validate/Abstract.php';
  264. Zend_Validate_Abstract::setDefaultTranslator($translator);
  265. }
  266. }