Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
✨ Add Conversion class
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Nov 15, 2023
1 parent 0b412d8 commit 8215ef9
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/Concerns/AsBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/Concerns/AsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/AsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/AsMixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/Concerns/AsNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/AsNumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/AsScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/AsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions src/Contracts/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@ interface MixedType
{
public function get(): mixed;

public static function from(mixed $value): self;

/**
* @param class-string<MixedType> $type
*/
public function to(string $type): MixedType;
public static function of(mixed $value): self;
}
106 changes: 106 additions & 0 deletions src/Conversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values;

use Sikessem\Values\Contracts\BoolType;
use Sikessem\Values\Contracts\FloatType;
use Sikessem\Values\Contracts\IntType;
use Sikessem\Values\Contracts\MixedType;
use Sikessem\Values\Contracts\NumberType;
use Sikessem\Values\Contracts\NumericType;
use Sikessem\Values\Contracts\ScalarType;
use Sikessem\Values\Contracts\StringType;

class Conversion
{
public function __construct(protected mixed $value)
{
}

public static function from(mixed $value): self
{
return new self($value);
}

public function intoBool(): BoolType
{
return BoolValue::of($this->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();
}
}
3 changes: 2 additions & 1 deletion tests/Feat/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Feat/NumericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Feat/ScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/BoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/FloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/IntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/StringValueTest.php → tests/Unit/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 8215ef9

Please sign in to comment.