PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / trunk
Kirki – Freeform Page Builder, Website Builder & Customizer vtrunk
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Validation / Rules / NumericRule.php
kirki / libraries / framework / Validation / Rules Last commit date
ArrayRule.php 1 month ago BooleanRule.php 1 month ago ConditionalRule.php 1 month ago DateRule.php 1 month ago ExistsRule.php 1 month ago FileRule.php 1 month ago InRule.php 1 month ago NotInRule.php 1 month ago NullableRule.php 1 month ago NumericRule.php 1 month ago ObjectRule.php 1 month ago ProhibitedIfRule.php 1 month ago ProhibitedRule.php 1 month ago ProhibitedUnlessRule.php 1 month ago RequiredIfRule.php 1 month ago RequiredRule.php 1 month ago RequiredUnlessRule.php 1 month ago SameAsRule.php 1 month ago SometimesRule.php 1 month ago StringRule.php 1 month ago UniqueRule.php 1 month 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