Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/Abstract.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Interface.php';
00026 
00033 abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
00034 {
00040     protected $_value;
00041 
00047     protected $_messageVariables = array();
00048 
00054     protected $_messageTemplates = array();
00055 
00061     protected $_messages = array();
00062 
00068     protected $_obscureValue = false;
00069 
00076     protected $_errors = array();
00077 
00082     protected $_translator;
00083 
00088     protected static $_defaultTranslator;
00089 
00094     protected $_translatorDisabled = false;
00095 
00101     protected static $_messageLength = -1;
00102 
00108     public function getMessages()
00109     {
00110         return $this->_messages;
00111     }
00112 
00118     public function getMessageVariables()
00119     {
00120         return array_keys($this->_messageVariables);
00121     }
00122 
00128     public function getMessageTemplates()
00129     {
00130         return $this->_messageTemplates;
00131     }
00132 
00141     public function setMessage($messageString, $messageKey = null)
00142     {
00143         if ($messageKey === null) {
00144             $keys = array_keys($this->_messageTemplates);
00145             foreach($keys as $key) {
00146                 $this->setMessage($messageString, $key);
00147             }
00148             return $this;
00149         }
00150 
00151         if (!isset($this->_messageTemplates[$messageKey])) {
00152             require_once 'Zend/Validate/Exception.php';
00153             throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
00154         }
00155 
00156         $this->_messageTemplates[$messageKey] = $messageString;
00157         return $this;
00158     }
00159 
00167     public function setMessages(array $messages)
00168     {
00169         foreach ($messages as $key => $message) {
00170             $this->setMessage($message, $key);
00171         }
00172         return $this;
00173     }
00174 
00183     public function __get($property)
00184     {
00185         if ($property == 'value') {
00186             return $this->_value;
00187         }
00188         if (array_key_exists($property, $this->_messageVariables)) {
00189             return $this->{$this->_messageVariables[$property]};
00190         }
00194         require_once 'Zend/Validate/Exception.php';
00195         throw new Zend_Validate_Exception("No property exists by the name '$property'");
00196     }
00197 
00210     protected function _createMessage($messageKey, $value)
00211     {
00212         if (!isset($this->_messageTemplates[$messageKey])) {
00213             return null;
00214         }
00215 
00216         $message = $this->_messageTemplates[$messageKey];
00217 
00218         if (null !== ($translator = $this->getTranslator())) {
00219             if ($translator->isTranslated($messageKey)) {
00220                 $message = $translator->translate($messageKey);
00221             } else {
00222                 $message = $translator->translate($message);
00223             }
00224         }
00225 
00226         if (is_object($value)) {
00227             if (!in_array('__toString', get_class_methods($value))) {
00228                 $value = get_class($value) . ' object';
00229             } else {
00230                 $value = $value->__toString();
00231             }
00232         } else {
00233             $value = (string)$value;
00234         }
00235 
00236         if ($this->getObscureValue()) {
00237             $value = str_repeat('*', strlen($value));
00238         }
00239 
00240         $message = str_replace('%value%', (string) $value, $message);
00241         foreach ($this->_messageVariables as $ident => $property) {
00242             $message = str_replace("%$ident%", (string) $this->$property, $message);
00243         }
00244 
00245         $length = self::getMessageLength();
00246         if (($length > -1) && (strlen($message) > $length)) {
00247             $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
00248         }
00249 
00250         return $message;
00251     }
00252 
00258     protected function _error($messageKey, $value = null)
00259     {
00260         if ($messageKey === null) {
00261             $keys = array_keys($this->_messageTemplates);
00262             $messageKey = current($keys);
00263         }
00264         if ($value === null) {
00265             $value = $this->_value;
00266         }
00267         $this->_errors[]              = $messageKey;
00268         $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
00269     }
00270 
00277     protected function _setValue($value)
00278     {
00279         $this->_value    = $value;
00280         $this->_messages = array();
00281         $this->_errors   = array();
00282     }
00283 
00290     public function getErrors()
00291     {
00292         return $this->_errors;
00293     }
00294 
00301     public function setObscureValue($flag)
00302     {
00303         $this->_obscureValue = (bool) $flag;
00304         return $this;
00305     }
00306 
00313     public function getObscureValue()
00314     {
00315         return $this->_obscureValue;
00316     }
00317 
00324     public function setTranslator($translator = null)
00325     {
00326         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
00327             $this->_translator = $translator;
00328         } elseif ($translator instanceof Zend_Translate) {
00329             $this->_translator = $translator->getAdapter();
00330         } else {
00331             require_once 'Zend/Validate/Exception.php';
00332             throw new Zend_Validate_Exception('Invalid translator specified');
00333         }
00334         return $this;
00335     }
00336 
00342     public function getTranslator()
00343     {
00344         if ($this->translatorIsDisabled()) {
00345             return null;
00346         }
00347 
00348         if (null === $this->_translator) {
00349             return self::getDefaultTranslator();
00350         }
00351 
00352         return $this->_translator;
00353     }
00354 
00360     public function hasTranslator()
00361     {
00362         return (bool)$this->_translator;
00363     }
00364 
00371     public static function setDefaultTranslator($translator = null)
00372     {
00373         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
00374             self::$_defaultTranslator = $translator;
00375         } elseif ($translator instanceof Zend_Translate) {
00376             self::$_defaultTranslator = $translator->getAdapter();
00377         } else {
00378             require_once 'Zend/Validate/Exception.php';
00379             throw new Zend_Validate_Exception('Invalid translator specified');
00380         }
00381     }
00382 
00388     public static function getDefaultTranslator()
00389     {
00390         if (null === self::$_defaultTranslator) {
00391             require_once 'Zend/Registry.php';
00392             if (Zend_Registry::isRegistered('Zend_Translate')) {
00393                 $translator = Zend_Registry::get('Zend_Translate');
00394                 if ($translator instanceof Zend_Translate_Adapter) {
00395                     return $translator;
00396                 } elseif ($translator instanceof Zend_Translate) {
00397                     return $translator->getAdapter();
00398                 }
00399             }
00400         }
00401 
00402         return self::$_defaultTranslator;
00403     }
00404 
00410     public static function hasDefaultTranslator()
00411     {
00412         return (bool)self::$_defaultTranslator;
00413     }
00414 
00421     public function setDisableTranslator($flag)
00422     {
00423         $this->_translatorDisabled = (bool) $flag;
00424         return $this;
00425     }
00426 
00432     public function translatorIsDisabled()
00433     {
00434         return $this->_translatorDisabled;
00435     }
00436 
00442     public static function getMessageLength()
00443     {
00444         return self::$_messageLength;
00445     }
00446 
00452     public static function setMessageLength($length = -1)
00453     {
00454         self::$_messageLength = $length;
00455     }
00456 }
 All Data Structures Namespaces Files Functions Variables Enumerations