[ZF-9302] Zend_Validate:
[zend.git] / library / Zend / Validate.php
blob870d0e49686da791ef9b3950bfcafa5ffa85e245
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
22 /**
23 * @see Zend_Validate_Interface
25 require_once 'Zend/Validate/Interface.php';
27 /**
28 * @category Zend
29 * @package Zend_Validate
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Validate implements Zend_Validate_Interface
35 /**
36 * Validator chain
38 * @var array
40 protected $_validators = array();
42 /**
43 * Array of validation failure messages
45 * @var array
47 protected $_messages = array();
49 /**
50 * Default Namespaces
52 * @var array
54 protected static $_defaultNamespaces = array();
56 /**
57 * Array of validation failure message codes
59 * @var array
60 * @deprecated Since 1.5.0
62 protected $_errors = array();
64 /**
65 * Adds a validator to the end of the chain
67 * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
68 * if one exists, will not be executed.
70 * @param Zend_Validate_Interface $validator
71 * @param boolean $breakChainOnFailure
72 * @return Zend_Validate Provides a fluent interface
74 public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false)
76 $this->_validators[] = array(
77 'instance' => $validator,
78 'breakChainOnFailure' => (boolean) $breakChainOnFailure
80 return $this;
83 /**
84 * Returns true if and only if $value passes all validations in the chain
86 * Validators are run in the order in which they were added to the chain (FIFO).
88 * @param mixed $value
89 * @return boolean
91 public function isValid($value)
93 $this->_messages = array();
94 $this->_errors = array();
95 $result = true;
96 foreach ($this->_validators as $element) {
97 $validator = $element['instance'];
98 if ($validator->isValid($value)) {
99 continue;
101 $result = false;
102 $messages = $validator->getMessages();
103 $this->_messages = array_merge($this->_messages, $messages);
104 $this->_errors = array_merge($this->_errors, array_keys($messages));
105 if ($element['breakChainOnFailure']) {
106 break;
109 return $result;
113 * Defined by Zend_Validate_Interface
115 * Returns array of validation failure messages
117 * @return array
119 public function getMessages()
121 return $this->_messages;
125 * Defined by Zend_Validate_Interface
127 * Returns array of validation failure message codes
129 * @return array
130 * @deprecated Since 1.5.0
132 public function getErrors()
134 return $this->_errors;
138 * Returns the set default namespaces
140 * @return array
142 public static function getDefaultNamespaces()
144 return self::$_defaultNamespaces;
148 * Sets new default namespaces
150 * @param array|string $namespace
151 * @return null
153 public static function setDefaultNamespaces($namespace)
155 if (!is_array($namespace)) {
156 $namespace = array((string) $namespace);
159 self::$_defaultNamespaces = $namespace;
163 * Adds a new default namespace
165 * @param array|string $namespace
166 * @return null
168 public static function addDefaultNamespaces($namespace)
170 if (!is_array($namespace)) {
171 $namespace = array((string) $namespace);
174 self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
178 * Returns true when defaultNamespaces are set
180 * @return boolean
182 public static function hasDefaultNamespaces()
184 return (!empty(self::$_defaultNamespaces));
188 * @param mixed $value
189 * @param string $classBaseName
190 * @param array $args OPTIONAL
191 * @param mixed $namespaces OPTIONAL
192 * @return boolean
193 * @throws Zend_Validate_Exception
195 public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
197 $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate'));
198 $className = ucfirst($classBaseName);
199 try {
200 if (!class_exists($className, false)) {
201 require_once 'Zend/Loader.php';
202 foreach($namespaces as $namespace) {
203 $class = $namespace . '_' . $className;
204 $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
205 if (Zend_Loader::isReadable($file)) {
206 Zend_Loader::loadClass($class);
207 $className = $class;
208 break;
213 $class = new ReflectionClass($className);
214 if ($class->implementsInterface('Zend_Validate_Interface')) {
215 if ($class->hasMethod('__construct')) {
216 $keys = array_keys($args);
217 $numeric = false;
218 foreach($keys as $key) {
219 if (is_numeric($key)) {
220 $numeric = true;
221 break;
225 if ($numeric) {
226 $object = $class->newInstanceArgs($args);
227 } else {
228 $object = $class->newInstance($args);
230 } else {
231 $object = $class->newInstance();
234 return $object->isValid($value);
236 } catch (Zend_Validate_Exception $ze) {
237 // if there is an exception while validating throw it
238 throw $ze;
239 } catch (Exception $e) {
240 // fallthrough and continue for missing validation classes
243 require_once 'Zend/Validate/Exception.php';
244 throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
248 * Returns the maximum allowed message length
250 * @return integer
252 public static function getMessageLength()
254 require_once 'Zend/Validate/Abstract.php';
255 return Zend_Validate_Abstract::getMessageLength();
259 * Sets the maximum allowed message length
261 * @param integer $length
263 public static function setMessageLength($length = -1)
265 require_once 'Zend/Validate/Abstract.php';
266 Zend_Validate_Abstract::setMessageLength($length);