[I18N] Zend_Filter/Zend_Validate:
[zend.git] / library / Zend / Validate / Abstract.php
blob48f3f5049ef7db85a5ab91d4f0048ce9bdb8fc35
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 abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
35 /**
36 * The value to be validated
38 * @var mixed
40 protected $_value;
42 /**
43 * Additional variables available for validation failure messages
45 * @var array
47 protected $_messageVariables = array();
49 /**
50 * Validation failure message template definitions
52 * @var array
54 protected $_messageTemplates = array();
56 /**
57 * Array of validation failure messages
59 * @var array
61 protected $_messages = array();
63 /**
64 * Flag indidcating whether or not value should be obfuscated in error
65 * messages
66 * @var bool
68 protected $_obscureValue = false;
70 /**
71 * Array of validation failure message codes
73 * @var array
74 * @deprecated Since 1.5.0
76 protected $_errors = array();
78 /**
79 * Translation object
80 * @var Zend_Translate
82 protected $_translator;
84 /**
85 * Default translation object for all validate objects
86 * @var Zend_Translate
88 protected static $_defaultTranslator;
90 /**
91 * Is translation disabled?
92 * @var Boolean
94 protected $_translatorDisabled = false;
96 /**
97 * Limits the maximum returned length of a error message
99 * @var Integer
101 protected static $_messageLength = -1;
104 * Returns array of validation failure messages
106 * @return array
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
116 * @return array
118 public function getMessageVariables()
120 return array_keys($this->_messageVariables);
124 * Returns the message templates from the validator
126 * @return array
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);
148 return $this;
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;
157 return $this;
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);
172 return $this;
176 * Magic function returns the value of the requested property, if and only if it is the value or a
177 * message variable.
179 * @param string $property
180 * @return mixed
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
208 * @return string
210 protected function _createMessage($messageKey, $value)
212 if (!isset($this->_messageTemplates[$messageKey])) {
213 return null;
216 $message = $this->_messageTemplates[$messageKey];
218 if (null !== ($translator = $this->getTranslator())) {
219 if ($translator->isTranslated($message)) {
220 $message = $translator->translate($message);
221 } else {
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';
229 } else {
230 $value = $value->__toString();
232 } else {
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)) . '...';
250 return $message;
254 * @param string $messageKey
255 * @param string $value OPTIONAL
256 * @return void
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
275 * @return void
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
287 * @return array
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
298 * @param bool $flag
299 * @return Zend_Validate_Abstract
301 public function setObscureValue($flag)
303 $this->_obscureValue = (bool) $flag;
304 return $this;
308 * Retrieve flag indicating whether or not value should be obfuscated in
309 * messages
311 * @return bool
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();
330 } else {
331 require_once 'Zend/Validate/Exception.php';
332 throw new Zend_Validate_Exception('Invalid translator specified');
334 return $this;
338 * Return translation object
340 * @return Zend_Translate_Adapter|null
342 public function getTranslator()
344 if ($this->translatorIsDisabled()) {
345 return null;
348 if (null === $this->_translator) {
349 return self::getDefaultTranslator();
352 return $this->_translator;
356 * Does this validator have its own specific translator?
358 * @return bool
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
369 * @return void
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();
377 } else {
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) {
395 return $translator;
396 } elseif ($translator instanceof Zend_Translate) {
397 return $translator->getAdapter();
402 return self::$_defaultTranslator;
406 * Is there a default translation object set?
408 * @return boolean
410 public static function hasDefaultTranslator()
412 return (bool)self::$_defaultTranslator;
416 * Indicate whether or not translation should be disabled
418 * @param bool $flag
419 * @return Zend_Validate_Abstract
421 public function setDisableTranslator($flag)
423 $this->_translatorDisabled = (bool) $flag;
424 return $this;
428 * Is translation disabled?
430 * @return bool
432 public function translatorIsDisabled()
434 return $this->_translatorDisabled;
438 * Returns the maximum allowed message length
440 * @return integer
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;