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.
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
23 * @see Zend_Validate_Interface
25 require_once 'Zend/Validate/Interface.php';
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 abstract class Zend_Validate_Abstract
implements Zend_Validate_Interface
36 * The value to be validated
43 * Additional variables available for validation failure messages
47 protected $_messageVariables = array();
50 * Validation failure message template definitions
54 protected $_messageTemplates = array();
57 * Array of validation failure messages
61 protected $_messages = array();
64 * Flag indidcating whether or not value should be obfuscated in error
68 protected $_obscureValue = false;
71 * Array of validation failure message codes
74 * @deprecated Since 1.5.0
76 protected $_errors = array();
82 protected $_translator;
85 * Default translation object for all validate objects
88 protected static $_defaultTranslator;
91 * Is translation disabled?
94 protected $_translatorDisabled = false;
97 * Limits the maximum returned length of a error message
101 protected static $_messageLength = -1;
104 * Returns array of validation failure messages
108 public function getMessages()
110 return $this->_messages
;
114 * Returns an array of the names of variables that are used in constructing validation failure messages
118 public function getMessageVariables()
120 return array_keys($this->_messageVariables
);
124 * Returns the message templates from the validator
128 public function getMessageTemplates()
130 return $this->_messageTemplates
;
134 * Sets the validation failure message template for a particular key
136 * @param string $messageString
137 * @param string $messageKey OPTIONAL
138 * @return Zend_Validate_Abstract Provides a fluent interface
139 * @throws Zend_Validate_Exception
141 public function setMessage($messageString, $messageKey = null)
143 if ($messageKey === null) {
144 $keys = array_keys($this->_messageTemplates
);
145 foreach($keys as $key) {
146 $this->setMessage($messageString, $key);
151 if (!isset($this->_messageTemplates
[$messageKey])) {
152 require_once 'Zend/Validate/Exception.php';
153 throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
156 $this->_messageTemplates
[$messageKey] = $messageString;
161 * Sets validation failure message templates given as an array, where the array keys are the message keys,
162 * and the array values are the message template strings.
164 * @param array $messages
165 * @return Zend_Validate_Abstract
167 public function setMessages(array $messages)
169 foreach ($messages as $key => $message) {
170 $this->setMessage($message, $key);
176 * Magic function returns the value of the requested property, if and only if it is the value or a
179 * @param string $property
181 * @throws Zend_Validate_Exception
183 public function __get($property)
185 if ($property == 'value') {
186 return $this->_value
;
188 if (array_key_exists($property, $this->_messageVariables
)) {
189 return $this->{$this->_messageVariables
[$property]};
192 * @see Zend_Validate_Exception
194 require_once 'Zend/Validate/Exception.php';
195 throw new Zend_Validate_Exception("No property exists by the name '$property'");
199 * Constructs and returns a validation failure message with the given message key and value.
201 * Returns null if and only if $messageKey does not correspond to an existing template.
203 * If a translator is available and a translation exists for $messageKey,
204 * the translation will be used.
206 * @param string $messageKey
207 * @param string $value
210 protected function _createMessage($messageKey, $value)
212 if (!isset($this->_messageTemplates
[$messageKey])) {
216 $message = $this->_messageTemplates
[$messageKey];
218 if (null !== ($translator = $this->getTranslator())) {
219 if ($translator->isTranslated($message)) {
220 $message = $translator->translate($message);
222 $message = $translator->translate($messageKey);
226 if (is_object($value)) {
227 if (!in_array('__toString', get_class_methods($value))) {
228 $value = get_class($value) . ' object';
230 $value = $value->__toString();
233 $value = (string)$value;
236 if ($this->getObscureValue()) {
237 $value = str_repeat('*', strlen($value));
240 $message = str_replace('%value%', (string) $value, $message);
241 foreach ($this->_messageVariables
as $ident => $property) {
242 $message = str_replace("%$ident%", (string) $this->$property, $message);
245 $length = self
::getMessageLength();
246 if (($length > -1) && (strlen($message) > $length)) {
247 $message = substr($message, 0, (self
::getMessageLength() - 3)) . '...';
254 * @param string $messageKey
255 * @param string $value OPTIONAL
258 protected function _error($messageKey, $value = null)
260 if ($messageKey === null) {
261 $keys = array_keys($this->_messageTemplates
);
262 $messageKey = current($keys);
264 if ($value === null) {
265 $value = $this->_value
;
267 $this->_errors
[] = $messageKey;
268 $this->_messages
[$messageKey] = $this->_createMessage($messageKey, $value);
272 * Sets the value to be validated and clears the messages and errors arrays
274 * @param mixed $value
277 protected function _setValue($value)
279 $this->_value
= $value;
280 $this->_messages
= array();
281 $this->_errors
= array();
285 * Returns array of validation failure message codes
288 * @deprecated Since 1.5.0
290 public function getErrors()
292 return $this->_errors
;
296 * Set flag indicating whether or not value should be obfuscated in messages
299 * @return Zend_Validate_Abstract
301 public function setObscureValue($flag)
303 $this->_obscureValue
= (bool) $flag;
308 * Retrieve flag indicating whether or not value should be obfuscated in
313 public function getObscureValue()
315 return $this->_obscureValue
;
319 * Set translation object
321 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
322 * @return Zend_Validate_Abstract
324 public function setTranslator($translator = null)
326 if ((null === $translator) ||
($translator instanceof Zend_Translate_Adapter
)) {
327 $this->_translator
= $translator;
328 } elseif ($translator instanceof Zend_Translate
) {
329 $this->_translator
= $translator->getAdapter();
331 require_once 'Zend/Validate/Exception.php';
332 throw new Zend_Validate_Exception('Invalid translator specified');
338 * Return translation object
340 * @return Zend_Translate_Adapter|null
342 public function getTranslator()
344 if ($this->translatorIsDisabled()) {
348 if (null === $this->_translator
) {
349 return self
::getDefaultTranslator();
352 return $this->_translator
;
356 * Does this validator have its own specific translator?
360 public function hasTranslator()
362 return (bool)$this->_translator
;
366 * Set default translation object for all validate objects
368 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
371 public static function setDefaultTranslator($translator = null)
373 if ((null === $translator) ||
($translator instanceof Zend_Translate_Adapter
)) {
374 self
::$_defaultTranslator = $translator;
375 } elseif ($translator instanceof Zend_Translate
) {
376 self
::$_defaultTranslator = $translator->getAdapter();
378 require_once 'Zend/Validate/Exception.php';
379 throw new Zend_Validate_Exception('Invalid translator specified');
384 * Get default translation object for all validate objects
386 * @return Zend_Translate_Adapter|null
388 public static function getDefaultTranslator()
390 if (null === self
::$_defaultTranslator) {
391 require_once 'Zend/Registry.php';
392 if (Zend_Registry
::isRegistered('Zend_Translate')) {
393 $translator = Zend_Registry
::get('Zend_Translate');
394 if ($translator instanceof Zend_Translate_Adapter
) {
396 } elseif ($translator instanceof Zend_Translate
) {
397 return $translator->getAdapter();
402 return self
::$_defaultTranslator;
406 * Is there a default translation object set?
410 public static function hasDefaultTranslator()
412 return (bool)self
::$_defaultTranslator;
416 * Indicate whether or not translation should be disabled
419 * @return Zend_Validate_Abstract
421 public function setDisableTranslator($flag)
423 $this->_translatorDisabled
= (bool) $flag;
428 * Is translation disabled?
432 public function translatorIsDisabled()
434 return $this->_translatorDisabled
;
438 * Returns the maximum allowed message length
442 public static function getMessageLength()
444 return self
::$_messageLength;
448 * Sets the maximum allowed message length
450 * @param integer $length
452 public static function setMessageLength($length = -1)
454 self
::$_messageLength = $length;