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 / ConditionalRule.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
ConditionalRule.php
140 lines
1 <?php
2
3 /**
4 * Conditional 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\Supports\Arr;
13 use Kirki\Framework\Validation\ValidationRule;
14 \defined('ABSPATH') || exit;
15 class ConditionalRule extends ValidationRule
16 {
17 /**
18 * The rule name.
19 *
20 * @var string
21 *
22 * @since 1.0.0
23 */
24 protected string $rule = 'conditional';
25 /**
26 * The boolean condition for which the rules should be applied.
27 *
28 * @var bool|callable
29 *
30 * @since 1.0.0
31 */
32 protected $condition;
33 /**
34 * The rules to apply when the condition is true.
35 *
36 * @var array
37 *
38 * @since 1.0.0
39 */
40 protected $rules;
41 /**
42 * The rules to apply when the condition is false.
43 *
44 * @var array
45 *
46 * @since 1.0.0
47 */
48 protected $default_rules;
49 /**
50 * Create a new conditional rules instance.
51 *
52 * @param bool|callable $condition The boolean condition for which the rules should be applied.
53 * @param array $rules The rules to apply when the condition is true.
54 * @param array $default_rules The rules to apply when the condition is false.
55 *
56 * @return void
57 *
58 * @since 1.0.0
59 */
60 public function __construct($condition, $rules, $default_rules = [])
61 {
62 $this->condition = $condition;
63 $this->rules = $rules;
64 $this->default_rules = $default_rules;
65 }
66 /**
67 * Validate the rule.
68 *
69 * @return bool
70 *
71 * @since 1.0.0
72 */
73 public function validate() : bool
74 {
75 return \true;
76 }
77 /**
78 * Check if the condition passes.
79 *
80 * @param array $data The data to check.
81 *
82 * @return bool
83 *
84 * @since 1.0.0
85 */
86 public function passes(array $data)
87 {
88 return \is_callable($this->condition) ? \call_user_func($this->condition, $data) : $this->condition;
89 }
90 /**
91 * Make the rules into an array.
92 *
93 * @param string|array $rules The rules to make into an array.
94 *
95 * @return array
96 *
97 * @since 1.0.0
98 */
99 protected function make_array($rules)
100 {
101 if (\is_string($rules)) {
102 return \explode('|', $rules);
103 }
104 return $rules;
105 }
106 /**
107 * Get the rules.
108 *
109 * @return array
110 *
111 * @since 1.0.0
112 */
113 public function rules()
114 {
115 return $this->make_array($this->rules);
116 }
117 /**
118 * Get the default rules.
119 *
120 * @return array
121 *
122 * @since 1.0.0
123 */
124 public function default_rules()
125 {
126 return $this->make_array($this->default_rules);
127 }
128 /**
129 * Get the messages.
130 *
131 * @return array
132 *
133 * @since 1.0.0
134 */
135 public function messages()
136 {
137 return [];
138 }
139 }
140