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
BooleanRule.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Validates that the given value is a boolean. |
| 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\message; |
| 14 | class BooleanRule extends BaseRule |
| 15 | { |
| 16 | /** |
| 17 | * Check for strict data type |
| 18 | * |
| 19 | * @var bool |
| 20 | * |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | protected $check_strict_data_type = \true; |
| 24 | /** |
| 25 | * Determine if the value is a boolean. |
| 26 | * |
| 27 | * @return bool |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | public function validate_rule() |
| 32 | { |
| 33 | $value = $this->value; |
| 34 | // Normalize string values that represent booleans |
| 35 | if (\is_string($value)) { |
| 36 | $value = \strtolower($value); |
| 37 | return \in_array($value, ['true', 'false', '1', '0'], \true); |
| 38 | } |
| 39 | // Accept actual booleans and integer equivalents |
| 40 | if (\is_bool($value) || $value === 1 || $value === 0) { |
| 41 | return \true; |
| 42 | } |
| 43 | return \false; |
| 44 | } |
| 45 | /** |
| 46 | * Get the error message for a non-boolean value. |
| 47 | * |
| 48 | * @return string |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | public function get_error_message() |
| 53 | { |
| 54 | return message('validator.boolean', $this->last_key_segment()); |
| 55 | } |
| 56 | } |
| 57 |