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 / RequiredUnlessRule.php
kirki / libraries / framework / Validation / Rules Last commit date
ArrayRule.php 6 days ago BooleanRule.php 6 days ago ConditionalRule.php 6 days ago DateRule.php 6 days ago ExistsRule.php 6 days ago FileRule.php 6 days ago InRule.php 6 days ago NotInRule.php 6 days ago NullableRule.php 6 days ago NumericRule.php 6 days ago ObjectRule.php 6 days ago ProhibitedIfRule.php 6 days ago ProhibitedRule.php 6 days ago ProhibitedUnlessRule.php 6 days ago RequiredIfRule.php 6 days ago RequiredRule.php 6 days ago RequiredUnlessRule.php 6 days ago SameAsRule.php 6 days ago SometimesRule.php 6 days ago StringRule.php 6 days ago UniqueRule.php 6 days ago
RequiredUnlessRule.php
93 lines
1 <?php
2
3 /**
4 * Required rule class.
5 *
6 * @package Framework
7 * @subpackage Validation
8 * @since 1.0.0
9 */
10 namespace Kirki\Framework\Validation\Rules;
11
12 use Closure;
13 use Kirki\Framework\Supports\Arr;
14 use Kirki\Framework\Validation\ValidationRule;
15 use InvalidArgumentException;
16 use function Kirki\Framework\deep_get;
17 use function Kirki\Framework\Polyfill\array_first;
18 \defined('ABSPATH') || exit;
19 class RequiredUnlessRule extends ValidationRule
20 {
21 /**
22 * The rule name.
23 *
24 * @var string
25 *
26 * @since 1.0.0
27 */
28 protected string $rule = 'required_unless';
29 /**
30 * Whether the rule is an implicit rule.
31 *
32 * @var bool
33 *
34 * @since 1.0.0
35 */
36 public bool $is_implicit = \true;
37 /**
38 * Validate the rule.
39 *
40 * @return bool
41 *
42 * @since 1.0.0
43 */
44 public function validate() : bool
45 {
46 $callback = $this->get_callback();
47 if ($callback()) {
48 return \true;
49 }
50 if (static::is_nullish($this->value)) {
51 return $this->fails($this->default_messages['default']);
52 }
53 return \true;
54 }
55 /**
56 * Get the callback.
57 *
58 * @return Closure
59 *
60 * @since 1.0.0
61 */
62 protected function get_callback()
63 {
64 $arguments = Arr::wrap($this->args);
65 $other = array_first($arguments);
66 $value = \array_slice($arguments, 1);
67 if (!$other instanceof Closure) {
68 $other = function () use($other, $value) {
69 if (empty($value)) {
70 throw new InvalidArgumentException('The second argument must be a non-empty string.');
71 }
72 $data = deep_get($this->data, (string) $other);
73 if (\count($value) > 1) {
74 return \in_array($data, $value);
75 }
76 return $data == array_first($value);
77 };
78 }
79 return $other;
80 }
81 /**
82 * Get the error message.
83 *
84 * @return string
85 *
86 * @since 1.0.0
87 */
88 public function messages()
89 {
90 return $this->process_messages($this->messages);
91 }
92 }
93