Identical.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if (array_key_exists('token', $token)) {
  68. $token = $token['token'];
  69. } else {
  70. require_once 'Zend/Validate/Exception.php';
  71. throw new Zend_Validate_Exception("Missing option 'token'");
  72. }
  73. }
  74. if (null !== $token) {
  75. $this->setToken($token);
  76. }
  77. }
  78. /**
  79. * Set token against which to compare
  80. *
  81. * @param mixed $token
  82. * @return Zend_Validate_Identical
  83. */
  84. public function setToken($token)
  85. {
  86. $this->_tokenString = (string) $token;
  87. $this->_token = $token;
  88. return $this;
  89. }
  90. /**
  91. * Retrieve token
  92. *
  93. * @return string
  94. */
  95. public function getToken()
  96. {
  97. return $this->_token;
  98. }
  99. /**
  100. * Defined by Zend_Validate_Interface
  101. *
  102. * Returns true if and only if a token has been set and the provided value
  103. * matches that token.
  104. *
  105. * @param mixed $value
  106. * @return boolean
  107. */
  108. public function isValid($value)
  109. {
  110. $this->_setValue((string) $value);
  111. $token = $this->getToken();
  112. if ($token === null) {
  113. $this->_error(self::MISSING_TOKEN);
  114. return false;
  115. }
  116. if ($value !== $token) {
  117. $this->_error(self::NOT_SAME);
  118. return false;
  119. }
  120. return true;
  121. }
  122. }