From 4bd8ad45e2baec9bda9bc9f2708c85c7a81b0f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigui=20Kess=C3=A9=20Emmanuel?= Date: Fri, 27 Sep 2024 06:08:26 +0100 Subject: [PATCH] :sparkles: Add `__invoke()` method --- src/Concerns/AsBool.php | 9 +++++++++ src/Concerns/AsFloat.php | 9 +++++++++ src/Concerns/AsInt.php | 9 +++++++++ src/Concerns/AsMixed.php | 9 +++++++++ src/Concerns/AsNumber.php | 9 +++++++++ src/Concerns/AsNumeric.php | 9 +++++++++ src/Concerns/AsScalar.php | 9 +++++++++ src/Concerns/AsString.php | 9 +++++++++ src/Types/BoolType.php | 2 ++ src/Types/FloatType.php | 2 ++ src/Types/IntType.php | 2 ++ src/Types/MixedType.php | 2 ++ src/Types/NumberType.php | 2 ++ src/Types/NumericType.php | 2 ++ src/Types/ScalarType.php | 2 ++ src/Types/StringType.php | 2 ++ 16 files changed, 88 insertions(+) diff --git a/src/Concerns/AsBool.php b/src/Concerns/AsBool.php index c290472..414b573 100644 --- a/src/Concerns/AsBool.php +++ b/src/Concerns/AsBool.php @@ -35,6 +35,15 @@ public static function of(mixed $value): self )); } + public function __invoke(mixed $value = null): bool + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + public function isTrue(): bool { return $this->get(); diff --git a/src/Concerns/AsFloat.php b/src/Concerns/AsFloat.php index b9104cc..44954e3 100644 --- a/src/Concerns/AsFloat.php +++ b/src/Concerns/AsFloat.php @@ -34,4 +34,13 @@ public static function of(mixed $value): self get_debug_type($value), )); } + + public function __invoke(mixed $value = null): float + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Concerns/AsInt.php b/src/Concerns/AsInt.php index 2bb748f..6d3ae77 100644 --- a/src/Concerns/AsInt.php +++ b/src/Concerns/AsInt.php @@ -34,4 +34,13 @@ public static function of(mixed $value): self get_debug_type($value), )); } + + public function __invoke(mixed $value = null): int + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Concerns/AsMixed.php b/src/Concerns/AsMixed.php index 6d4a483..3cf45b5 100644 --- a/src/Concerns/AsMixed.php +++ b/src/Concerns/AsMixed.php @@ -31,4 +31,13 @@ public function to(string $type): MixedType { return $type::of($this->get()); } + + public function __invoke(mixed $value = null): mixed + { + if ($value !== null) { + static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Concerns/AsNumber.php b/src/Concerns/AsNumber.php index afa2a77..82ab3fe 100644 --- a/src/Concerns/AsNumber.php +++ b/src/Concerns/AsNumber.php @@ -34,4 +34,13 @@ public static function of(mixed $value): self get_debug_type($value), )); } + + public function __invoke(mixed $value = null): int|float + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Concerns/AsNumeric.php b/src/Concerns/AsNumeric.php index 479f8d0..6250446 100644 --- a/src/Concerns/AsNumeric.php +++ b/src/Concerns/AsNumeric.php @@ -34,4 +34,13 @@ public static function of(mixed $value): self get_debug_type($value), )); } + + public function __invoke(mixed $value = null): int|float|string + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Concerns/AsScalar.php b/src/Concerns/AsScalar.php index 093c819..3d35e41 100644 --- a/src/Concerns/AsScalar.php +++ b/src/Concerns/AsScalar.php @@ -41,4 +41,13 @@ public static function of(mixed $value): self get_debug_type($value), )); } + + public function __invoke(mixed $value = null): bool|int|float|string + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Concerns/AsString.php b/src/Concerns/AsString.php index 77c95a5..86aedf9 100644 --- a/src/Concerns/AsString.php +++ b/src/Concerns/AsString.php @@ -32,4 +32,13 @@ public static function of(mixed $value): self get_debug_type($value), )); } + + public function __invoke(mixed $value = null): string + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } } diff --git a/src/Types/BoolType.php b/src/Types/BoolType.php index 9d8986a..1752f52 100644 --- a/src/Types/BoolType.php +++ b/src/Types/BoolType.php @@ -23,4 +23,6 @@ public static function isFalsy(bool $value): bool; public static function truthify(mixed $value): self; public static function falsify(mixed $value): self; + + public function __invoke(mixed $value = null): bool; } diff --git a/src/Types/FloatType.php b/src/Types/FloatType.php index ed8438e..c5f0ce9 100644 --- a/src/Types/FloatType.php +++ b/src/Types/FloatType.php @@ -7,4 +7,6 @@ interface FloatType extends NumberType { public function get(): float; + + public function __invoke(mixed $value = null): float; } diff --git a/src/Types/IntType.php b/src/Types/IntType.php index 8d5cee5..0135abd 100644 --- a/src/Types/IntType.php +++ b/src/Types/IntType.php @@ -7,4 +7,6 @@ interface IntType extends NumberType { public function get(): int; + + public function __invoke(mixed $value = null): int; } diff --git a/src/Types/MixedType.php b/src/Types/MixedType.php index 84d23f9..138cd3c 100644 --- a/src/Types/MixedType.php +++ b/src/Types/MixedType.php @@ -9,4 +9,6 @@ interface MixedType public function get(): mixed; public static function of(mixed $value): self; + + public function __invoke(mixed $value = null): mixed; } diff --git a/src/Types/NumberType.php b/src/Types/NumberType.php index bba2082..d417cb0 100644 --- a/src/Types/NumberType.php +++ b/src/Types/NumberType.php @@ -7,4 +7,6 @@ interface NumberType extends NumericType { public function get(): int|float; + + public function __invoke(mixed $value = null): int|float; } diff --git a/src/Types/NumericType.php b/src/Types/NumericType.php index dc2d6dd..71b9148 100644 --- a/src/Types/NumericType.php +++ b/src/Types/NumericType.php @@ -7,4 +7,6 @@ interface NumericType extends ScalarType { public function get(): int|float|string; + + public function __invoke(mixed $value = null): int|float|string; } diff --git a/src/Types/ScalarType.php b/src/Types/ScalarType.php index 7241fc6..7818bf2 100644 --- a/src/Types/ScalarType.php +++ b/src/Types/ScalarType.php @@ -7,4 +7,6 @@ interface ScalarType extends \Stringable, MixedType { public function get(): bool|int|float|string; + + public function __invoke(mixed $value = null): bool|int|float|string; } diff --git a/src/Types/StringType.php b/src/Types/StringType.php index c06ae33..72e6804 100644 --- a/src/Types/StringType.php +++ b/src/Types/StringType.php @@ -7,4 +7,6 @@ interface StringType extends ScalarType { public function get(): string; + + public function __invoke(mixed $value = null): string; }