From 0b412d89f78bd433a96e8e869d826fc70e03e58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SIGUI=20Kess=C3=A9=20Emmanuel?= Date: Wed, 15 Nov 2023 14:43:24 +0100 Subject: [PATCH] :sparkles: Handle bool --- src/Concerns/AsBool.php | 40 ++++++++++++++++++++++++++++++++++++++ src/Contracts/BoolType.php | 16 +++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Concerns/AsBool.php b/src/Concerns/AsBool.php index d6b7fc6..96a2ad2 100644 --- a/src/Concerns/AsBool.php +++ b/src/Concerns/AsBool.php @@ -34,4 +34,44 @@ public static function from(mixed $value): self get_debug_type($value), )); } + + public function isTrue(): bool + { + return $this->get(); + } + + public function isFalse(): bool + { + return ! $this->get(); + } + + public function toTrue(): self + { + return static::from(true); + } + + public function toFalse(): self + { + return static::from(false); + } + + public static function isTruthy(bool $value): bool + { + return static::from($value)->isTrue(); + } + + public static function isFalsy(bool $value): bool + { + return static::from($value)->isFalse(); + } + + public static function truthify(mixed $value): self + { + return static::from($value)->toTrue(); + } + + public static function falsify(mixed $value): self + { + return static::from($value)->toFalse(); + } } diff --git a/src/Contracts/BoolType.php b/src/Contracts/BoolType.php index ce15cfb..dc5d0d7 100644 --- a/src/Contracts/BoolType.php +++ b/src/Contracts/BoolType.php @@ -7,4 +7,20 @@ interface BoolType extends ScalarType { public function get(): bool; + + public function isTrue(): bool; + + public function isFalse(): bool; + + public function toTrue(): self; + + public function toFalse(): self; + + public static function isTruthy(bool $value): bool; + + public static function isFalsy(bool $value): bool; + + public static function truthify(mixed $value): self; + + public static function falsify(mixed $value): self; }