Between.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Between extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value is not between the min and max, inclusively
  35. */
  36. const NOT_BETWEEN = 'notBetween';
  37. /**
  38. * Validation failure message key for when the value is not strictly between the min and max
  39. */
  40. const NOT_BETWEEN_STRICT = 'notBetweenStrict';
  41. /**
  42. * Validation failure message template definitions
  43. *
  44. * @var array
  45. */
  46. protected $_messageTemplates = array(
  47. self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%', inclusively",
  48. self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'"
  49. );
  50. /**
  51. * Additional variables available for validation failure messages
  52. *
  53. * @var array
  54. */
  55. protected $_messageVariables = array(
  56. 'min' => '_min',
  57. 'max' => '_max'
  58. );
  59. /**
  60. * Minimum value
  61. *
  62. * @var mixed
  63. */
  64. protected $_min;
  65. /**
  66. * Maximum value
  67. *
  68. * @var mixed
  69. */
  70. protected $_max;
  71. /**
  72. * Whether to do inclusive comparisons, allowing equivalence to min and/or max
  73. *
  74. * If false, then strict comparisons are done, and the value may equal neither
  75. * the min nor max options
  76. *
  77. * @var boolean
  78. */
  79. protected $_inclusive;
  80. /**
  81. * Sets validator options
  82. * Accepts the following option keys:
  83. * 'min' => scalar, minimum border
  84. * 'max' => scalar, maximum border
  85. * 'inclusive' => boolean, inclusive border values
  86. *
  87. * @param array|Zend_Config $options
  88. * @return void
  89. */
  90. public function __construct($options)
  91. {
  92. if ($options instanceof Zend_Config) {
  93. $options = $options->toArray();
  94. } else if (!is_array($options)) {
  95. $count = func_num_args();
  96. if ($count > 1) {
  97. // @todo: Preperation for 2.0... needs to be cleared with the dev-team
  98. // trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
  99. }
  100. $options = func_get_args();
  101. $temp['min'] = array_shift($options);
  102. if (!empty($options)) {
  103. $temp['max'] = array_shift($options);
  104. }
  105. if (!empty($options)) {
  106. $temp['inclusive'] = array_shift($options);
  107. }
  108. $options = $temp;
  109. }
  110. if (!array_key_exists('min', $options) || !array_key_exists('max', $options)) {
  111. require_once 'Zend/Validate/Exception.php';
  112. throw new Zend_Validate_Exception("Missing option. 'min' and 'max' has to be given");
  113. }
  114. if (!array_key_exists('inclusive', $options)) {
  115. $options['inclusive'] = true;
  116. }
  117. $this->setMin($options['min'])
  118. ->setMax($options['max'])
  119. ->setInclusive($options['inclusive']);
  120. }
  121. /**
  122. * Returns the min option
  123. *
  124. * @return mixed
  125. */
  126. public function getMin()
  127. {
  128. return $this->_min;
  129. }
  130. /**
  131. * Sets the min option
  132. *
  133. * @param mixed $min
  134. * @return Zend_Validate_Between Provides a fluent interface
  135. */
  136. public function setMin($min)
  137. {
  138. $this->_min = $min;
  139. return $this;
  140. }
  141. /**
  142. * Returns the max option
  143. *
  144. * @return mixed
  145. */
  146. public function getMax()
  147. {
  148. return $this->_max;
  149. }
  150. /**
  151. * Sets the max option
  152. *
  153. * @param mixed $max
  154. * @return Zend_Validate_Between Provides a fluent interface
  155. */
  156. public function setMax($max)
  157. {
  158. $this->_max = $max;
  159. return $this;
  160. }
  161. /**
  162. * Returns the inclusive option
  163. *
  164. * @return boolean
  165. */
  166. public function getInclusive()
  167. {
  168. return $this->_inclusive;
  169. }
  170. /**
  171. * Sets the inclusive option
  172. *
  173. * @param boolean $inclusive
  174. * @return Zend_Validate_Between Provides a fluent interface
  175. */
  176. public function setInclusive($inclusive)
  177. {
  178. $this->_inclusive = $inclusive;
  179. return $this;
  180. }
  181. /**
  182. * Defined by Zend_Validate_Interface
  183. *
  184. * Returns true if and only if $value is between min and max options, inclusively
  185. * if inclusive option is true.
  186. *
  187. * @param mixed $value
  188. * @return boolean
  189. */
  190. public function isValid($value)
  191. {
  192. $this->_setValue($value);
  193. if ($this->_inclusive) {
  194. if ($this->_min > $value || $value > $this->_max) {
  195. $this->_error(self::NOT_BETWEEN);
  196. return false;
  197. }
  198. } else {
  199. if ($this->_min >= $value || $value >= $this->_max) {
  200. $this->_error(self::NOT_BETWEEN_STRICT);
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. }