AfterRule.php
3 weeks ago
ArrayRule.php
3 weeks ago
BaseRule.php
3 weeks ago
BooleanRule.php
3 weeks ago
DateFormatRule.php
3 weeks ago
DateRule.php
3 weeks ago
DateTimeRule.php
3 weeks ago
EmailRule.php
3 weeks ago
EmailUniqueRule.php
3 weeks ago
ExistsRule.php
3 weeks ago
FloatRule.php
3 weeks ago
GreaterThanEqualRule.php
3 weeks ago
GreaterThanRule.php
3 weeks ago
InRule.php
3 weeks ago
IntegerRule.php
3 weeks ago
IsValidImageIdRule.php
3 weeks ago
LessThanEqualRule.php
3 weeks ago
LessThanRule.php
3 weeks ago
MaxRule.php
3 weeks ago
MinRule.php
3 weeks ago
NotInRule.php
3 weeks ago
NullableRule.php
3 weeks ago
NumberRule.php
3 weeks ago
ObjectRule.php
3 weeks ago
ProhibitedIfRule.php
3 weeks ago
ProhibitedRule.php
3 weeks ago
RegexRule.php
3 weeks ago
RequiredIfExists.php
3 weeks ago
RequiredIfRule.php
3 weeks ago
RequiredIfSiblingRule.php
3 weeks ago
RequiredRule.php
3 weeks ago
SameAsRule.php
3 weeks ago
Sanitizer.php
3 weeks ago
StringRule.php
3 weeks ago
UniqueRule.php
3 weeks ago
UrlRule.php
3 weeks ago
UserExists.php
3 weeks ago
RequiredIfSiblingRule.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Validates that a value is present and not null. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Validation\Rules |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Validation\Rules; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use function Kirki\Framework\deep_get; |
| 14 | use function Kirki\Framework\message; |
| 15 | class RequiredIfSiblingRule extends BaseRule |
| 16 | { |
| 17 | /** |
| 18 | * Determine if the value is present. |
| 19 | * |
| 20 | * @return bool |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | public function validate_rule() |
| 25 | { |
| 26 | list($field, $values) = \explode(',', $this->rule_value, 2); |
| 27 | $values = \explode(';', $values); |
| 28 | $values = \array_map(function ($value) { |
| 29 | return $this->filter_value($value); |
| 30 | }, $values); |
| 31 | $parent_key = \substr($this->key, 0, \strrpos($this->key, '.')); |
| 32 | $parent_value = deep_get($this->data, $parent_key); |
| 33 | if (\is_array($parent_value) && \array_key_exists($field, $parent_value) && \in_array($parent_value[$field], $values, \true)) { |
| 34 | if (\is_array($this->value) && empty($this->value)) { |
| 35 | return \false; |
| 36 | } |
| 37 | return !\is_null($this->value) && $this->value !== ''; |
| 38 | } |
| 39 | return \true; |
| 40 | } |
| 41 | /** |
| 42 | * Get the error message for a missing required field. |
| 43 | * |
| 44 | * @return string |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | public function get_error_message() |
| 49 | { |
| 50 | return message('validator.required_if_sibling', $this->last_key_segment()); |
| 51 | } |
| 52 | /** |
| 53 | * Ignore rule check. |
| 54 | * |
| 55 | * @return void |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | protected function ignore_rule_check() |
| 60 | { |
| 61 | return \false; |
| 62 | } |
| 63 | /** |
| 64 | * Filter value. |
| 65 | * |
| 66 | * @param mixed $value The value. |
| 67 | * |
| 68 | * @return void |
| 69 | * |
| 70 | * @since 1.0.0 |
| 71 | */ |
| 72 | protected function filter_value($value) |
| 73 | { |
| 74 | if ($value === 'false' || $value === 'FALSE') { |
| 75 | $value = \false; |
| 76 | } elseif ($value === 'true' || $value === 'TRUE') { |
| 77 | $value = \true; |
| 78 | } elseif ($value === 'null' || $value === 'NULL') { |
| 79 | $value = null; |
| 80 | } |
| 81 | return $value; |
| 82 | } |
| 83 | } |
| 84 |