GreaterThan.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ SG2M8qQT6r0JuvSfpNgUv+XajuZOuzzhFvtCTD6FL1s=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.01]
  11. */
  12. /**
  13. * checks if the variable is greater than a set value (with option to check if greater or equal)
  14. */
  15. namespace Cube\Validate;
  16. class GreaterThan extends AbstractValidate
  17. {
  18. const GREATER_EQUAL = 1;
  19. const GREATER = 2;
  20. const TOO_LONG = 3;
  21. protected $_messages = array(
  22. self::GREATER_EQUAL => "'%s' must be greater or equal to %value%.",
  23. self::GREATER => "'%s' must be greater than %value%.",
  24. );
  25. /**
  26. *
  27. * the minimum value allowed for the validator to check
  28. *
  29. * @var float
  30. */
  31. private $_minValue;
  32. /**
  33. *
  34. * if true, it will check for equal values as well
  35. *
  36. * @var bool
  37. */
  38. private $_equal = false;
  39. /**
  40. *
  41. * strict checking for the empty value
  42. *
  43. * @var bool
  44. */
  45. private $_strict = false;
  46. /**
  47. *
  48. * class constructor
  49. *
  50. * initialize the minimum value allowed and the equal check
  51. *
  52. * @param array $data data[0] -> min value;
  53. * data[1] -> accept equal values (default = false)
  54. */
  55. public function __construct(array $data = null)
  56. {
  57. $this->setMinValue($data[0]);
  58. if (isset($data[1])) {
  59. $this->setEqual($data[1]);
  60. }
  61. if (isset($data[2])) {
  62. $this->setStrict($data[2]);
  63. }
  64. }
  65. /**
  66. *
  67. * get the minimum value accepted by the validator
  68. *
  69. * @return float
  70. */
  71. public function getMinValue()
  72. {
  73. return $this->_minValue;
  74. }
  75. /**
  76. *
  77. * set the minimum value the validator will compare against
  78. *
  79. * @param mixed $minValue
  80. * @return \Cube\Validate\GreaterThan
  81. */
  82. public function setMinValue($minValue)
  83. {
  84. $this->_minValue = $minValue;
  85. return $this;
  86. }
  87. /**
  88. *
  89. * check if equal values are accepted
  90. *
  91. * @return bool
  92. */
  93. public function getEqual()
  94. {
  95. return $this->_equal;
  96. }
  97. /**
  98. *
  99. * set whether to validate equal values
  100. *
  101. * @param bool $equal
  102. * @return \Cube\Validate\GreaterThan
  103. */
  104. public function setEqual($equal = true)
  105. {
  106. $this->_equal = (bool) $equal;
  107. if ($this->_equal === true) {
  108. $this->setMessage($this->_messages[self::GREATER_EQUAL]);
  109. }
  110. else {
  111. $this->setMessage($this->_messages[self::GREATER]);
  112. }
  113. return $this;
  114. }
  115. /**
  116. *
  117. * get strict value
  118. *
  119. * @return bool
  120. */
  121. public function getStrict()
  122. {
  123. return $this->_strict;
  124. }
  125. /**
  126. *
  127. * set strict value
  128. *
  129. * @param bool $strict
  130. * @return \Cube\Validate\GreaterThan
  131. */
  132. public function setStrict($strict = true)
  133. {
  134. $this->_strict = (bool) $strict;
  135. return $this;
  136. }
  137. /**
  138. *
  139. * checks if the variable is greater than (or equal to) the set minimum value
  140. * also returns true if value is empty (or null if strict is enabled)
  141. *
  142. * @return bool return true if the validation is successful
  143. */
  144. public function isValid()
  145. {
  146. $this->setMessage(
  147. str_replace('%value%', $this->_minValue, $this->getMessage()));
  148. if (((empty($this->_value) || (doubleval($this->_value) == 0)) && $this->_strict === false)
  149. || (is_null($this->_value) && $this->_strict === true)) {
  150. return true;
  151. }
  152. else if ($this->_equal === true) {
  153. if ($this->_value < $this->_minValue) {
  154. return false;
  155. }
  156. return true;
  157. }
  158. else {
  159. if ($this->_value <= $this->_minValue) {
  160. return false;
  161. }
  162. return true;
  163. }
  164. }
  165. }