[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Validate / Identical.php
blobb78cb2b0c9eadfc14b8c8c71d006af3b6e97e94d
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /** @see Zend_Validate_Abstract */
23 require_once 'Zend/Validate/Abstract.php';
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
31 class Zend_Validate_Identical extends Zend_Validate_Abstract
33 /**
34 * Error codes
35 * @const string
37 const NOT_SAME = 'notSame';
38 const MISSING_TOKEN = 'missingToken';
40 /**
41 * Error messages
42 * @var array
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',
49 /**
50 * @var array
52 protected $_messageVariables = array(
53 'token' => '_tokenString'
56 /**
57 * Original token against which to validate
58 * @var string
60 protected $_tokenString;
61 protected $_token;
62 protected $_strict = true;
64 /**
65 * Sets validator options
67 * @param mixed $token
68 * @return void
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);
87 /**
88 * Retrieve token
90 * @return string
92 public function getToken()
94 return $this->_token;
97 /**
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;
107 return $this;
111 * Returns the strict parameter
113 * @return boolean
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;
128 return $this;
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
139 * @return boolean
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()];
147 } else {
148 $token = $this->getToken();
151 if ($token === null) {
152 $this->_error(self::MISSING_TOKEN);
153 return false;
156 $strict = $this->getStrict();
157 if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
158 $this->_error(self::NOT_SAME);
159 return false;
162 return true;