PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
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 / StringRule.php
kirki / libraries / framework / Validation / Rules Last commit date
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
StringRule.php
330 lines
1 <?php
2
3 /**
4 * String 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 use function Kirki\Framework\Polyfill\str_ends_with;
14 use function Kirki\Framework\Polyfill\str_starts_with;
15 \defined('ABSPATH') || exit;
16 /**
17 * Validates that the given value is a string.
18 *
19 * @method StringRule min(int $min)
20 * @method StringRule max(int $max)
21 * @method StringRule length(int $length)
22 * @method StringRule size(int $size)
23 * @method StringRule email()
24 * @method StringRule url()
25 * @method StringRule ip()
26 * @method StringRule alpha()
27 * @method StringRule alpha_num()
28 * @method StringRule alpha_dash()
29 * @method StringRule between(array $values)
30 * @method StringRule starts_with(string $value)
31 * @method StringRule ends_with(string $value)
32 * @method StringRule doesnt_start_with(string $value)
33 * @method StringRule doesnt_end_with(string $value)
34 * @method StringRule exactly(int $value)
35 * @method StringRule regex(string $pattern)
36 */
37 class StringRule extends ValidationRule
38 {
39 /**
40 * The rule name.
41 *
42 * @var string
43 *
44 * @since 1.0.0
45 */
46 protected string $rule = 'string';
47 /**
48 * The supported constraints.
49 *
50 * @var array
51 *
52 * @since 1.0.0
53 */
54 protected array $constraints = ['min', 'max', 'length', 'size', 'email', 'url', 'regex', 'ip', 'alpha', 'alpha_num', 'alpha_dash', 'between', 'starts_with', 'ends_with', 'doesnt_start_with', 'doesnt_end_with', 'exactly'];
55 /**
56 * @inheritDoc
57 */
58 public function validate() : bool
59 {
60 if (!\is_string($this->value)) {
61 return $this->fails($this->default_messages['default']);
62 }
63 return $this->validate_constraints(function ($passed, $constraint) {
64 if (!$passed) {
65 $this->fails($this->default_messages[$constraint], $this->constraint_placeholders($constraint));
66 }
67 });
68 }
69 /**
70 * Build the message placeholders for a failed constraint.
71 *
72 * @param string $constraint The failed constraint name.
73 *
74 * @return array
75 *
76 * @since 1.0.0
77 */
78 protected function constraint_placeholders(string $constraint)
79 {
80 if ($constraint === 'between') {
81 [$min, $max] = $this->get('between');
82 return ['min' => (string) $min, 'max' => (string) $max];
83 }
84 return [$constraint => (string) $this->get($constraint)];
85 }
86 /**
87 * Validate the min constraint.
88 *
89 * @param string $value The value to validate.
90 *
91 * @return bool
92 *
93 * @since 1.0.0
94 */
95 protected function validate_min($value)
96 {
97 return \mb_strlen($value) >= (int) $this->get('min');
98 }
99 /**
100 * Validate the max constraint.
101 *
102 * @param string $value The value to validate.
103 *
104 * @return bool
105 *
106 * @since 1.0.0
107 */
108 protected function validate_max($value)
109 {
110 return \mb_strlen($value) <= (int) $this->get('max');
111 }
112 /**
113 * Validate the length constraint.
114 *
115 * @param string $value The value to validate.
116 *
117 * @return bool
118 *
119 * @since 1.0.0
120 */
121 protected function validate_length($value)
122 {
123 return $this->has_exact_length($value, 'length');
124 }
125 /**
126 * Validate the size constraint.
127 *
128 * @param string $value The value to validate.
129 *
130 * @return bool
131 *
132 * @since 1.0.0
133 */
134 protected function validate_size($value)
135 {
136 return $this->has_exact_length($value, 'size');
137 }
138 /**
139 * Validate the exactly constraint.
140 *
141 * @param string $value The value to validate.
142 *
143 * @return bool
144 *
145 * @since 1.0.0
146 */
147 protected function validate_exactly($value)
148 {
149 return $this->has_exact_length($value, 'exactly');
150 }
151 /**
152 * Check whether the value length matches the given constraint value.
153 *
154 * @param string $value The value to check.
155 * @param string $constraint The constraint holding the expected length.
156 *
157 * @return bool
158 *
159 * @since 1.0.0
160 */
161 protected function has_exact_length($value, string $constraint)
162 {
163 return \mb_strlen($value) === (int) $this->get($constraint);
164 }
165 /**
166 * Validate the starts_with constraint.
167 *
168 * @param string $value The value to validate.
169 *
170 * @return bool
171 *
172 * @since 1.0.0
173 */
174 protected function validate_starts_with($value)
175 {
176 return str_starts_with($value, $this->get('starts_with'));
177 }
178 /**
179 * Validate the ends_with constraint.
180 *
181 * @param string $value The value to validate.
182 *
183 * @return bool
184 *
185 * @since 1.0.0
186 */
187 protected function validate_ends_with($value)
188 {
189 return str_ends_with($value, $this->get('ends_with'));
190 }
191 /**
192 * Validate the doesnt_start_with constraint.
193 *
194 * @param string $value The value to validate.
195 *
196 * @return bool
197 *
198 * @since 1.0.0
199 */
200 protected function validate_doesnt_start_with($value)
201 {
202 return !str_starts_with($value, $this->get('doesnt_start_with'));
203 }
204 /**
205 * Validate the doesnt_end_with constraint.
206 *
207 * @param string $value The value to validate.
208 *
209 * @return bool
210 *
211 * @since 1.0.0
212 */
213 protected function validate_doesnt_end_with($value)
214 {
215 return !str_ends_with($value, $this->get('doesnt_end_with'));
216 }
217 /**
218 * Validate the between constraint.
219 *
220 * @param string $value The value to validate.
221 *
222 * @return bool
223 *
224 * @since 1.0.0
225 */
226 protected function validate_between($value)
227 {
228 [$min, $max] = $this->get('between');
229 return \mb_strlen($value) >= (int) $min && \mb_strlen($value) <= (int) $max;
230 }
231 /**
232 * Validate the regex constraint.
233 *
234 * @param string $value The value to validate.
235 *
236 * @return bool
237 *
238 * @since 1.0.0
239 */
240 protected function validate_regex($value)
241 {
242 return \preg_match($this->get('regex'), $value) === 1;
243 }
244 /**
245 * Validate the alpha constraint.
246 *
247 * @param string $value The value to validate.
248 *
249 * @return bool
250 *
251 * @since 1.0.0
252 */
253 protected function validate_alpha($value)
254 {
255 return \preg_match('/^[a-zA-Z]+$/', $value) === 1;
256 }
257 /**
258 * Validate the alpha_num constraint.
259 *
260 * @param string $value The value to validate.
261 *
262 * @return bool
263 *
264 * @since 1.0.0
265 */
266 protected function validate_alpha_num($value)
267 {
268 return \preg_match('/^[a-zA-Z0-9]+$/', $value) === 1;
269 }
270 /**
271 * Validate the alpha_dash constraint.
272 *
273 * @param string $value The value to validate.
274 *
275 * @return bool
276 *
277 * @since 1.0.0
278 */
279 protected function validate_alpha_dash($value)
280 {
281 return \preg_match('/^[a-zA-Z0-9_-]+$/', $value) === 1;
282 }
283 /**
284 * Validate the email constraint.
285 *
286 * @param string $value The value to validate.
287 *
288 * @return bool
289 *
290 * @since 1.0.0
291 */
292 protected function validate_email($value)
293 {
294 return \filter_var($value, \FILTER_VALIDATE_EMAIL) !== \false;
295 }
296 /**
297 * Validate the url constraint.
298 *
299 * @param string $value The value to validate.
300 *
301 * @return bool
302 *
303 * @since 1.0.0
304 */
305 protected function validate_url($value)
306 {
307 return \filter_var($value, \FILTER_VALIDATE_URL) !== \false;
308 }
309 /**
310 * Validate the ip constraint.
311 *
312 * @param string $value The value to validate.
313 *
314 * @return bool
315 *
316 * @since 1.0.0
317 */
318 protected function validate_ip($value)
319 {
320 return \filter_var($value, \FILTER_VALIDATE_IP) !== \false;
321 }
322 /**
323 * @inheritDoc
324 */
325 public function messages()
326 {
327 return $this->process_messages($this->messages);
328 }
329 }
330