Passed
Push — 4.x ( 2e43a7...18aaa8 )
by Doug
06:37
created

Time   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 79.31%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 85
ccs 23
cts 29
cp 0.7931
rs 10
c 3
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A makeUnit() 0 8 2
A add() 0 9 2
A multiply() 0 3 1
A divide() 0 3 1
A getSupportedSRIDsWithHelp() 0 6 1
A subtract() 0 9 2
A __toString() 0 3 1
A getSupportedSRIDs() 0 3 1
1
<?php
2
3
/**
4
 * PHPCoord.
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace PHPCoord\UnitOfMeasure\Time;
11
12
use PHPCoord\Exception\UnknownUnitOfMeasureException;
13
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
14
15
use function array_map;
16
17
abstract class Time implements UnitOfMeasure
18
{
19
    /**
20
     * Year.
21
     */
22
    public const EPSG_YEAR = 'urn:ogc:def:uom:EPSG::1029';
23
24
    /**
25
     * @var array<string, array{name: string, fqcn?: class-string<self>, help: string}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, array{name...g<self>, help: string}> at position 12 could not be parsed: Unknown type name 'class-string' at position 12 in array<string, array{name: string, fqcn?: class-string<self>, help: string}>.
Loading history...
26
     */
27
    protected static array $sridData = [
28
        'urn:ogc:def:uom:EPSG::1029' => [
29
            'name' => 'year',
30
            'help' => '',
31
        ],
32
    ];
33
34
    abstract public function __construct(float $time);
35
36
    abstract public function asYears(): Year;
37
38 9
    public function add(self $unit): self
39
    {
40 9
        if ($this::class === $unit::class) {
41 9
            return new static($this->getValue() + $unit->getValue());
42
        }
43
        $resultAsYears = new Year($this->asYears()->getValue() + $unit->asYears()->getValue());
44
        $conversionRatio = (new static(1))->asYears()->getValue();
45
46
        return new static($resultAsYears->getValue() / $conversionRatio);
47
    }
48
49 27
    public function subtract(self $unit): self
50
    {
51 27
        if ($this::class === $unit::class) {
52 27
            return new static($this->getValue() - $unit->getValue());
53
        }
54
        $resultAsYears = new Year($this->asYears()->getValue() - $unit->asYears()->getValue());
55
        $conversionRatio = (new static(1))->asYears()->getValue();
56
57
        return new static($resultAsYears->getValue() / $conversionRatio);
58
    }
59
60 9
    public function multiply(float $multiplicand): self
61
    {
62 9
        return new static($this->getValue() * $multiplicand);
63
    }
64
65 9
    public function divide(float $divisor): self
66
    {
67 9
        return new static($this->getValue() / $divisor);
68
    }
69
70 18
    public static function makeUnit(float $measurement, string $srid): self
71
    {
72
        switch ($srid) {
73 18
            case self::EPSG_YEAR:
74 9
                return new Year($measurement);
75
        }
76
77 9
        throw new UnknownUnitOfMeasureException($srid);
78
    }
79
80
    /**
81
     * @return array<string, string>
82
     */
83 36
    public static function getSupportedSRIDs(): array
84
    {
85 36
        return array_map(fn ($supportedSrid) => $supportedSrid['name'], self::$sridData);
86
    }
87
88
    /**
89
     * @return array<string, array{name: string, help: string}>
90
     */
91 9
    public static function getSupportedSRIDsWithHelp(): array
92
    {
93 9
        return array_map(fn (array $data) => [
94 9
            'name' => $data['name'],
95 9
            'help' => $data['help'],
96 9
        ], static::$sridData);
97
    }
98
99 18
    public function __toString(): string
100
    {
101 18
        return (string) $this->getValue();
102
    }
103
}
104