PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/Between.php

http://github.com/zendframework/zf2
PHP | 181 lines | 88 code | 20 blank | 73 comment | 13 complexity | 3cd00c27ea9414221be18b3f41edbfdc MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2015 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 Between extends AbstractValidator
  13. {
  14. const NOT_BETWEEN = 'notBetween';
  15. const NOT_BETWEEN_STRICT = 'notBetweenStrict';
  16. /**
  17. * Validation failure message template definitions
  18. *
  19. * @var array
  20. */
  21. protected $messageTemplates = array(
  22. self::NOT_BETWEEN => "The input is not between '%min%' and '%max%', inclusively",
  23. self::NOT_BETWEEN_STRICT => "The input is not strictly between '%min%' and '%max%'"
  24. );
  25. /**
  26. * Additional variables available for validation failure messages
  27. *
  28. * @var array
  29. */
  30. protected $messageVariables = array(
  31. 'min' => array('options' => 'min'),
  32. 'max' => array('options' => 'max'),
  33. );
  34. /**
  35. * Options for the between validator
  36. *
  37. * @var array
  38. */
  39. protected $options = array(
  40. 'inclusive' => true, // Whether to do inclusive comparisons, allowing equivalence to min and/or max
  41. 'min' => 0,
  42. 'max' => PHP_INT_MAX,
  43. );
  44. /**
  45. * Sets validator options
  46. * Accepts the following option keys:
  47. * 'min' => scalar, minimum border
  48. * 'max' => scalar, maximum border
  49. * 'inclusive' => boolean, inclusive border values
  50. *
  51. * @param array|Traversable $options
  52. *
  53. * @throws Exception\InvalidArgumentException
  54. */
  55. public function __construct($options = null)
  56. {
  57. if ($options instanceof Traversable) {
  58. $options = ArrayUtils::iteratorToArray($options);
  59. }
  60. if (!is_array($options)) {
  61. $options = func_get_args();
  62. $temp['min'] = array_shift($options);
  63. if (!empty($options)) {
  64. $temp['max'] = array_shift($options);
  65. }
  66. if (!empty($options)) {
  67. $temp['inclusive'] = array_shift($options);
  68. }
  69. $options = $temp;
  70. }
  71. if (count($options) !== 2
  72. && (!array_key_exists('min', $options) || !array_key_exists('max', $options))
  73. ) {
  74. throw new Exception\InvalidArgumentException("Missing option. 'min' and 'max' have to be given");
  75. }
  76. parent::__construct($options);
  77. }
  78. /**
  79. * Returns the min option
  80. *
  81. * @return mixed
  82. */
  83. public function getMin()
  84. {
  85. return $this->options['min'];
  86. }
  87. /**
  88. * Sets the min option
  89. *
  90. * @param mixed $min
  91. * @return Between Provides a fluent interface
  92. */
  93. public function setMin($min)
  94. {
  95. $this->options['min'] = $min;
  96. return $this;
  97. }
  98. /**
  99. * Returns the max option
  100. *
  101. * @return mixed
  102. */
  103. public function getMax()
  104. {
  105. return $this->options['max'];
  106. }
  107. /**
  108. * Sets the max option
  109. *
  110. * @param mixed $max
  111. * @return Between Provides a fluent interface
  112. */
  113. public function setMax($max)
  114. {
  115. $this->options['max'] = $max;
  116. return $this;
  117. }
  118. /**
  119. * Returns the inclusive option
  120. *
  121. * @return bool
  122. */
  123. public function getInclusive()
  124. {
  125. return $this->options['inclusive'];
  126. }
  127. /**
  128. * Sets the inclusive option
  129. *
  130. * @param bool $inclusive
  131. * @return Between Provides a fluent interface
  132. */
  133. public function setInclusive($inclusive)
  134. {
  135. $this->options['inclusive'] = $inclusive;
  136. return $this;
  137. }
  138. /**
  139. * Returns true if and only if $value is between min and max options, inclusively
  140. * if inclusive option is true.
  141. *
  142. * @param mixed $value
  143. * @return bool
  144. */
  145. public function isValid($value)
  146. {
  147. $this->setValue($value);
  148. if ($this->getInclusive()) {
  149. if ($this->getMin() > $value || $value > $this->getMax()) {
  150. $this->error(self::NOT_BETWEEN);
  151. return false;
  152. }
  153. } else {
  154. if ($this->getMin() >= $value || $value >= $this->getMax()) {
  155. $this->error(self::NOT_BETWEEN_STRICT);
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. }