ArrayRule.php
1 week ago
BooleanRule.php
1 week ago
ConditionalRule.php
1 week ago
DateRule.php
1 week ago
ExistsRule.php
1 week ago
FileRule.php
1 week ago
InRule.php
1 week ago
NotInRule.php
1 week ago
NullableRule.php
1 week ago
NumericRule.php
1 week ago
ObjectRule.php
1 week ago
ProhibitedIfRule.php
1 week ago
ProhibitedRule.php
1 week ago
ProhibitedUnlessRule.php
1 week ago
RequiredIfRule.php
1 week ago
RequiredRule.php
1 week ago
RequiredUnlessRule.php
1 week ago
SameAsRule.php
1 week ago
SometimesRule.php
1 week ago
StringRule.php
1 week ago
UniqueRule.php
1 week ago
NumericRule.php
200 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Numeric rule class. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Validation |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Validation\Rules; |
| 11 | |
| 12 | use Kirki\Framework\Validation\ValidationRule; |
| 13 | \defined('ABSPATH') || exit; |
| 14 | /** |
| 15 | * Validates that the given value is numeric. |
| 16 | * |
| 17 | * @method $this integer() |
| 18 | * @method $this int() |
| 19 | * @method $this min(int $min) |
| 20 | * @method $this max(int $max) |
| 21 | */ |
| 22 | class NumericRule extends ValidationRule |
| 23 | { |
| 24 | /** |
| 25 | * The rule name. |
| 26 | * |
| 27 | * @var string |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | protected string $rule = 'numeric'; |
| 32 | /** |
| 33 | * The supported constraints. |
| 34 | * |
| 35 | * @var array |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | protected array $constraints = ['min', 'max', 'integer', 'int', 'float', 'decimal', 'gt', 'gte', 'lt', 'lte']; |
| 40 | /** |
| 41 | * Validate the rule. |
| 42 | * |
| 43 | * @return bool |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | public function validate() : bool |
| 48 | { |
| 49 | if (!\is_numeric($this->value)) { |
| 50 | return $this->fails($this->default_messages['default']); |
| 51 | } |
| 52 | return $this->validate_constraints(function ($passed, $constraint) { |
| 53 | if (!$passed) { |
| 54 | $this->fails($this->default_messages[$constraint], [$constraint => $this->get($constraint)]); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | /** |
| 59 | * Validate the min constraint. |
| 60 | * |
| 61 | * @param string $value The value to validate. |
| 62 | * |
| 63 | * @return bool |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | protected function validate_min($value) |
| 68 | { |
| 69 | return \floatval($value) >= \floatval($this->get('min')); |
| 70 | } |
| 71 | /** |
| 72 | * Validate the max constraint. |
| 73 | * |
| 74 | * @param string $value The value to validate. |
| 75 | * |
| 76 | * @return bool |
| 77 | * |
| 78 | * @since 1.0.0 |
| 79 | */ |
| 80 | protected function validate_max($value) |
| 81 | { |
| 82 | return \floatval($value) <= \floatval($this->get('max')); |
| 83 | } |
| 84 | /** |
| 85 | * Validate the integer constraint. |
| 86 | * |
| 87 | * @param string $value The value to validate. |
| 88 | * |
| 89 | * @return bool |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | protected function validate_integer($value) |
| 94 | { |
| 95 | return \filter_var($value, \FILTER_VALIDATE_INT) !== \false; |
| 96 | } |
| 97 | /** |
| 98 | * Validate the float constraint. |
| 99 | * |
| 100 | * @param string $value The value to validate. |
| 101 | * |
| 102 | * @return bool |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | */ |
| 106 | protected function validate_float($value) |
| 107 | { |
| 108 | return \filter_var($value, \FILTER_VALIDATE_FLOAT) !== \false; |
| 109 | } |
| 110 | /** |
| 111 | * Validate the decimal constraint. |
| 112 | * |
| 113 | * @param string $value The value to validate. |
| 114 | * |
| 115 | * @return bool |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | */ |
| 119 | protected function validate_decimal($value) |
| 120 | { |
| 121 | return \filter_var($value, \FILTER_VALIDATE_FLOAT) !== \false; |
| 122 | } |
| 123 | /** |
| 124 | * Validate the int constraint. |
| 125 | * |
| 126 | * @param string $value The value to validate. |
| 127 | * |
| 128 | * @return bool |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | */ |
| 132 | protected function validate_int($value) |
| 133 | { |
| 134 | return $this->validate_integer($value); |
| 135 | } |
| 136 | /** |
| 137 | * Validate the gt constraint. |
| 138 | * |
| 139 | * @param string $value The value to validate. |
| 140 | * |
| 141 | * @return bool |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | */ |
| 145 | protected function validate_gt($value) |
| 146 | { |
| 147 | return \floatval($value) > \floatval($this->get('gt')); |
| 148 | } |
| 149 | /** |
| 150 | * Validate the gte constraint. |
| 151 | * |
| 152 | * @param string $value The value to validate. |
| 153 | * |
| 154 | * @return bool |
| 155 | * |
| 156 | * @since 1.0.0 |
| 157 | */ |
| 158 | protected function validate_gte($value) |
| 159 | { |
| 160 | return \floatval($value) >= \floatval($this->get('gte')); |
| 161 | } |
| 162 | /** |
| 163 | * Validate the lt constraint. |
| 164 | * |
| 165 | * @param string $value The value to validate. |
| 166 | * |
| 167 | * @return bool |
| 168 | * |
| 169 | * @since 1.0.0 |
| 170 | */ |
| 171 | protected function validate_lt($value) |
| 172 | { |
| 173 | return \floatval($value) < \floatval($this->get('lt')); |
| 174 | } |
| 175 | /** |
| 176 | * Validate the lte constraint. |
| 177 | * |
| 178 | * @param string $value The value to validate. |
| 179 | * |
| 180 | * @return bool |
| 181 | * |
| 182 | * @since 1.0.0 |
| 183 | */ |
| 184 | protected function validate_lte($value) |
| 185 | { |
| 186 | return \floatval($value) <= \floatval($this->get('lte')); |
| 187 | } |
| 188 | /** |
| 189 | * Get the error message. |
| 190 | * |
| 191 | * @return string |
| 192 | * |
| 193 | * @since 1.0.0 |
| 194 | */ |
| 195 | public function messages() |
| 196 | { |
| 197 | return $this->process_messages($this->messages); |
| 198 | } |
| 199 | } |
| 200 |