1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/value-object project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
13
|
|
|
|
14
|
|
|
final class FloatValue implements MakeEmptyInterface, ValueObjectInterface |
15
|
|
|
{ |
16
|
|
|
private ?float $value; |
17
|
|
|
|
18
|
|
|
/** @param null|int|float $value */ |
19
|
9 |
|
public static function fromNative($value): self |
20
|
|
|
{ |
21
|
9 |
|
if (is_integer($value)) { |
22
|
3 |
|
$value = floatval($value); |
23
|
|
|
} |
24
|
9 |
|
Assertion::nullOrFloat($value, 'Trying to create FloatValue VO from unsupported value type.'); |
25
|
|
|
/** @psalm-suppress PossiblyInvalidArgument */ |
26
|
9 |
|
return is_null($value) ? new self : new self(floatval($value)); |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public static function zero(): self |
30
|
|
|
{ |
31
|
2 |
|
return new self(0.0); |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
public static function makeEmpty(): self |
35
|
|
|
{ |
36
|
2 |
|
return new self; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function isEmpty(): bool |
40
|
|
|
{ |
41
|
1 |
|
return is_null($this->value); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function isZero(): bool |
45
|
|
|
{ |
46
|
1 |
|
return $this->value === 0.0; |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
public function toNative(): ?float |
50
|
|
|
{ |
51
|
6 |
|
return $this->value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @param self $comparator */ |
55
|
1 |
|
public function equals($comparator): bool |
56
|
|
|
{ |
57
|
1 |
|
Assertion::isInstanceOf($comparator, self::class); |
58
|
1 |
|
return $this->toNative() === $comparator->toNative(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function __toString(): string |
62
|
|
|
{ |
63
|
1 |
|
return is_null($this->value) ? 'null' : (string)$this->value; |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
private function __construct(?float $value = null) |
67
|
|
|
{ |
68
|
9 |
|
$this->value = $value; |
69
|
9 |
|
} |
70
|
|
|
} |
71
|
|
|
|