Passed
Push — master ( 716ba7...2c38a0 )
by Mr
01:58
created

FloatValue   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 55
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toNative() 0 3 1
A zero() 0 3 1
A isZero() 0 3 1
A equals() 0 4 1
A makeEmpty() 0 3 1
A fromNative() 0 8 3
A __construct() 0 3 1
A isEmpty() 0 3 1
A __toString() 0 3 2
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