Numeric.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Traits\Conditionable;
  5. use Stringable;
  6. class Numeric implements Stringable
  7. {
  8. use Conditionable;
  9. /**
  10. * The constraints for the number rule.
  11. */
  12. protected array $constraints = ['numeric'];
  13. /**
  14. * The field under validation must have a size between the given min and max (inclusive).
  15. *
  16. * @param int|float $min
  17. * @param int|float $max
  18. * @return $this
  19. */
  20. public function between(int|float $min, int|float $max): Numeric
  21. {
  22. return $this->addRule('between:'.$min.','.$max);
  23. }
  24. /**
  25. * The field under validation must contain the specified number of decimal places.
  26. *
  27. * @param int $min
  28. * @param int|null $max
  29. * @return $this
  30. */
  31. public function decimal(int $min, ?int $max = null): Numeric
  32. {
  33. $rule = 'decimal:'.$min;
  34. if ($max !== null) {
  35. $rule .= ','.$max;
  36. }
  37. return $this->addRule($rule);
  38. }
  39. /**
  40. * The field under validation must have a different value than field.
  41. *
  42. * @param string $field
  43. * @return $this
  44. */
  45. public function different(string $field): Numeric
  46. {
  47. return $this->addRule('different:'.$field);
  48. }
  49. /**
  50. * The integer under validation must have an exact number of digits.
  51. *
  52. * @param int $length
  53. * @return $this
  54. */
  55. public function digits(int $length): Numeric
  56. {
  57. return $this->integer()->addRule('digits:'.$length);
  58. }
  59. /**
  60. * The integer under validation must between the given min and max number of digits.
  61. *
  62. * @param int $min
  63. * @param int $max
  64. * @return $this
  65. */
  66. public function digitsBetween(int $min, int $max): Numeric
  67. {
  68. return $this->integer()->addRule('digits_between:'.$min.','.$max);
  69. }
  70. /**
  71. * The field under validation must be greater than the given field or value.
  72. *
  73. * @param string $field
  74. * @return $this
  75. */
  76. public function greaterThan(string $field): Numeric
  77. {
  78. return $this->addRule('gt:'.$field);
  79. }
  80. /**
  81. * The field under validation must be greater than or equal to the given field or value.
  82. *
  83. * @param string $field
  84. * @return $this
  85. */
  86. public function greaterThanOrEqualTo(string $field): Numeric
  87. {
  88. return $this->addRule('gte:'.$field);
  89. }
  90. /**
  91. * The field under validation must be an integer.
  92. *
  93. * @return $this
  94. */
  95. public function integer(bool $strict = false): Numeric
  96. {
  97. return $this->addRule($strict ? 'integer:strict' : 'integer');
  98. }
  99. /**
  100. * The field under validation must be less than the given field.
  101. *
  102. * @param string $field
  103. * @return $this
  104. */
  105. public function lessThan(string $field): Numeric
  106. {
  107. return $this->addRule('lt:'.$field);
  108. }
  109. /**
  110. * The field under validation must be less than or equal to the given field.
  111. *
  112. * @param string $field
  113. * @return $this
  114. */
  115. public function lessThanOrEqualTo(string $field): Numeric
  116. {
  117. return $this->addRule('lte:'.$field);
  118. }
  119. /**
  120. * The field under validation must be less than or equal to a maximum value.
  121. *
  122. * @param int|float $value
  123. * @return $this
  124. */
  125. public function max(int|float $value): Numeric
  126. {
  127. return $this->addRule('max:'.$value);
  128. }
  129. /**
  130. * The integer under validation must have a maximum number of digits.
  131. *
  132. * @param int $value
  133. * @return $this
  134. */
  135. public function maxDigits(int $value): Numeric
  136. {
  137. return $this->addRule('max_digits:'.$value);
  138. }
  139. /**
  140. * The field under validation must have a minimum value.
  141. *
  142. * @param int|float $value
  143. * @return $this
  144. */
  145. public function min(int|float $value): Numeric
  146. {
  147. return $this->addRule('min:'.$value);
  148. }
  149. /**
  150. * The integer under validation must have a minimum number of digits.
  151. *
  152. * @param int $value
  153. * @return $this
  154. */
  155. public function minDigits(int $value): Numeric
  156. {
  157. return $this->addRule('min_digits:'.$value);
  158. }
  159. /**
  160. * The field under validation must be a multiple of the given value.
  161. *
  162. * @param int|float $value
  163. * @return $this
  164. */
  165. public function multipleOf(int|float $value): Numeric
  166. {
  167. return $this->addRule('multiple_of:'.$value);
  168. }
  169. /**
  170. * The given field must match the field under validation.
  171. *
  172. * @param string $field
  173. * @return $this
  174. */
  175. public function same(string $field): Numeric
  176. {
  177. return $this->addRule('same:'.$field);
  178. }
  179. /**
  180. * The field under validation must match the given value.
  181. *
  182. * @param int $value
  183. * @return $this
  184. */
  185. public function exactly(int $value): Numeric
  186. {
  187. return $this->integer()->addRule('size:'.$value);
  188. }
  189. /**
  190. * Convert the rule to a validation string.
  191. */
  192. public function __toString(): string
  193. {
  194. return implode('|', array_unique($this->constraints));
  195. }
  196. /**
  197. * Add custom rules to the validation rules array.
  198. */
  199. protected function addRule(array|string $rules): Numeric
  200. {
  201. $this->constraints = array_merge($this->constraints, Arr::wrap($rules));
  202. return $this;
  203. }
  204. }