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.
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** @see Zend_Validate_Abstract */
23 require_once 'Zend/Validate/Abstract.php';
27 * @package Zend_Validate
28 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
29 * @license http://framework.zend.com/license/new-bsd New BSD License
31 class Zend_Validate_Identical
extends Zend_Validate_Abstract
37 const NOT_SAME
= 'notSame';
38 const MISSING_TOKEN
= 'missingToken';
44 protected $_messageTemplates = array(
45 self
::NOT_SAME
=> "The two given tokens do not match",
46 self
::MISSING_TOKEN
=> 'No token was provided to match against',
52 protected $_messageVariables = array(
53 'token' => '_tokenString'
57 * Original token against which to validate
60 protected $_tokenString;
62 protected $_strict = true;
65 * Sets validator options
70 public function __construct($token = null)
72 if ($token instanceof Zend_Config
) {
73 $token = $token->toArray();
76 if (is_array($token) && array_key_exists('token', $token)) {
77 if (array_key_exists('strict', $token)) {
78 $this->setStrict($token['strict']);
81 $this->setToken($token['token']);
82 } else if (null !== $token) {
83 $this->setToken($token);
92 public function getToken()
98 * Set token against which to compare
100 * @param mixed $token
101 * @return Zend_Validate_Identical
103 public function setToken($token)
105 $this->_tokenString
= (string) $token;
106 $this->_token
= $token;
111 * Returns the strict parameter
115 public function getStrict()
117 return $this->_strict
;
121 * Sets the strict parameter
123 * @param Zend_Validate_Identical
125 public function setStrict($strict)
127 $this->_strict
= (boolean
) $strict;
132 * Defined by Zend_Validate_Interface
134 * Returns true if and only if a token has been set and the provided value
135 * matches that token.
137 * @param mixed $value
138 * @param array $context
141 public function isValid($value, $context = null)
143 $this->_setValue((string) $value);
145 if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
146 $token = $context[$this->getToken()];
148 $token = $this->getToken();
151 if ($token === null) {
152 $this->_error(self
::MISSING_TOKEN
);
156 $strict = $this->getStrict();
157 if (($strict && ($value !== $token)) ||
(!$strict && ($value != $token))) {
158 $this->_error(self
::NOT_SAME
);