File indexing completed on 2024-05-12 06:03:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Interface
0024  */
0025 // require_once 'Zend/Validate/Interface.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
0034 {
0035     /**
0036      * The value to be validated
0037      *
0038      * @var mixed
0039      */
0040     protected $_value;
0041 
0042     /**
0043      * Additional variables available for validation failure messages
0044      *
0045      * @var array
0046      */
0047     protected $_messageVariables = array();
0048 
0049     /**
0050      * Validation failure message template definitions
0051      *
0052      * @var array
0053      */
0054     protected $_messageTemplates = array();
0055 
0056     /**
0057      * Array of validation failure messages
0058      *
0059      * @var array
0060      */
0061     protected $_messages = array();
0062 
0063     /**
0064      * Flag indidcating whether or not value should be obfuscated in error
0065      * messages
0066      * @var bool
0067      */
0068     protected $_obscureValue = false;
0069 
0070     /**
0071      * Array of validation failure message codes
0072      *
0073      * @var array
0074      * @deprecated Since 1.5.0
0075      */
0076     protected $_errors = array();
0077 
0078     /**
0079      * Translation object
0080      * @var Zend_Translate
0081      */
0082     protected $_translator;
0083 
0084     /**
0085      * Default translation object for all validate objects
0086      * @var Zend_Translate
0087      */
0088     protected static $_defaultTranslator;
0089 
0090     /**
0091      * Is translation disabled?
0092      * @var Boolean
0093      */
0094     protected $_translatorDisabled = false;
0095 
0096     /**
0097      * Limits the maximum returned length of a error message
0098      *
0099      * @var Integer
0100      */
0101     protected static $_messageLength = -1;
0102 
0103     /**
0104      * Returns array of validation failure messages
0105      *
0106      * @return array
0107      */
0108     public function getMessages()
0109     {
0110         return $this->_messages;
0111     }
0112 
0113     /**
0114      * Returns an array of the names of variables that are used in constructing validation failure messages
0115      *
0116      * @return array
0117      */
0118     public function getMessageVariables()
0119     {
0120         return array_keys($this->_messageVariables);
0121     }
0122 
0123     /**
0124      * Returns the message templates from the validator
0125      *
0126      * @return array
0127      */
0128     public function getMessageTemplates()
0129     {
0130         return $this->_messageTemplates;
0131     }
0132 
0133     /**
0134      * Sets the validation failure message template for a particular key
0135      *
0136      * @param  string $messageString
0137      * @param  string $messageKey     OPTIONAL
0138      * @return Zend_Validate_Abstract Provides a fluent interface
0139      * @throws Zend_Validate_Exception
0140      */
0141     public function setMessage($messageString, $messageKey = null)
0142     {
0143         if ($messageKey === null) {
0144             $keys = array_keys($this->_messageTemplates);
0145             foreach($keys as $key) {
0146                 $this->setMessage($messageString, $key);
0147             }
0148             return $this;
0149         }
0150 
0151         if (!isset($this->_messageTemplates[$messageKey])) {
0152             // require_once 'Zend/Validate/Exception.php';
0153             throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
0154         }
0155 
0156         $this->_messageTemplates[$messageKey] = $messageString;
0157         return $this;
0158     }
0159 
0160     /**
0161      * Sets validation failure message templates given as an array, where the array keys are the message keys,
0162      * and the array values are the message template strings.
0163      *
0164      * @param  array $messages
0165      * @return Zend_Validate_Abstract
0166      */
0167     public function setMessages(array $messages)
0168     {
0169         foreach ($messages as $key => $message) {
0170             $this->setMessage($message, $key);
0171         }
0172         return $this;
0173     }
0174 
0175     /**
0176      * Magic function returns the value of the requested property, if and only if it is the value or a
0177      * message variable.
0178      *
0179      * @param  string $property
0180      * @return mixed
0181      * @throws Zend_Validate_Exception
0182      */
0183     public function __get($property)
0184     {
0185         if ($property == 'value') {
0186             return $this->_value;
0187         }
0188         if (array_key_exists($property, $this->_messageVariables)) {
0189             return $this->{$this->_messageVariables[$property]};
0190         }
0191         /**
0192          * @see Zend_Validate_Exception
0193          */
0194         // require_once 'Zend/Validate/Exception.php';
0195         throw new Zend_Validate_Exception("No property exists by the name '$property'");
0196     }
0197 
0198     /**
0199      * Constructs and returns a validation failure message with the given message key and value.
0200      *
0201      * Returns null if and only if $messageKey does not correspond to an existing template.
0202      *
0203      * If a translator is available and a translation exists for $messageKey,
0204      * the translation will be used.
0205      *
0206      * @param  string $messageKey
0207      * @param  string $value
0208      * @return string
0209      */
0210     protected function _createMessage($messageKey, $value)
0211     {
0212         if (!isset($this->_messageTemplates[$messageKey])) {
0213             return null;
0214         }
0215 
0216         $message = $this->_messageTemplates[$messageKey];
0217 
0218         if (null !== ($translator = $this->getTranslator())) {
0219             if ($translator->isTranslated($messageKey)) {
0220                 $message = $translator->translate($messageKey);
0221             } else {
0222                 $message = $translator->translate($message);
0223             }
0224         }
0225 
0226         if (is_object($value)) {
0227             if (!in_array('__toString', get_class_methods($value))) {
0228                 $value = get_class($value) . ' object';
0229             } else {
0230                 $value = $value->__toString();
0231             }
0232         } elseif (is_array($value)) {
0233             $value = $this->_implodeRecursive($value);
0234         } else {
0235             $value = implode((array) $value);
0236         }
0237 
0238         if ($this->getObscureValue()) {
0239             $value = str_repeat('*', strlen($value));
0240         }
0241 
0242         $message = str_replace('%value%', $value, $message);
0243         foreach ($this->_messageVariables as $ident => $property) {
0244             $message = str_replace(
0245                 "%$ident%",
0246                 implode(' ', (array) $this->$property),
0247                 $message
0248             );
0249         }
0250 
0251         $length = self::getMessageLength();
0252         if (($length > -1) && (strlen($message) > $length)) {
0253             $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
0254         }
0255 
0256         return $message;
0257     }
0258 
0259     /**
0260      * Joins elements of a multidimensional array
0261      *
0262      * @param array $pieces
0263      * @return string
0264      */
0265     protected function _implodeRecursive(array $pieces)
0266     {
0267         $values = array();
0268         foreach ($pieces as $item) {
0269             if (is_array($item)) {
0270                 $values[] = $this->_implodeRecursive($item);
0271             } else {
0272                 $values[] = $item;
0273             }
0274         }
0275 
0276         return implode(', ', $values);
0277     }
0278 
0279     /**
0280      * @param  string $messageKey
0281      * @param  string $value      OPTIONAL
0282      * @return void
0283      */
0284     protected function _error($messageKey, $value = null)
0285     {
0286         if ($messageKey === null) {
0287             $keys = array_keys($this->_messageTemplates);
0288             $messageKey = current($keys);
0289         }
0290         if ($value === null) {
0291             $value = $this->_value;
0292         }
0293         $this->_errors[]              = $messageKey;
0294         $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
0295     }
0296 
0297     /**
0298      * Sets the value to be validated and clears the messages and errors arrays
0299      *
0300      * @param  mixed $value
0301      * @return void
0302      */
0303     protected function _setValue($value)
0304     {
0305         $this->_value    = $value;
0306         $this->_messages = array();
0307         $this->_errors   = array();
0308     }
0309 
0310     /**
0311      * Returns array of validation failure message codes
0312      *
0313      * @return array
0314      * @deprecated Since 1.5.0
0315      */
0316     public function getErrors()
0317     {
0318         return $this->_errors;
0319     }
0320 
0321     /**
0322      * Set flag indicating whether or not value should be obfuscated in messages
0323      *
0324      * @param  bool $flag
0325      * @return Zend_Validate_Abstract
0326      */
0327     public function setObscureValue($flag)
0328     {
0329         $this->_obscureValue = (bool) $flag;
0330         return $this;
0331     }
0332 
0333     /**
0334      * Retrieve flag indicating whether or not value should be obfuscated in
0335      * messages
0336      *
0337      * @return bool
0338      */
0339     public function getObscureValue()
0340     {
0341         return $this->_obscureValue;
0342     }
0343 
0344     /**
0345      * Set translation object
0346      *
0347      * @param  Zend_Translate|Zend_Translate_Adapter|null $translator
0348      * @throws Zend_Validate_Exception
0349      * @return Zend_Validate_Abstract
0350      */
0351     public function setTranslator($translator = null)
0352     {
0353         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
0354             $this->_translator = $translator;
0355         } elseif ($translator instanceof Zend_Translate) {
0356             $this->_translator = $translator->getAdapter();
0357         } else {
0358             // require_once 'Zend/Validate/Exception.php';
0359             throw new Zend_Validate_Exception('Invalid translator specified');
0360         }
0361         return $this;
0362     }
0363 
0364     /**
0365      * Return translation object
0366      *
0367      * @return Zend_Translate_Adapter|null
0368      */
0369     public function getTranslator()
0370     {
0371         if ($this->translatorIsDisabled()) {
0372             return null;
0373         }
0374 
0375         if (null === $this->_translator) {
0376             return self::getDefaultTranslator();
0377         }
0378 
0379         return $this->_translator;
0380     }
0381 
0382     /**
0383      * Does this validator have its own specific translator?
0384      *
0385      * @return bool
0386      */
0387     public function hasTranslator()
0388     {
0389         return (bool)$this->_translator;
0390     }
0391 
0392     /**
0393      * Set default translation object for all validate objects
0394      *
0395      * @param  Zend_Translate|Zend_Translate_Adapter|null $translator
0396      * @throws Zend_Validate_Exception
0397      */
0398     public static function setDefaultTranslator($translator = null)
0399     {
0400         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
0401             self::$_defaultTranslator = $translator;
0402         } elseif ($translator instanceof Zend_Translate) {
0403             self::$_defaultTranslator = $translator->getAdapter();
0404         } else {
0405             // require_once 'Zend/Validate/Exception.php';
0406             throw new Zend_Validate_Exception('Invalid translator specified');
0407         }
0408     }
0409 
0410     /**
0411      * Get default translation object for all validate objects
0412      *
0413      * @return Zend_Translate_Adapter|null
0414      */
0415     public static function getDefaultTranslator()
0416     {
0417         if (null === self::$_defaultTranslator) {
0418             // require_once 'Zend/Registry.php';
0419             if (Zend_Registry::isRegistered('Zend_Translate')) {
0420                 $translator = Zend_Registry::get('Zend_Translate');
0421                 if ($translator instanceof Zend_Translate_Adapter) {
0422                     return $translator;
0423                 } elseif ($translator instanceof Zend_Translate) {
0424                     return $translator->getAdapter();
0425                 }
0426             }
0427         }
0428 
0429         return self::$_defaultTranslator;
0430     }
0431 
0432     /**
0433      * Is there a default translation object set?
0434      *
0435      * @return boolean
0436      */
0437     public static function hasDefaultTranslator()
0438     {
0439         return (bool)self::$_defaultTranslator;
0440     }
0441 
0442     /**
0443      * Indicate whether or not translation should be disabled
0444      *
0445      * @param  bool $flag
0446      * @return Zend_Validate_Abstract
0447      */
0448     public function setDisableTranslator($flag)
0449     {
0450         $this->_translatorDisabled = (bool) $flag;
0451         return $this;
0452     }
0453 
0454     /**
0455      * Is translation disabled?
0456      *
0457      * @return bool
0458      */
0459     public function translatorIsDisabled()
0460     {
0461         return $this->_translatorDisabled;
0462     }
0463 
0464     /**
0465      * Returns the maximum allowed message length
0466      *
0467      * @return integer
0468      */
0469     public static function getMessageLength()
0470     {
0471         return self::$_messageLength;
0472     }
0473 
0474     /**
0475      * Sets the maximum allowed message length
0476      *
0477      * @param integer $length
0478      */
0479     public static function setMessageLength($length = -1)
0480     {
0481         self::$_messageLength = $length;
0482     }
0483 }