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
ObjectRule.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Object 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 object or an associative array. |
| 16 | */ |
| 17 | class ObjectRule extends ValidationRule |
| 18 | { |
| 19 | /** |
| 20 | * The rule name. |
| 21 | * |
| 22 | * @var string |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | protected string $rule = 'object'; |
| 27 | /** |
| 28 | * Validate the rule. |
| 29 | * |
| 30 | * @return bool |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | public function validate() : bool |
| 35 | { |
| 36 | if (\is_object($this->value) || $this->is_associative_array($this->value)) { |
| 37 | return \true; |
| 38 | } |
| 39 | return $this->fails($this->default_messages['default']); |
| 40 | } |
| 41 | /** |
| 42 | * Check whether the value is a non-empty associative array. |
| 43 | * |
| 44 | * @param mixed $value The value to check. |
| 45 | * |
| 46 | * @return bool |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | protected function is_associative_array($value) |
| 51 | { |
| 52 | if (!\is_array($value) || $value === []) { |
| 53 | return \false; |
| 54 | } |
| 55 | return \array_keys($value) !== \range(0, \count($value) - 1); |
| 56 | } |
| 57 | /** |
| 58 | * Get the error messages. |
| 59 | * |
| 60 | * @return array |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | public function messages() |
| 65 | { |
| 66 | return $this->process_messages($this->messages); |
| 67 | } |
| 68 | } |
| 69 |