This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
generated from Sikessem/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |