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-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id: Abstract.php 16223 2009-06-21 20:04:53Z thomas $
23 * @see Zend_Validate_Interface
25 require_once 'Zend/Validate/Interface.php';
29 * @package Zend_Validate
30 * @copyright Copyright (c) 2005-2009 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 $messageKey = current($keys);
147 if (!isset($this->_messageTemplates
[$messageKey])) {
148 require_once 'Zend/Validate/Exception.php';
149 throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
151 $this->_messageTemplates
[$messageKey] = $messageString;
156 * Sets validation failure message templates given as an array, where the array keys are the message keys,
157 * and the array values are the message template strings.
159 * @param array $messages
160 * @return Zend_Validate_Abstract
162 public function setMessages(array $messages)
164 foreach ($messages as $key => $message) {
165 $this->setMessage($message, $key);
171 * Magic function returns the value of the requested property, if and only if it is the value or a
174 * @param string $property
176 * @throws Zend_Validate_Exception
178 public function __get($property)
180 if ($property == 'value') {
181 return $this->_value
;
183 if (array_key_exists($property, $this->_messageVariables
)) {
184 return $this->{$this->_messageVariables
[$property]};
187 * @see Zend_Validate_Exception
189 require_once 'Zend/Validate/Exception.php';
190 throw new Zend_Validate_Exception("No property exists by the name '$property'");
194 * Constructs and returns a validation failure message with the given message key and value.
196 * Returns null if and only if $messageKey does not correspond to an existing template.
198 * If a translator is available and a translation exists for $messageKey,
199 * the translation will be used.
201 * @param string $messageKey
202 * @param string $value
205 protected function _createMessage($messageKey, $value)
207 if (!isset($this->_messageTemplates
[$messageKey])) {
211 $message = $this->_messageTemplates
[$messageKey];
213 if (null !== ($translator = $this->getTranslator())) {
214 if ($translator->isTranslated($message)) {
215 $message = $translator->translate($message);
216 } elseif ($translator->isTranslated($messageKey)) {
217 $message = $translator->translate($messageKey);
221 if (is_object($value)) {
222 if (!in_array('__toString', get_class_methods($value))) {
223 $value = get_class($value) . ' object';
225 $value = $value->__toString();
228 $value = (string)$value;
231 if ($this->getObscureValue()) {
232 $value = str_repeat('*', strlen($value));
235 $message = str_replace('%value%', (string) $value, $message);
236 foreach ($this->_messageVariables
as $ident => $property) {
237 $message = str_replace("%$ident%", (string) $this->$property, $message);
240 $length = self
::getMessageLength();
241 if (($length > -1) && (strlen($message) > $length)) {
242 $message = substr($message, 0, (self
::getMessageLength() - 3)) . '...';
249 * @param string $messageKey OPTIONAL
250 * @param string $value OPTIONAL
253 protected function _error($messageKey = null, $value = null)
255 if ($messageKey === null) {
256 $keys = array_keys($this->_messageTemplates
);
257 $messageKey = current($keys);
259 if ($value === null) {
260 $value = $this->_value
;
262 $this->_errors
[] = $messageKey;
263 $this->_messages
[$messageKey] = $this->_createMessage($messageKey, $value);
267 * Sets the value to be validated and clears the messages and errors arrays
269 * @param mixed $value
272 protected function _setValue($value)
274 $this->_value
= $value;
275 $this->_messages
= array();
276 $this->_errors
= array();
280 * Returns array of validation failure message codes
283 * @deprecated Since 1.5.0
285 public function getErrors()
287 return $this->_errors
;
291 * Set flag indicating whether or not value should be obfuscated in messages
294 * @return Zend_Validate_Abstract
296 public function setObscureValue($flag)
298 $this->_obscureValue
= (bool) $flag;
303 * Retrieve flag indicating whether or not value should be obfuscated in
308 public function getObscureValue()
310 return $this->_obscureValue
;
314 * Set translation object
316 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
317 * @return Zend_Validate_Abstract
319 public function setTranslator($translator = null)
321 if ((null === $translator) ||
($translator instanceof Zend_Translate_Adapter
)) {
322 $this->_translator
= $translator;
323 } elseif ($translator instanceof Zend_Translate
) {
324 $this->_translator
= $translator->getAdapter();
326 require_once 'Zend/Validate/Exception.php';
327 throw new Zend_Validate_Exception('Invalid translator specified');
333 * Return translation object
335 * @return Zend_Translate_Adapter|null
337 public function getTranslator()
339 if ($this->translatorIsDisabled()) {
343 if (null === $this->_translator
) {
344 return self
::getDefaultTranslator();
347 return $this->_translator
;
351 * Set default translation object for all validate objects
353 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
356 public static function setDefaultTranslator($translator = null)
358 if ((null === $translator) ||
($translator instanceof Zend_Translate_Adapter
)) {
359 self
::$_defaultTranslator = $translator;
360 } elseif ($translator instanceof Zend_Translate
) {
361 self
::$_defaultTranslator = $translator->getAdapter();
363 require_once 'Zend/Validate/Exception.php';
364 throw new Zend_Validate_Exception('Invalid translator specified');
369 * Get default translation object for all validate objects
371 * @return Zend_Translate_Adapter|null
373 public static function getDefaultTranslator()
375 if (null === self
::$_defaultTranslator) {
376 require_once 'Zend/Registry.php';
377 if (Zend_Registry
::isRegistered('Zend_Translate')) {
378 $translator = Zend_Registry
::get('Zend_Translate');
379 if ($translator instanceof Zend_Translate_Adapter
) {
381 } elseif ($translator instanceof Zend_Translate
) {
382 return $translator->getAdapter();
387 return self
::$_defaultTranslator;
391 * Indicate whether or not translation should be disabled
394 * @return Zend_Validate_Abstract
396 public function setDisableTranslator($flag)
398 $this->_translatorDisabled
= (bool) $flag;
403 * Is translation disabled?
407 public function translatorIsDisabled()
409 return $this->_translatorDisabled
;
413 * Returns the maximum allowed message length
417 public static function getMessageLength()
419 return self
::$_messageLength;
423 * Sets the maximum allowed message length
425 * @param integer $length
427 public static function setMessageLength($length = -1)
429 self
::$_messageLength = $length;