-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatValueObject.php
37 lines (29 loc) · 960 Bytes
/
FloatValueObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace FrankVanHest\ValueObjects\Abstracts;
use FrankVanHest\ValueObjects\Interfaces\FloatValueObject as FloatValueObjectInterface;
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use Throwable;
abstract readonly class FloatValueObject implements FloatValueObjectInterface
{
use DontUseMagicMethods;
final protected function __construct(private float $value)
{
$this->assert($this->value);
}
final public function asFloat(): float
{
return $this->value;
}
final public function equals(?ValueObject $valueObject): bool
{
return $valueObject instanceof static && $valueObject->asFloat() === $this->value;
}
final public static function fromFloat(float $value): static
{
return new static($value);
}
/**
* @throws Throwable When the float value does not match the requirements
*/
abstract protected function assert(float $value): void;
}