PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / trunk
Kirki – Freeform Page Builder, Website Builder & Customizer vtrunk
6.3.0 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 / ArrayRule.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
ArrayRule.php
139 lines
1 <?php
2
3 /**
4 * Array 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 an array.
16 *
17 * @method $this min(int $min)
18 * @method $this max(int $max)
19 * @method $this size(int $size)
20 * @method $this exactly(int $exactly)
21 * @method $this contains(mixed $contains)
22 */
23 class ArrayRule extends ValidationRule
24 {
25 /**
26 * The rule name.
27 *
28 * @var string
29 *
30 * @since 1.0.0
31 */
32 protected string $rule = 'array';
33 /**
34 * The supported constraints.
35 *
36 * @var array
37 *
38 * @since 1.0.0
39 */
40 protected array $constraints = ['min', 'max', 'size', 'exactly', 'contains'];
41 /**
42 * Validate the rule.
43 *
44 * @return bool
45 *
46 * @since 1.0.0
47 */
48 public function validate() : bool
49 {
50 if (!\is_iterable($this->value)) {
51 return $this->fails($this->default_messages['default']);
52 }
53 return $this->validate_constraints(function ($passed, $constraint) {
54 if (!$passed) {
55 $this->fails($this->default_messages[$constraint], [$constraint => $this->get($constraint)]);
56 }
57 });
58 }
59 /**
60 * Validate the min constraint.
61 *
62 * @return bool
63 *
64 * @since 1.0.0
65 */
66 protected function validate_min() : bool
67 {
68 return \count($this->value) >= (int) $this->get('min');
69 }
70 /**
71 * Validate the max constraint.
72 *
73 * @return bool
74 *
75 * @since 1.0.0
76 */
77 protected function validate_max() : bool
78 {
79 return \count($this->value) <= (int) $this->get('max');
80 }
81 /**
82 * Validate the size constraint.
83 *
84 * @return bool
85 *
86 * @since 1.0.0
87 */
88 protected function validate_size() : bool
89 {
90 return $this->has_exact_count('size');
91 }
92 /**
93 * Validate the exactly constraint.
94 *
95 * @return bool
96 *
97 * @since 1.0.0
98 */
99 protected function validate_exactly() : bool
100 {
101 return $this->has_exact_count('exactly');
102 }
103 /**
104 * Check whether the value item count matches the given constraint value.
105 *
106 * @param string $constraint The constraint holding the expected count.
107 *
108 * @return bool
109 *
110 * @since 1.0.0
111 */
112 protected function has_exact_count(string $constraint) : bool
113 {
114 return \count($this->value) === (int) $this->get($constraint);
115 }
116 /**
117 * Validate the contains constraint.
118 *
119 * @return bool
120 *
121 * @since 1.0.0
122 */
123 protected function validate_contains() : bool
124 {
125 return \in_array($this->get('contains'), $this->value);
126 }
127 /**
128 * Get the error message.
129 *
130 * @return string
131 *
132 * @since 1.0.0
133 */
134 public function messages()
135 {
136 return $this->process_messages($this->messages);
137 }
138 }
139