Identical.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-2015 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-2015 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 two given tokens do not match",
  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. protected $_strict = true;
  58. /**
  59. * Sets validator options
  60. *
  61. * @param mixed $token
  62. */
  63. public function __construct($token = null)
  64. {
  65. if ($token instanceof Zend_Config) {
  66. $token = $token->toArray();
  67. }
  68. if (is_array($token) && array_key_exists('token', $token)) {
  69. if (array_key_exists('strict', $token)) {
  70. $this->setStrict($token['strict']);
  71. }
  72. $this->setToken($token['token']);
  73. } else if (null !== $token) {
  74. $this->setToken($token);
  75. }
  76. }
  77. /**
  78. * Retrieve token
  79. *
  80. * @return string
  81. */
  82. public function getToken()
  83. {
  84. return $this->_token;
  85. }
  86. /**
  87. * Set token against which to compare
  88. *
  89. * @param mixed $token
  90. * @return Zend_Validate_Identical
  91. */
  92. public function setToken($token)
  93. {
  94. $this->_tokenString = $token;
  95. $this->_token = $token;
  96. return $this;
  97. }
  98. /**
  99. * Returns the strict parameter
  100. *
  101. * @return boolean
  102. */
  103. public function getStrict()
  104. {
  105. return $this->_strict;
  106. }
  107. /**
  108. * Sets the strict parameter
  109. *
  110. * @param Zend_Validate_Identical
  111. * @return $this
  112. */
  113. public function setStrict($strict)
  114. {
  115. $this->_strict = (boolean) $strict;
  116. return $this;
  117. }
  118. /**
  119. * Defined by Zend_Validate_Interface
  120. *
  121. * Returns true if and only if a token has been set and the provided value
  122. * matches that token.
  123. *
  124. * @param mixed $value
  125. * @param array $context
  126. * @return boolean
  127. */
  128. public function isValid($value, $context = null)
  129. {
  130. $this->_setValue($value);
  131. if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
  132. $token = $context[$this->getToken()];
  133. } else {
  134. $token = $this->getToken();
  135. }
  136. if ($token === null) {
  137. $this->_error(self::MISSING_TOKEN);
  138. return false;
  139. }
  140. $strict = $this->getStrict();
  141. if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
  142. $this->_error(self::NOT_SAME);
  143. return false;
  144. }
  145. return true;
  146. }
  147. }