Identical.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /** 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' => '_token'
  50. );
  51. /**
  52. * Original token against which to validate
  53. * @var string
  54. */
  55. protected $_token;
  56. /**
  57. * Sets validator options
  58. *
  59. * @param string $token
  60. * @return void
  61. */
  62. public function __construct($token = null)
  63. {
  64. if (null !== $token) {
  65. $this->setToken($token);
  66. }
  67. }
  68. /**
  69. * Set token against which to compare
  70. *
  71. * @param string $token
  72. * @return Zend_Validate_Identical
  73. */
  74. public function setToken($token)
  75. {
  76. $this->_token = (string) $token;
  77. return $this;
  78. }
  79. /**
  80. * Retrieve token
  81. *
  82. * @return string
  83. */
  84. public function getToken()
  85. {
  86. return $this->_token;
  87. }
  88. /**
  89. * Defined by Zend_Validate_Interface
  90. *
  91. * Returns true if and only if a token has been set and the provided value
  92. * matches that token.
  93. *
  94. * @param mixed $value
  95. * @return boolean
  96. */
  97. public function isValid($value)
  98. {
  99. $this->_setValue($value);
  100. $token = $this->getToken();
  101. if ($token === null) {
  102. $this->_error(self::MISSING_TOKEN);
  103. return false;
  104. }
  105. if ($value !== $token) {
  106. $this->_error(self::NOT_SAME);
  107. return false;
  108. }
  109. return true;
  110. }
  111. }