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