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
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 |