Abstracts
2 years ago
Boolean.php
2 years ago
Currency.php
2 years ago
DateTime.php
2 years ago
Email.php
2 years ago
Exclude.php
2 years ago
ExcludeIf.php
2 years ago
ExcludeUnless.php
2 years ago
In.php
2 years ago
InStrict.php
2 years ago
Integer.php
2 years ago
Max.php
2 years ago
Min.php
2 years ago
Nullable.php
2 years ago
NullableIf.php
2 years ago
NullableUnless.php
2 years ago
Numeric.php
2 years ago
Optional.php
2 years ago
OptionalIf.php
2 years ago
OptionalUnless.php
2 years ago
Required.php
2 years ago
Size.php
2 years ago
Boolean.php
73 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * |
| 5 | * Modified by impress-org on 30-October-2023 using Strauss. |
| 6 | * @see https://github.com/BrianHenryIE/strauss |
| 7 | */ |
| 8 | |
| 9 | namespace Give\Vendors\StellarWP\Validation\Rules; |
| 10 | |
| 11 | use Closure; |
| 12 | use Give\Vendors\StellarWP\Validation\Contracts\Sanitizer; |
| 13 | use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 14 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 15 | |
| 16 | use const FILTER_NULL_ON_FAILURE; |
| 17 | |
| 18 | class Boolean implements ValidationRule, ValidatesOnFrontEnd, Sanitizer |
| 19 | { |
| 20 | /** |
| 21 | * {@inheritDoc} |
| 22 | * |
| 23 | * @since 1.4.0 |
| 24 | */ |
| 25 | public static function id(): string |
| 26 | { |
| 27 | return 'boolean'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * {@inheritDoc} |
| 32 | * |
| 33 | * @since 1.4.0 |
| 34 | */ |
| 35 | public static function fromString(string $options = null): ValidationRule |
| 36 | { |
| 37 | return new self(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * {@inheritDoc} |
| 42 | * |
| 43 | * @since 1.4.1 add is_bool check and FILTER_NULL_ON_FAILURE flag to prevent false positives |
| 44 | * @since 1.4.0 |
| 45 | */ |
| 46 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 47 | { |
| 48 | if (!is_bool(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) { |
| 49 | $fail(sprintf(__('%s must be a boolean', 'give'), '{field}')); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritDoc} |
| 55 | * |
| 56 | * @since 1.4.0 |
| 57 | */ |
| 58 | public function serializeOption() |
| 59 | { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritDoc} |
| 65 | * |
| 66 | * @since 1.4.0 |
| 67 | */ |
| 68 | public function sanitize($value) |
| 69 | { |
| 70 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
| 71 | } |
| 72 | } |
| 73 |