Passed
Push — master ( 8bd5c3...1e8670 )
by Doug
62:22
created

Time::getSupportedSRIDsWithHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\UnitOfMeasure\Time;
10
11
use PHPCoord\Exception\UnknownUnitOfMeasureException;
12
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
13
14
use function array_map;
15
16
abstract class Time implements UnitOfMeasure
17
{
18
    /**
19
     * Year.
20
     */
21
    public const EPSG_YEAR = 'urn:ogc:def:uom:EPSG::1029';
22
23
    /**
24
     * @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...
25
     */
26
    protected static array $sridData = [
27
        'urn:ogc:def:uom:EPSG::1029' => [
28
            'name' => 'year',
29
            'help' => '',
30
        ],
31
    ];
32
33
    abstract public function __construct(float $time);
34
35 9
    abstract public function asYears(): Year;
36
37 9
    public function add(self $unit): self
38 9
    {
39
        if ($this::class === $unit::class) {
40
            return new static($this->getValue() + $unit->getValue());
41
        }
42
        $resultAsYears = new Year($this->asYears()->getValue() + $unit->asYears()->getValue());
43
        $conversionRatio = (new static(1))->asYears()->getValue();
44
45
        return new static($resultAsYears->getValue() / $conversionRatio);
46 46
    }
47
48 46
    public function subtract(self $unit): self
49 46
    {
50
        if ($this::class === $unit::class) {
51
            return new static($this->getValue() - $unit->getValue());
52
        }
53
        $resultAsYears = new Year($this->asYears()->getValue() - $unit->asYears()->getValue());
54
        $conversionRatio = (new static(1))->asYears()->getValue();
55
56
        return new static($resultAsYears->getValue() / $conversionRatio);
57 9
    }
58
59 9
    public function multiply(float $multiplicand): self
60
    {
61
        return new static($this->getValue() * $multiplicand);
62 9
    }
63
64 9
    public function divide(float $divisor): self
65
    {
66
        return new static($this->getValue() / $divisor);
67 46
    }
68
69 46
    public static function makeUnit(float $measurement, string $srid): self
70
    {
71
        if (isset(self::$sridData[$srid]['fqcn'])) {
72
            return new self::$sridData[$srid]['fqcn']($measurement);
73 46
        }
74 46
75 46
        return match ($srid) {
76 46
            self::EPSG_YEAR => new Year($measurement),
77
            default => throw new UnknownUnitOfMeasureException($srid),
78
        };
79 27
    }
80
81 27
    /**
82
     * @return array<string, string>
83
     */
84
    public static function getSupportedSRIDs(): array
85
    {
86
        return array_map(fn ($supportedSrid) => $supportedSrid['name'], self::$sridData);
87 27
    }
88
89
    /**
90
     * @return array<string, array{name: string, help: string}>
91
     */
92
    public static function getSupportedSRIDsWithHelp(): array
93
    {
94
        return array_map(fn (array $data) => ['name' => $data['name'], 'help' => $data['help']], static::$sridData);
95
    }
96
97 18
    /**
98
     * @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...
99 18
     */
100
    public static function registerCustomUnit(string $srid, string $name, string $implementingClassFQCN, string $help = ''): void
101
    {
102
        self::$sridData[$srid] = ['name' => $name, 'fqcn' => $implementingClassFQCN, 'help' => $help];
103
    }
104
105
    public function __toString(): string
106
    {
107
        return (string) $this->getValue();
108
    }
109
}
110