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

Commit

Permalink
✨ Add int and float value objects
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Nov 13, 2023
1 parent b3d563e commit 317dc5f
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Concerns/AsFloat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Concerns;

use Sikessem\Values\Contracts\NumberType;

trait AsFloat
{
use AsNumber;

abstract public function get(): float;

/**
* @throws \InvalidArgumentException If the value is not a float.
*/
public static function from(mixed $value): self
{
if ($value instanceof static) {
return $value;
}

if ($value instanceof NumberType) {
$value = (float) $value->get();
}

if (is_float($value)) {
return new self($value);
}

throw new \InvalidArgumentException(sprintf(
'Value "%s" is not a float.',
get_debug_type($value),
));
}
}
37 changes: 37 additions & 0 deletions src/Concerns/AsInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Concerns;

use Sikessem\Values\Contracts\NumberType;

trait AsInt
{
use AsNumber;

abstract public function get(): int;

/**
* @throws \InvalidArgumentException If the value is not an integer.
*/
public static function from(mixed $value): self
{
if ($value instanceof static) {
return $value;
}

if ($value instanceof NumberType) {
$value = (int) $value->get();
}

if (is_int($value)) {
return new self($value);
}

throw new \InvalidArgumentException(sprintf(
'Value "%s" is not an integer.',
get_debug_type($value),
));
}
}
10 changes: 10 additions & 0 deletions src/Contracts/FloatType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Contracts;

interface FloatType extends NumberType
{
public function get(): float;
}
10 changes: 10 additions & 0 deletions src/Contracts/IntType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values\Contracts;

interface IntType extends NumberType
{
public function get(): int;
}
23 changes: 23 additions & 0 deletions src/FloatValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values;

use Sikessem\Values\Concerns\AsFloat;
use Sikessem\Values\Contracts\FloatType;

class FloatValue implements FloatType
{
use AsFloat;

public function __construct(protected float $value)
{
$this->value = $value;
}

public function get(): float
{
return $this->value;
}
}
23 changes: 23 additions & 0 deletions src/IntValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sikessem\Values;

use Sikessem\Values\Concerns\AsInt;
use Sikessem\Values\Contracts\IntType;

class IntValue implements IntType
{
use AsInt;

public function __construct(protected int $value)
{
$this->value = $value;
}

public function get(): int
{
return $this->value;
}
}
27 changes: 27 additions & 0 deletions tests/Unit/FloatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Sikessem\Values\Contracts\FloatType;
use Sikessem\Values\Contracts\NumberType;
use Sikessem\Values\FloatValue;

beforeEach(function () {
$this->float = FloatValue::from(84.21);
});

it('should be instantiable', function () {
expect($this->float)->toBeInstanceOf(FloatValue::class);
});

it('should be an instance of FloatType', function () {
expect($this->float)->toBeInstanceOf(FloatType::class);
});

it('should be an instance of NumberType', function () {
expect($this->float)->toBeInstanceOf(NumberType::class);
});

it('should return the value', function () {
expect($this->float->get())->toBe(84.21);
});
27 changes: 27 additions & 0 deletions tests/Unit/IntTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Sikessem\Values\Contracts\IntType;
use Sikessem\Values\Contracts\NumberType;
use Sikessem\Values\IntValue;

beforeEach(function () {
$this->int = IntValue::from(42);
});

it('should be instantiable', function () {
expect($this->int)->toBeInstanceOf(IntValue::class);
});

it('should be an instance of IntType', function () {
expect($this->int)->toBeInstanceOf(IntType::class);
});

it('should be an instance of NumberType', function () {
expect($this->int)->toBeInstanceOf(NumberType::class);
});

it('should return the value', function () {
expect($this->int->get())->toBe(42);
});

0 comments on commit 317dc5f

Please sign in to comment.