"Fossies" - the Fresh Open Source Software Archive

Member "icingaweb2-2.11.4/library/vendor/Zend/Validate/Abstract.php" (26 Jan 2023, 12655 Bytes) of package /linux/www/icingaweb2-2.11.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * Zend Framework
    4  *
    5  * LICENSE
    6  *
    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.
   14  *
   15  * @category   Zend
   16  * @package    Zend_Validate
   17  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   19  * @version    $Id$
   20  */
   21 
   22 /**
   23  * @see Zend_Validate_Interface
   24  */
   25 
   26 /**
   27  * @category   Zend
   28  * @package    Zend_Validate
   29  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   31  */
   32 abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
   33 {
   34     /**
   35      * The value to be validated
   36      *
   37      * @var mixed
   38      */
   39     protected $_value;
   40 
   41     /**
   42      * Additional variables available for validation failure messages
   43      *
   44      * @var array
   45      */
   46     protected $_messageVariables = array();
   47 
   48     /**
   49      * Validation failure message template definitions
   50      *
   51      * @var array
   52      */
   53     protected $_messageTemplates = array();
   54 
   55     /**
   56      * Array of validation failure messages
   57      *
   58      * @var array
   59      */
   60     protected $_messages = array();
   61 
   62     /**
   63      * Flag indidcating whether or not value should be obfuscated in error
   64      * messages
   65      * @var bool
   66      */
   67     protected $_obscureValue = false;
   68 
   69     /**
   70      * Array of validation failure message codes
   71      *
   72      * @var array
   73      * @deprecated Since 1.5.0
   74      */
   75     protected $_errors = array();
   76 
   77     /**
   78      * Translation object
   79      * @var Zend_Translate
   80      */
   81     protected $_translator;
   82 
   83     /**
   84      * Default translation object for all validate objects
   85      * @var Zend_Translate
   86      */
   87     protected static $_defaultTranslator;
   88 
   89     /**
   90      * Is translation disabled?
   91      * @var Boolean
   92      */
   93     protected $_translatorDisabled = false;
   94 
   95     /**
   96      * Limits the maximum returned length of a error message
   97      *
   98      * @var Integer
   99      */
  100     protected static $_messageLength = -1;
  101 
  102     /**
  103      * Returns array of validation failure messages
  104      *
  105      * @return array
  106      */
  107     public function getMessages()
  108     {
  109         return $this->_messages;
  110     }
  111 
  112     /**
  113      * Returns an array of the names of variables that are used in constructing validation failure messages
  114      *
  115      * @return array
  116      */
  117     public function getMessageVariables()
  118     {
  119         return array_keys($this->_messageVariables);
  120     }
  121 
  122     /**
  123      * Returns the message templates from the validator
  124      *
  125      * @return array
  126      */
  127     public function getMessageTemplates()
  128     {
  129         return $this->_messageTemplates;
  130     }
  131 
  132     /**
  133      * Sets the validation failure message template for a particular key
  134      *
  135      * @param  string $messageString
  136      * @param  string $messageKey     OPTIONAL
  137      * @return Zend_Validate_Abstract Provides a fluent interface
  138      * @throws Zend_Validate_Exception
  139      */
  140     public function setMessage($messageString, $messageKey = null)
  141     {
  142         if ($messageKey === null) {
  143             $keys = array_keys($this->_messageTemplates);
  144             foreach($keys as $key) {
  145                 $this->setMessage($messageString, $key);
  146             }
  147             return $this;
  148         }
  149 
  150         if (!isset($this->_messageTemplates[$messageKey])) {
  151             throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
  152         }
  153 
  154         $this->_messageTemplates[$messageKey] = $messageString;
  155         return $this;
  156     }
  157 
  158     /**
  159      * Sets validation failure message templates given as an array, where the array keys are the message keys,
  160      * and the array values are the message template strings.
  161      *
  162      * @param  array $messages
  163      * @return Zend_Validate_Abstract
  164      */
  165     public function setMessages(array $messages)
  166     {
  167         foreach ($messages as $key => $message) {
  168             $this->setMessage($message, $key);
  169         }
  170         return $this;
  171     }
  172 
  173     /**
  174      * Magic function returns the value of the requested property, if and only if it is the value or a
  175      * message variable.
  176      *
  177      * @param  string $property
  178      * @return mixed
  179      * @throws Zend_Validate_Exception
  180      */
  181     public function __get($property)
  182     {
  183         if ($property == 'value') {
  184             return $this->_value;
  185         }
  186         if (array_key_exists($property, $this->_messageVariables)) {
  187             return $this->{$this->_messageVariables[$property]};
  188         }
  189         /**
  190          * @see Zend_Validate_Exception
  191          */
  192         throw new Zend_Validate_Exception("No property exists by the name '$property'");
  193     }
  194 
  195     /**
  196      * Constructs and returns a validation failure message with the given message key and value.
  197      *
  198      * Returns null if and only if $messageKey does not correspond to an existing template.
  199      *
  200      * If a translator is available and a translation exists for $messageKey,
  201      * the translation will be used.
  202      *
  203      * @param  string $messageKey
  204      * @param  string $value
  205      * @return string
  206      */
  207     protected function _createMessage($messageKey, $value)
  208     {
  209         if (!isset($this->_messageTemplates[$messageKey])) {
  210             return null;
  211         }
  212 
  213         $message = $this->_messageTemplates[$messageKey];
  214 
  215         if (null !== ($translator = $this->getTranslator())) {
  216             if ($translator->isTranslated($messageKey)) {
  217                 $message = $translator->translate($messageKey);
  218             } else {
  219                 $message = $translator->translate($message);
  220             }
  221         }
  222 
  223         if (is_object($value)) {
  224             if (!in_array('__toString', get_class_methods($value))) {
  225                 $value = get_class($value) . ' object';
  226             } else {
  227                 $value = $value->__toString();
  228             }
  229         } elseif (is_array($value)) {
  230             $value = $this->_implodeRecursive($value);
  231         } else {
  232             $value = implode((array) $value);
  233         }
  234 
  235         if ($this->getObscureValue()) {
  236             $value = str_repeat('*', strlen($value));
  237         }
  238 
  239         $message = str_replace('%value%', $value, $message);
  240         foreach ($this->_messageVariables as $ident => $property) {
  241             $message = str_replace(
  242                 "%$ident%",
  243                 implode(' ', (array) $this->$property),
  244                 $message
  245             );
  246         }
  247 
  248         $length = self::getMessageLength();
  249         if (($length > -1) && (strlen($message) > $length)) {
  250             $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
  251         }
  252 
  253         return $message;
  254     }
  255 
  256     /**
  257      * Joins elements of a multidimensional array
  258      *
  259      * @param array $pieces
  260      * @return string
  261      */
  262     protected function _implodeRecursive(array $pieces)
  263     {
  264         $values = array();
  265         foreach ($pieces as $item) {
  266             if (is_array($item)) {
  267                 $values[] = $this->_implodeRecursive($item);
  268             } else {
  269                 $values[] = $item;
  270             }
  271         }
  272 
  273         return implode(', ', $values);
  274     }
  275 
  276     /**
  277      * @param  string $messageKey
  278      * @param  string $value      OPTIONAL
  279      * @return void
  280      */
  281     protected function _error($messageKey, $value = null)
  282     {
  283         if ($messageKey === null) {
  284             $keys = array_keys($this->_messageTemplates);
  285             $messageKey = current($keys);
  286         }
  287         if ($value === null) {
  288             $value = $this->_value;
  289         }
  290         $this->_errors[]              = $messageKey;
  291         $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
  292     }
  293 
  294     /**
  295      * Sets the value to be validated and clears the messages and errors arrays
  296      *
  297      * @param  mixed $value
  298      * @return void
  299      */
  300     protected function _setValue($value)
  301     {
  302         $this->_value    = $value;
  303         $this->_messages = array();
  304         $this->_errors   = array();
  305     }
  306 
  307     /**
  308      * Returns array of validation failure message codes
  309      *
  310      * @return array
  311      * @deprecated Since 1.5.0
  312      */
  313     public function getErrors()
  314     {
  315         return $this->_errors;
  316     }
  317 
  318     /**
  319      * Set flag indicating whether or not value should be obfuscated in messages
  320      *
  321      * @param  bool $flag
  322      * @return Zend_Validate_Abstract
  323      */
  324     public function setObscureValue($flag)
  325     {
  326         $this->_obscureValue = (bool) $flag;
  327         return $this;
  328     }
  329 
  330     /**
  331      * Retrieve flag indicating whether or not value should be obfuscated in
  332      * messages
  333      *
  334      * @return bool
  335      */
  336     public function getObscureValue()
  337     {
  338         return $this->_obscureValue;
  339     }
  340 
  341     /**
  342      * Set translation object
  343      *
  344      * @param  Zend_Translate|Zend_Translate_Adapter|null $translator
  345      * @throws Zend_Validate_Exception
  346      * @return Zend_Validate_Abstract
  347      */
  348     public function setTranslator($translator = null)
  349     {
  350         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  351             $this->_translator = $translator;
  352         } elseif ($translator instanceof Zend_Translate) {
  353             $this->_translator = $translator->getAdapter();
  354         } else {
  355             throw new Zend_Validate_Exception('Invalid translator specified');
  356         }
  357         return $this;
  358     }
  359 
  360     /**
  361      * Return translation object
  362      *
  363      * @return Zend_Translate_Adapter|null
  364      */
  365     public function getTranslator()
  366     {
  367         if ($this->translatorIsDisabled()) {
  368             return null;
  369         }
  370 
  371         if (null === $this->_translator) {
  372             return self::getDefaultTranslator();
  373         }
  374 
  375         return $this->_translator;
  376     }
  377 
  378     /**
  379      * Does this validator have its own specific translator?
  380      *
  381      * @return bool
  382      */
  383     public function hasTranslator()
  384     {
  385         return (bool)$this->_translator;
  386     }
  387 
  388     /**
  389      * Set default translation object for all validate objects
  390      *
  391      * @param  Zend_Translate|Zend_Translate_Adapter|null $translator
  392      * @throws Zend_Validate_Exception
  393      */
  394     public static function setDefaultTranslator($translator = null)
  395     {
  396         if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  397             self::$_defaultTranslator = $translator;
  398         } elseif ($translator instanceof Zend_Translate) {
  399             self::$_defaultTranslator = $translator->getAdapter();
  400         } else {
  401             throw new Zend_Validate_Exception('Invalid translator specified');
  402         }
  403     }
  404 
  405     /**
  406      * Get default translation object for all validate objects
  407      *
  408      * @return Zend_Translate_Adapter|null
  409      */
  410     public static function getDefaultTranslator()
  411     {
  412         if (null === self::$_defaultTranslator) {
  413             if (Zend_Registry::isRegistered('Zend_Translate')) {
  414                 $translator = Zend_Registry::get('Zend_Translate');
  415                 if ($translator instanceof Zend_Translate_Adapter) {
  416                     return $translator;
  417                 } elseif ($translator instanceof Zend_Translate) {
  418                     return $translator->getAdapter();
  419                 }
  420             }
  421         }
  422 
  423         return self::$_defaultTranslator;
  424     }
  425 
  426     /**
  427      * Is there a default translation object set?
  428      *
  429      * @return boolean
  430      */
  431     public static function hasDefaultTranslator()
  432     {
  433         return (bool)self::$_defaultTranslator;
  434     }
  435 
  436     /**
  437      * Indicate whether or not translation should be disabled
  438      *
  439      * @param  bool $flag
  440      * @return Zend_Validate_Abstract
  441      */
  442     public function setDisableTranslator($flag)
  443     {
  444         $this->_translatorDisabled = (bool) $flag;
  445         return $this;
  446     }
  447 
  448     /**
  449      * Is translation disabled?
  450      *
  451      * @return bool
  452      */
  453     public function translatorIsDisabled()
  454     {
  455         return $this->_translatorDisabled;
  456     }
  457 
  458     /**
  459      * Returns the maximum allowed message length
  460      *
  461      * @return integer
  462      */
  463     public static function getMessageLength()
  464     {
  465         return self::$_messageLength;
  466     }
  467 
  468     /**
  469      * Sets the maximum allowed message length
  470      *
  471      * @param integer $length
  472      */
  473     public static function setMessageLength($length = -1)
  474     {
  475         self::$_messageLength = $length;
  476     }
  477 }