File indexing completed on 2024-12-22 05:37:12

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 /** @see Zend_Validate_Abstract */
0023 // require_once 'Zend/Validate/Abstract.php';
0024 
0025 /**
0026  * @category   Zend
0027  * @package    Zend_Validate
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_Validate_Identical extends Zend_Validate_Abstract
0032 {
0033     /**
0034      * Error codes
0035      * @const string
0036      */
0037     const NOT_SAME      = 'notSame';
0038     const MISSING_TOKEN = 'missingToken';
0039 
0040     /**
0041      * Error messages
0042      * @var array
0043      */
0044     protected $_messageTemplates = array(
0045         self::NOT_SAME      => "The two given tokens do not match",
0046         self::MISSING_TOKEN => 'No token was provided to match against',
0047     );
0048 
0049     /**
0050      * @var array
0051      */
0052     protected $_messageVariables = array(
0053         'token' => '_tokenString'
0054     );
0055 
0056     /**
0057      * Original token against which to validate
0058      * @var string
0059      */
0060     protected $_tokenString;
0061     protected $_token;
0062     protected $_strict = true;
0063 
0064     /**
0065      * Sets validator options
0066      *
0067      * @param mixed $token
0068      */
0069     public function __construct($token = null)
0070     {
0071         if ($token instanceof Zend_Config) {
0072             $token = $token->toArray();
0073         }
0074 
0075         if (is_array($token) && array_key_exists('token', $token)) {
0076             if (array_key_exists('strict', $token)) {
0077                 $this->setStrict($token['strict']);
0078             }
0079 
0080             $this->setToken($token['token']);
0081         } else if (null !== $token) {
0082             $this->setToken($token);
0083         }
0084     }
0085 
0086     /**
0087      * Retrieve token
0088      *
0089      * @return string
0090      */
0091     public function getToken()
0092     {
0093         return $this->_token;
0094     }
0095 
0096     /**
0097      * Set token against which to compare
0098      *
0099      * @param  mixed $token
0100      * @return Zend_Validate_Identical
0101      */
0102     public function setToken($token)
0103     {
0104         $this->_tokenString = $token;
0105         $this->_token       = $token;
0106         return $this;
0107     }
0108 
0109     /**
0110      * Returns the strict parameter
0111      *
0112      * @return boolean
0113      */
0114     public function getStrict()
0115     {
0116         return $this->_strict;
0117     }
0118 
0119     /**
0120      * Sets the strict parameter
0121      *
0122      * @param Zend_Validate_Identical
0123      * @return $this
0124      */
0125     public function setStrict($strict)
0126     {
0127         $this->_strict = (boolean) $strict;
0128         return $this;
0129     }
0130 
0131     /**
0132      * Defined by Zend_Validate_Interface
0133      *
0134      * Returns true if and only if a token has been set and the provided value
0135      * matches that token.
0136      *
0137      * @param  mixed $value
0138      * @param  array $context
0139      * @return boolean
0140      */
0141     public function isValid($value, $context = null)
0142     {
0143         $this->_setValue($value);
0144 
0145         if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
0146             $token = $context[$this->getToken()];
0147         } else {
0148             $token = $this->getToken();
0149         }
0150 
0151         if ($token === null) {
0152             $this->_error(self::MISSING_TOKEN);
0153             return false;
0154         }
0155 
0156         $strict = $this->getStrict();
0157         if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
0158             $this->_error(self::NOT_SAME);
0159             return false;
0160         }
0161 
0162         return true;
0163     }
0164 }