Identical.php 2.7 KB

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