|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Identical.php 22075 2010-05-02 13:42:08Z thomas $ |
|
20 */ |
|
21 |
|
22 /** @see Zend_Validate_Abstract */ |
|
23 require_once 'Zend/Validate/Abstract.php'; |
|
24 |
|
25 /** |
|
26 * @category Zend |
|
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 |
|
30 */ |
|
31 class Zend_Validate_Identical extends Zend_Validate_Abstract |
|
32 { |
|
33 /** |
|
34 * Error codes |
|
35 * @const string |
|
36 */ |
|
37 const NOT_SAME = 'notSame'; |
|
38 const MISSING_TOKEN = 'missingToken'; |
|
39 |
|
40 /** |
|
41 * Error messages |
|
42 * @var array |
|
43 */ |
|
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', |
|
47 ); |
|
48 |
|
49 /** |
|
50 * @var array |
|
51 */ |
|
52 protected $_messageVariables = array( |
|
53 'token' => '_tokenString' |
|
54 ); |
|
55 |
|
56 /** |
|
57 * Original token against which to validate |
|
58 * @var string |
|
59 */ |
|
60 protected $_tokenString; |
|
61 protected $_token; |
|
62 protected $_strict = true; |
|
63 |
|
64 /** |
|
65 * Sets validator options |
|
66 * |
|
67 * @param mixed $token |
|
68 * @return void |
|
69 */ |
|
70 public function __construct($token = null) |
|
71 { |
|
72 if ($token instanceof Zend_Config) { |
|
73 $token = $token->toArray(); |
|
74 } |
|
75 |
|
76 if (is_array($token) && array_key_exists('token', $token)) { |
|
77 if (array_key_exists('strict', $token)) { |
|
78 $this->setStrict($token['strict']); |
|
79 } |
|
80 |
|
81 $this->setToken($token['token']); |
|
82 } else if (null !== $token) { |
|
83 $this->setToken($token); |
|
84 } |
|
85 } |
|
86 |
|
87 /** |
|
88 * Retrieve token |
|
89 * |
|
90 * @return string |
|
91 */ |
|
92 public function getToken() |
|
93 { |
|
94 return $this->_token; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Set token against which to compare |
|
99 * |
|
100 * @param mixed $token |
|
101 * @return Zend_Validate_Identical |
|
102 */ |
|
103 public function setToken($token) |
|
104 { |
|
105 $this->_tokenString = (string) $token; |
|
106 $this->_token = $token; |
|
107 return $this; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Returns the strict parameter |
|
112 * |
|
113 * @return boolean |
|
114 */ |
|
115 public function getStrict() |
|
116 { |
|
117 return $this->_strict; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Sets the strict parameter |
|
122 * |
|
123 * @param Zend_Validate_Identical |
|
124 */ |
|
125 public function setStrict($strict) |
|
126 { |
|
127 $this->_strict = (boolean) $strict; |
|
128 return $this; |
|
129 } |
|
130 |
|
131 /** |
|
132 * Defined by Zend_Validate_Interface |
|
133 * |
|
134 * Returns true if and only if a token has been set and the provided value |
|
135 * matches that token. |
|
136 * |
|
137 * @param mixed $value |
|
138 * @param array $context |
|
139 * @return boolean |
|
140 */ |
|
141 public function isValid($value, $context = null) |
|
142 { |
|
143 $this->_setValue((string) $value); |
|
144 |
|
145 if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) { |
|
146 $token = $context[$this->getToken()]; |
|
147 } else { |
|
148 $token = $this->getToken(); |
|
149 } |
|
150 |
|
151 if ($token === null) { |
|
152 $this->_error(self::MISSING_TOKEN); |
|
153 return false; |
|
154 } |
|
155 |
|
156 $strict = $this->getStrict(); |
|
157 if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) { |
|
158 $this->_error(self::NOT_SAME); |
|
159 return false; |
|
160 } |
|
161 |
|
162 return true; |
|
163 } |
|
164 } |