diff --git a/src/Cast.php b/src/Cast.php index 0611dc8..acb4c90 100644 --- a/src/Cast.php +++ b/src/Cast.php @@ -4,15 +4,6 @@ namespace Sikessem\Values; -use Sikessem\Values\Types\BoolType; -use Sikessem\Values\Types\FloatType; -use Sikessem\Values\Types\IntType; -use Sikessem\Values\Types\MixedType; -use Sikessem\Values\Types\NumberType; -use Sikessem\Values\Types\NumericType; -use Sikessem\Values\Types\ScalarType; -use Sikessem\Values\Types\StringType; - class Cast { public function __construct(protected mixed $value) {} @@ -22,83 +13,43 @@ public static function from(mixed $value): self return new self($value); } - public function intoBool(): BoolType + public function intoBool(): BoolValue { return BoolValue::of($this->value); } - public function intoInt(): IntType + public function intoInt(): IntValue { return IntValue::of($this->value); } - public function intoFloat(): FloatType + public function intoFloat(): FloatValue { return FloatValue::of($this->value); } - public function intoString(): StringType + public function intoString(): StringValue { return StringValue::of($this->value); } - public function intoNumber(): NumberType + public function intoNumber(): NumberValue { return NumberValue::of($this->value); } - public function intoNumeric(): NumericType + public function intoNumeric(): NumericValue { return NumericValue::of($this->value); } - public function intoScalar(): ScalarType + public function intoScalar(): ScalarValue { return ScalarValue::of($this->value); } - public function intoMixed(): MixedType + public function intoMixed(): MixedValue { return MixedValue::of($this->value); } - - public static function toBool(mixed $value): BoolType - { - return self::from($value)->intoBool(); - } - - public static function toInt(mixed $value): IntType - { - return self::from($value)->intoInt(); - } - - public static function toFloat(mixed $value): FloatType - { - return self::from($value)->intoFloat(); - } - - public static function toString(mixed $value): StringType - { - return self::from($value)->intoString(); - } - - public static function toNumber(mixed $value): NumberType - { - return self::from($value)->intoNumber(); - } - - public static function toNumeric(mixed $value): NumericType - { - return self::from($value)->intoNumeric(); - } - - public static function toScalar(mixed $value): ScalarType - { - return self::from($value)->intoScalar(); - } - - public static function toMixed(mixed $value): MixedType - { - return self::from($value)->intoMixed(); - } } diff --git a/src/Concerns/AsBool.php b/src/Concerns/AsBool.php index 414b573..b1d8f50 100644 --- a/src/Concerns/AsBool.php +++ b/src/Concerns/AsBool.php @@ -10,6 +10,15 @@ trait AsBool { use AsScalar; + public function __invoke(mixed $value = null): bool + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + abstract public function get(): bool; /** @@ -35,15 +44,6 @@ 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 44954e3..559db7f 100644 --- a/src/Concerns/AsFloat.php +++ b/src/Concerns/AsFloat.php @@ -10,6 +10,15 @@ trait AsFloat { use AsNumber; + public function __invoke(mixed $value = null): float + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + abstract public function get(): float; /** @@ -34,13 +43,4 @@ 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 6d3ae77..593a7d7 100644 --- a/src/Concerns/AsInt.php +++ b/src/Concerns/AsInt.php @@ -10,6 +10,15 @@ trait AsInt { use AsNumber; + public function __invoke(mixed $value = null): int + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + abstract public function get(): int; /** @@ -34,13 +43,4 @@ 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 3cf45b5..897b0bb 100644 --- a/src/Concerns/AsMixed.php +++ b/src/Concerns/AsMixed.php @@ -8,6 +8,15 @@ trait AsMixed { + public function __invoke(mixed $value = null): mixed + { + if ($value !== null) { + static::of($value)->get(); + } + + return $this->get(); + } + abstract public function get(): mixed; public static function of(mixed $value): self @@ -31,13 +40,4 @@ 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 82ab3fe..718b971 100644 --- a/src/Concerns/AsNumber.php +++ b/src/Concerns/AsNumber.php @@ -10,6 +10,15 @@ trait AsNumber { use AsNumeric; + public function __invoke(mixed $value = null): int|float + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + abstract public function get(): int|float; /** @@ -34,13 +43,4 @@ 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 6250446..51cc85b 100644 --- a/src/Concerns/AsNumeric.php +++ b/src/Concerns/AsNumeric.php @@ -10,6 +10,15 @@ trait AsNumeric { use AsScalar; + public function __invoke(mixed $value = null): int|float|string + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + abstract public function get(): int|float|string; /** @@ -34,13 +43,4 @@ 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 3d35e41..3e1eda3 100644 --- a/src/Concerns/AsScalar.php +++ b/src/Concerns/AsScalar.php @@ -13,7 +13,16 @@ trait AsScalar public function __toString(): string { /** @psalm-suppress RedundantCast */ - return (string) $this->get(); + return $this->get(); + } + + public function __invoke(mixed $value = null): bool|int|float|string + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); } abstract public function get(): bool|int|float|string; @@ -41,13 +50,4 @@ 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 86aedf9..f0d1111 100644 --- a/src/Concerns/AsString.php +++ b/src/Concerns/AsString.php @@ -10,6 +10,15 @@ trait AsString { use AsScalar; + public function __invoke(mixed $value = null): string + { + if ($value !== null) { + return static::of($value)->get(); + } + + return $this->get(); + } + /** * @throws \InvalidArgumentException If the value is not a string. */ @@ -32,13 +41,4 @@ 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 1752f52..31d4e63 100644 --- a/src/Types/BoolType.php +++ b/src/Types/BoolType.php @@ -6,6 +6,8 @@ interface BoolType extends ScalarType { + public function __invoke(mixed $value = null): bool; + public function get(): bool; public function isTrue(): bool; @@ -15,14 +17,4 @@ 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; - - public function __invoke(mixed $value = null): bool; } diff --git a/src/Types/FloatType.php b/src/Types/FloatType.php index c5f0ce9..f724362 100644 --- a/src/Types/FloatType.php +++ b/src/Types/FloatType.php @@ -6,7 +6,7 @@ interface FloatType extends NumberType { - public function get(): float; - public function __invoke(mixed $value = null): float; + + public function get(): float; } diff --git a/src/Types/IntType.php b/src/Types/IntType.php index 0135abd..406d7d6 100644 --- a/src/Types/IntType.php +++ b/src/Types/IntType.php @@ -6,7 +6,7 @@ interface IntType extends NumberType { - public function get(): int; - public function __invoke(mixed $value = null): int; + + public function get(): int; } diff --git a/src/Types/MixedType.php b/src/Types/MixedType.php index 138cd3c..fe8c66e 100644 --- a/src/Types/MixedType.php +++ b/src/Types/MixedType.php @@ -6,9 +6,9 @@ interface MixedType { + public function __invoke(mixed $value = null): mixed; + 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 d417cb0..b544c7e 100644 --- a/src/Types/NumberType.php +++ b/src/Types/NumberType.php @@ -6,7 +6,7 @@ interface NumberType extends NumericType { - public function get(): int|float; - public function __invoke(mixed $value = null): int|float; + + public function get(): int|float; } diff --git a/src/Types/NumericType.php b/src/Types/NumericType.php index 71b9148..c27265b 100644 --- a/src/Types/NumericType.php +++ b/src/Types/NumericType.php @@ -6,7 +6,7 @@ interface NumericType extends ScalarType { - public function get(): int|float|string; - public function __invoke(mixed $value = null): int|float|string; + + public function get(): int|float|string; } diff --git a/src/Types/ScalarType.php b/src/Types/ScalarType.php index 7818bf2..d82c7e0 100644 --- a/src/Types/ScalarType.php +++ b/src/Types/ScalarType.php @@ -6,7 +6,7 @@ interface ScalarType extends \Stringable, MixedType { - public function get(): bool|int|float|string; - public function __invoke(mixed $value = null): bool|int|float|string; + + public function get(): bool|int|float|string; } diff --git a/src/Types/StringType.php b/src/Types/StringType.php index 72e6804..5a556ee 100644 --- a/src/Types/StringType.php +++ b/src/Types/StringType.php @@ -6,7 +6,7 @@ interface StringType extends ScalarType { - public function get(): string; - public function __invoke(mixed $value = null): string; + + public function get(): string; }