*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Validate / Abstract.php
blob0d0abbd8ab639f1c11625d3cf7e2eef59837f54c
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-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 $
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-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
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 $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;
152 return $this;
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);
167 return $this;
171 * Magic function returns the value of the requested property, if and only if it is the value or a
172 * message variable.
174 * @param string $property
175 * @return mixed
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
203 * @return string
205 protected function _createMessage($messageKey, $value)
207 if (!isset($this->_messageTemplates[$messageKey])) {
208 return null;
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';
224 } else {
225 $value = $value->__toString();
227 } else {
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)) . '...';
245 return $message;
249 * @param string $messageKey OPTIONAL
250 * @param string $value OPTIONAL
251 * @return void
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
270 * @return void
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
282 * @return array
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
293 * @param bool $flag
294 * @return Zend_Validate_Abstract
296 public function setObscureValue($flag)
298 $this->_obscureValue = (bool) $flag;
299 return $this;
303 * Retrieve flag indicating whether or not value should be obfuscated in
304 * messages
306 * @return bool
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();
325 } else {
326 require_once 'Zend/Validate/Exception.php';
327 throw new Zend_Validate_Exception('Invalid translator specified');
329 return $this;
333 * Return translation object
335 * @return Zend_Translate_Adapter|null
337 public function getTranslator()
339 if ($this->translatorIsDisabled()) {
340 return null;
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
354 * @return void
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();
362 } else {
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) {
380 return $translator;
381 } elseif ($translator instanceof Zend_Translate) {
382 return $translator->getAdapter();
387 return self::$_defaultTranslator;
391 * Indicate whether or not translation should be disabled
393 * @param bool $flag
394 * @return Zend_Validate_Abstract
396 public function setDisableTranslator($flag)
398 $this->_translatorDisabled = (bool) $flag;
399 return $this;
403 * Is translation disabled?
405 * @return bool
407 public function translatorIsDisabled()
409 return $this->_translatorDisabled;
413 * Returns the maximum allowed message length
415 * @return integer
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;