2
0

Identical.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2009 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. /** @see Zend_Validate_Abstract */
  22. require_once 'Zend/Validate/Abstract.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Validate
  26. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Validate_Identical extends Zend_Validate_Abstract
  30. {
  31. /**
  32. * Error codes
  33. * @const string
  34. */
  35. const NOT_SAME = 'notSame';
  36. const MISSING_TOKEN = 'missingToken';
  37. /**
  38. * Error messages
  39. * @var array
  40. */
  41. protected $_messageTemplates = array(
  42. self::NOT_SAME => "The token '%token%' does not match the given token '%value%'",
  43. self::MISSING_TOKEN => 'No token was provided to match against',
  44. );
  45. /**
  46. * @var array
  47. */
  48. protected $_messageVariables = array(
  49. 'token' => '_tokenString'
  50. );
  51. /**
  52. * Original token against which to validate
  53. * @var string
  54. */
  55. protected $_tokenString;
  56. protected $_token;
  57. /**
  58. * Sets validator options
  59. *
  60. * @param mixed $token
  61. * @return void
  62. */
  63. public function __construct($token = null)
  64. {
  65. if ($token instanceof Zend_Config) {
  66. $token = $token->toArray();
  67. }
  68. if (is_array($token) && (count($token) == 1) && array_key_exists('token', $token)) {
  69. $token = $token['token'];
  70. }
  71. if (null !== $token) {
  72. $this->setToken($token);
  73. }
  74. }
  75. /**
  76. * Set token against which to compare
  77. *
  78. * @param mixed $token
  79. * @return Zend_Validate_Identical
  80. */
  81. public function setToken($token)
  82. {
  83. $this->_tokenString = (string) $token;
  84. $this->_token = $token;
  85. return $this;
  86. }
  87. /**
  88. * Retrieve token
  89. *
  90. * @return string
  91. */
  92. public function getToken()
  93. {
  94. return $this->_token;
  95. }
  96. /**
  97. * Defined by Zend_Validate_Interface
  98. *
  99. * Returns true if and only if a token has been set and the provided value
  100. * matches that token.
  101. *
  102. * @param mixed $value
  103. * @return boolean
  104. */
  105. public function isValid($value)
  106. {
  107. $this->_setValue((string) $value);
  108. $token = $this->getToken();
  109. if ($token === null) {
  110. $this->_error(self::MISSING_TOKEN);
  111. return false;
  112. }
  113. if ($value !== $token) {
  114. $this->_error(self::NOT_SAME);
  115. return false;
  116. }
  117. return true;
  118. }
  119. }