PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Validator/Identical.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 193 lines | 98 code | 24 blank | 71 comment | 19 complexity | 9b6abb3979954c6dd2cf6368b744385b MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. class Identical extends AbstractValidator
  13. {
  14. /**
  15. * Error codes
  16. * @const string
  17. */
  18. const NOT_SAME = 'notSame';
  19. const MISSING_TOKEN = 'missingToken';
  20. /**
  21. * Error messages
  22. * @var array
  23. */
  24. protected $messageTemplates = array(
  25. self::NOT_SAME => "The two given tokens do not match",
  26. self::MISSING_TOKEN => 'No token was provided to match against',
  27. );
  28. /**
  29. * @var array
  30. */
  31. protected $messageVariables = array(
  32. 'token' => 'tokenString'
  33. );
  34. /**
  35. * Original token against which to validate
  36. * @var string
  37. */
  38. protected $tokenString;
  39. protected $token;
  40. protected $strict = true;
  41. protected $literal = false;
  42. /**
  43. * Sets validator options
  44. *
  45. * @param mixed $token
  46. */
  47. public function __construct($token = null)
  48. {
  49. if ($token instanceof Traversable) {
  50. $token = ArrayUtils::iteratorToArray($token);
  51. }
  52. if (is_array($token) && array_key_exists('token', $token)) {
  53. if (array_key_exists('strict', $token)) {
  54. $this->setStrict($token['strict']);
  55. }
  56. if (array_key_exists('literal', $token)) {
  57. $this->setLiteral($token['literal']);
  58. }
  59. $this->setToken($token['token']);
  60. } elseif (null !== $token) {
  61. $this->setToken($token);
  62. }
  63. parent::__construct(is_array($token) ? $token : null);
  64. }
  65. /**
  66. * Retrieve token
  67. *
  68. * @return mixed
  69. */
  70. public function getToken()
  71. {
  72. return $this->token;
  73. }
  74. /**
  75. * Set token against which to compare
  76. *
  77. * @param mixed $token
  78. * @return Identical
  79. */
  80. public function setToken($token)
  81. {
  82. $this->tokenString = (is_array($token) ? var_export($token, true) : (string) $token);
  83. $this->token = $token;
  84. return $this;
  85. }
  86. /**
  87. * Returns the strict parameter
  88. *
  89. * @return bool
  90. */
  91. public function getStrict()
  92. {
  93. return $this->strict;
  94. }
  95. /**
  96. * Sets the strict parameter
  97. *
  98. * @param bool $strict
  99. * @return Identical
  100. */
  101. public function setStrict($strict)
  102. {
  103. $this->strict = (bool) $strict;
  104. return $this;
  105. }
  106. /**
  107. * Returns the literal parameter
  108. *
  109. * @return bool
  110. */
  111. public function getLiteral()
  112. {
  113. return $this->literal;
  114. }
  115. /**
  116. * Sets the literal parameter
  117. *
  118. * @param bool $literal
  119. * @return Identical
  120. */
  121. public function setLiteral($literal)
  122. {
  123. $this->literal = (bool) $literal;
  124. return $this;
  125. }
  126. /**
  127. * Returns true if and only if a token has been set and the provided value
  128. * matches that token.
  129. *
  130. * @param mixed $value
  131. * @param array $context
  132. * @return bool
  133. * @throws Exception\RuntimeException if the token doesn't exist in the context array
  134. */
  135. public function isValid($value, array $context = null)
  136. {
  137. $this->setValue($value);
  138. $token = $this->getToken();
  139. if (!$this->getLiteral() && $context !== null) {
  140. if (is_array($token)) {
  141. while (is_array($token)) {
  142. $key = key($token);
  143. if (!isset($context[$key])) {
  144. break;
  145. }
  146. $context = $context[$key];
  147. $token = $token[$key];
  148. }
  149. }
  150. // if $token is an array it means the above loop didn't went all the way down to the leaf,
  151. // so the $token structure doesn't match the $context structure
  152. if (is_array($token) || !isset($context[$token])) {
  153. $token = $this->getToken();
  154. } else {
  155. $token = $context[$token];
  156. }
  157. }
  158. if ($token === null) {
  159. $this->error(self::MISSING_TOKEN);
  160. return false;
  161. }
  162. $strict = $this->getStrict();
  163. if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
  164. $this->error(self::NOT_SAME);
  165. return false;
  166. }
  167. return true;
  168. }
  169. }