Passed
Push — master ( 3ef45c...79e413 )
by Mr
02:01
created

FloatValue::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    public function format(int $decimals = 0, string $point = '.', string $separator = ','): string
40
    {
41
        return number_format($this->value, $decimals, $point, $separator);
42
    }
43
44 1
    public function isEmpty(): bool
45
    {
46 1
        return is_null($this->value);
47
    }
48
49 1
    public function isZero(): bool
50
    {
51 1
        return $this->value === 0.0;
52
    }
53
54 6
    public function toNative(): ?float
55
    {
56 6
        return $this->value;
57
    }
58
59
    /** @param self $comparator */
60 1
    public function equals($comparator): bool
61
    {
62 1
        Assertion::isInstanceOf($comparator, self::class);
63 1
        return $this->toNative() === $comparator->toNative();
64
    }
65
66 1
    public function __toString(): string
67
    {
68 1
        return is_null($this->value) ? 'null' : (string)$this->value;
69
    }
70
71 9
    private function __construct(?float $value = null)
72
    {
73 9
        $this->value = $value;
74 9
    }
75
}
76