Passed
Push — master ( 0baa7a...f13449 )
by Doug
50:51
created

Time   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 65.79%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 99
ccs 25
cts 38
cp 0.6579
rs 10
c 4
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 9 2
A multiply() 0 3 1
A divide() 0 3 1
A subtract() 0 9 2
A registerCustomUnit() 0 6 1
A makeUnit() 0 9 2
A getSupportedSRIDsWithHelp() 0 6 1
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 165
    public function subtract(self $unit): self
50
    {
51 165
        if ($this::class === $unit::class) {
52 165
            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 147
    public static function makeUnit(float $measurement, string $srid): self
71
    {
72 147
        if (isset(self::$sridData[$srid]['fqcn'])) {
73
            return new self::$sridData[$srid]['fqcn']($measurement);
74
        }
75
76 147
        return match ($srid) {
77 138
            self::EPSG_YEAR => new Year($measurement),
78 147
            default => throw new UnknownUnitOfMeasureException($srid),
79 147
        };
80
    }
81
82
    /**
83
     * @return array<string, string>
84
     */
85 27
    public static function getSupportedSRIDs(): array
86
    {
87 27
        return array_map(fn ($supportedSrid) => $supportedSrid['name'], self::$sridData);
88
    }
89
90
    /**
91
     * @return array<string, array{name: string, help: string}>
92
     */
93 9
    public static function getSupportedSRIDsWithHelp(): array
94
    {
95 9
        return array_map(fn (array $data) => [
96 9
            'name' => $data['name'],
97 9
            'help' => $data['help'],
98 9
        ], static::$sridData);
99
    }
100
101
    /**
102
     * @param class-string<self> $implementingClassFQCN
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<self> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<self>.
Loading history...
103
     */
104
    public static function registerCustomUnit(string $srid, string $name, string $implementingClassFQCN, string $help = ''): void
105
    {
106
        self::$sridData[$srid] = [
107
            'name' => $name,
108
            'fqcn' => $implementingClassFQCN,
109
            'help' => $help,
110
        ];
111
    }
112
113 18
    public function __toString(): string
114
    {
115 18
        return (string) $this->getValue();
116
    }
117
}
118