-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatValueObject.php
More file actions
38 lines (30 loc) · 976 Bytes
/
FloatValueObject.php
File metadata and controls
38 lines (30 loc) · 976 Bytes
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
38
<?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(set) 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;
}