From 8215ef9fd07f25eb214b8354a3b605371a7ad33e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SIGUI=20Kess=C3=A9=20Emmanuel?= Date: Wed, 15 Nov 2023 17:57:59 +0100 Subject: [PATCH] :sparkles: Add Conversion class --- src/Concerns/AsBool.php | 14 +-- src/Concerns/AsFloat.php | 2 +- src/Concerns/AsInt.php | 2 +- src/Concerns/AsMixed.php | 4 +- src/Concerns/AsNumber.php | 2 +- src/Concerns/AsNumeric.php | 2 +- src/Concerns/AsScalar.php | 2 +- src/Concerns/AsString.php | 2 +- src/Contracts/MixedType.php | 7 +- src/Conversion.php | 106 ++++++++++++++++++ tests/Feat/NumberTest.php | 3 +- tests/Feat/NumericTest.php | 3 +- tests/Feat/ScalarTest.php | 3 +- tests/Unit/BoolTest.php | 3 +- tests/Unit/FloatTest.php | 3 +- tests/Unit/IntTest.php | 3 +- .../{StringValueTest.php => StringTest.php} | 3 +- 17 files changed, 136 insertions(+), 28 deletions(-) create mode 100644 src/Conversion.php rename tests/Unit/{StringValueTest.php => StringTest.php} (87%) diff --git a/src/Concerns/AsBool.php b/src/Concerns/AsBool.php index 96a2ad2..aa4d5a5 100644 --- a/src/Concerns/AsBool.php +++ b/src/Concerns/AsBool.php @@ -15,7 +15,7 @@ abstract public function get(): bool; /** * @throws \InvalidArgumentException If the value is not a boolean. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; @@ -47,31 +47,31 @@ public function isFalse(): bool public function toTrue(): self { - return static::from(true); + return static::of(true); } public function toFalse(): self { - return static::from(false); + return static::of(false); } public static function isTruthy(bool $value): bool { - return static::from($value)->isTrue(); + return static::of($value)->isTrue(); } public static function isFalsy(bool $value): bool { - return static::from($value)->isFalse(); + return static::of($value)->isFalse(); } public static function truthify(mixed $value): self { - return static::from($value)->toTrue(); + return static::of($value)->toTrue(); } public static function falsify(mixed $value): self { - return static::from($value)->toFalse(); + return static::of($value)->toFalse(); } } diff --git a/src/Concerns/AsFloat.php b/src/Concerns/AsFloat.php index c51bf5a..6d4faa8 100644 --- a/src/Concerns/AsFloat.php +++ b/src/Concerns/AsFloat.php @@ -15,7 +15,7 @@ abstract public function get(): float; /** * @throws \InvalidArgumentException If the value is not a float. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; diff --git a/src/Concerns/AsInt.php b/src/Concerns/AsInt.php index 60013a9..d0bb836 100644 --- a/src/Concerns/AsInt.php +++ b/src/Concerns/AsInt.php @@ -15,7 +15,7 @@ abstract public function get(): int; /** * @throws \InvalidArgumentException If the value is not an integer. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; diff --git a/src/Concerns/AsMixed.php b/src/Concerns/AsMixed.php index cd7552b..fd8f5a0 100644 --- a/src/Concerns/AsMixed.php +++ b/src/Concerns/AsMixed.php @@ -10,7 +10,7 @@ trait AsMixed { abstract public function get(): mixed; - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; @@ -29,6 +29,6 @@ public static function from(mixed $value): self */ public function to(string $type): MixedType { - return $type::from($this->get()); + return $type::of($this->get()); } } diff --git a/src/Concerns/AsNumber.php b/src/Concerns/AsNumber.php index f010851..ed5868e 100644 --- a/src/Concerns/AsNumber.php +++ b/src/Concerns/AsNumber.php @@ -15,7 +15,7 @@ abstract public function get(): int|float; /** * @throws \InvalidArgumentException If the value is not a number. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; diff --git a/src/Concerns/AsNumeric.php b/src/Concerns/AsNumeric.php index 79b6906..79ba09b 100644 --- a/src/Concerns/AsNumeric.php +++ b/src/Concerns/AsNumeric.php @@ -15,7 +15,7 @@ abstract public function get(): int|float|string; /** * @throws \InvalidArgumentException If the value is not numeric. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; diff --git a/src/Concerns/AsScalar.php b/src/Concerns/AsScalar.php index f39f958..a0c85cc 100644 --- a/src/Concerns/AsScalar.php +++ b/src/Concerns/AsScalar.php @@ -21,7 +21,7 @@ abstract public function get(): bool|int|float|string; /** * @throws \InvalidArgumentException If the value is not a scalar. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; diff --git a/src/Concerns/AsString.php b/src/Concerns/AsString.php index 480670e..4f58e16 100644 --- a/src/Concerns/AsString.php +++ b/src/Concerns/AsString.php @@ -13,7 +13,7 @@ trait AsString /** * @throws \InvalidArgumentException If the value is not a string. */ - public static function from(mixed $value): self + public static function of(mixed $value): self { if ($value instanceof static) { return $value; diff --git a/src/Contracts/MixedType.php b/src/Contracts/MixedType.php index a949991..b9fad13 100644 --- a/src/Contracts/MixedType.php +++ b/src/Contracts/MixedType.php @@ -8,10 +8,5 @@ interface MixedType { public function get(): mixed; - public static function from(mixed $value): self; - - /** - * @param class-string $type - */ - public function to(string $type): MixedType; + public static function of(mixed $value): self; } diff --git a/src/Conversion.php b/src/Conversion.php new file mode 100644 index 0000000..6108ca1 --- /dev/null +++ b/src/Conversion.php @@ -0,0 +1,106 @@ +value); + } + + public function intoInt(): IntType + { + return IntValue::of($this->value); + } + + public function intoFloat(): FloatType + { + return FloatValue::of($this->value); + } + + public function intoString(): StringType + { + return StringValue::of($this->value); + } + + public function intoNumber(): NumberType + { + return NumberValue::of($this->value); + } + + public function intoNumeric(): NumericType + { + return NumericValue::of($this->value); + } + + public function intoScalar(): ScalarType + { + return ScalarValue::of($this->value); + } + + public function intoMixed(): MixedType + { + 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/tests/Feat/NumberTest.php b/tests/Feat/NumberTest.php index 579096e..5304a52 100644 --- a/tests/Feat/NumberTest.php +++ b/tests/Feat/NumberTest.php @@ -4,10 +4,11 @@ use Sikessem\Values\Contracts\NumberType; use Sikessem\Values\Contracts\NumericType; +use Sikessem\Values\Conversion; use Sikessem\Values\NumberValue; beforeEach(function () { - $this->number = NumberValue::from(84.21); + $this->number = Conversion::toNumber(84.21); }); it('should be instantiable', function () { diff --git a/tests/Feat/NumericTest.php b/tests/Feat/NumericTest.php index 0915769..670abec 100644 --- a/tests/Feat/NumericTest.php +++ b/tests/Feat/NumericTest.php @@ -4,10 +4,11 @@ use Sikessem\Values\Contracts\NumericType; use Sikessem\Values\Contracts\ScalarType; +use Sikessem\Values\Conversion; use Sikessem\Values\NumericValue; beforeEach(function () { - $this->numeric = NumericValue::from('84.21'); + $this->numeric = Conversion::toNumeric('84.21'); }); it('should be instantiable', function () { diff --git a/tests/Feat/ScalarTest.php b/tests/Feat/ScalarTest.php index 21577ce..b836ea4 100644 --- a/tests/Feat/ScalarTest.php +++ b/tests/Feat/ScalarTest.php @@ -4,10 +4,11 @@ use Sikessem\Values\Contracts\MixedType; use Sikessem\Values\Contracts\ScalarType; +use Sikessem\Values\Conversion; use Sikessem\Values\ScalarValue; beforeEach(function () { - $this->scalar = ScalarValue::from('Hello World'); + $this->scalar = Conversion::toScalar('Hello World'); }); it('should be instantiable', function () { diff --git a/tests/Unit/BoolTest.php b/tests/Unit/BoolTest.php index 7b6ddfb..4df84f7 100644 --- a/tests/Unit/BoolTest.php +++ b/tests/Unit/BoolTest.php @@ -5,9 +5,10 @@ use Sikessem\Values\BoolValue; use Sikessem\Values\Contracts\BoolType; use Sikessem\Values\Contracts\ScalarType; +use Sikessem\Values\Conversion; beforeEach(function () { - $this->bool = BoolValue::from(true); + $this->bool = Conversion::toBool(true); }); it('should be instantiable', function () { diff --git a/tests/Unit/FloatTest.php b/tests/Unit/FloatTest.php index 647bb82..ec0acca 100644 --- a/tests/Unit/FloatTest.php +++ b/tests/Unit/FloatTest.php @@ -4,10 +4,11 @@ use Sikessem\Values\Contracts\FloatType; use Sikessem\Values\Contracts\NumberType; +use Sikessem\Values\Conversion; use Sikessem\Values\FloatValue; beforeEach(function () { - $this->float = FloatValue::from(84.21); + $this->float = Conversion::toFloat(84.21); }); it('should be instantiable', function () { diff --git a/tests/Unit/IntTest.php b/tests/Unit/IntTest.php index 5731f7d..487dfec 100644 --- a/tests/Unit/IntTest.php +++ b/tests/Unit/IntTest.php @@ -4,10 +4,11 @@ use Sikessem\Values\Contracts\IntType; use Sikessem\Values\Contracts\NumberType; +use Sikessem\Values\Conversion; use Sikessem\Values\IntValue; beforeEach(function () { - $this->int = IntValue::from(42); + $this->int = Conversion::toInt(42); }); it('should be instantiable', function () { diff --git a/tests/Unit/StringValueTest.php b/tests/Unit/StringTest.php similarity index 87% rename from tests/Unit/StringValueTest.php rename to tests/Unit/StringTest.php index e9f90d7..feb5be5 100644 --- a/tests/Unit/StringValueTest.php +++ b/tests/Unit/StringTest.php @@ -4,10 +4,11 @@ use Sikessem\Values\Contracts\ScalarType; use Sikessem\Values\Contracts\StringType; +use Sikessem\Values\Conversion; use Sikessem\Values\StringValue; beforeEach(function () { - $this->string = StringValue::from('Hello World'); + $this->string = Conversion::toString('Hello World'); }); it('should be instantiable', function () {