Passed
Push — master ( 344e29...cbaf3e )
by Doug
38:02
created

Time::makeUnit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 2.0185
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
abstract class Time implements UnitOfMeasure
15
{
16
    /**
17
     * Year.
18
     */
19
    public const EPSG_YEAR = 'urn:ogc:def:uom:EPSG::1029';
20
21
    protected static array $sridData = [
22
        'urn:ogc:def:uom:EPSG::1029' => [
23
            'name' => 'year',
24
        ],
25
    ];
26
27
    protected static array $customSridData = [];
28
29
    private static array $supportedCache = [];
30
31
    abstract public function __construct(float $time);
32
33
    abstract public function asYears(): Year;
34
35 9
    public function add(self $unit): self
36
    {
37 9
        if ($this::class === $unit::class) {
38 9
            return new static($this->getValue() + $unit->getValue());
39
        }
40
        $resultAsYears = new Year($this->asYears()->getValue() + $unit->asYears()->getValue());
41
        $conversionRatio = (new static(1))->asYears()->getValue();
42
43
        return new static($resultAsYears->getValue() / $conversionRatio);
44
    }
45
46 45
    public function subtract(self $unit): self
47
    {
48 45
        if ($this::class === $unit::class) {
49 45
            return new static($this->getValue() - $unit->getValue());
50
        }
51
        $resultAsYears = new Year($this->asYears()->getValue() - $unit->asYears()->getValue());
52
        $conversionRatio = (new static(1))->asYears()->getValue();
53
54
        return new static($resultAsYears->getValue() / $conversionRatio);
55
    }
56
57 9
    public function multiply(float $multiplicand): self
58
    {
59 9
        return new static($this->getValue() * $multiplicand);
60
    }
61
62 9
    public function divide(float $divisor): self
63
    {
64 9
        return new static($this->getValue() / $divisor);
65
    }
66
67 45
    public static function makeUnit(float $measurement, string $srid): self
68
    {
69 45
        if (isset(self::$customSridData[$srid])) {
70
            return new self::$customSridData[$srid]['fqcn']($measurement);
71
        }
72
73 45
        return match ($srid) {
74 45
            self::EPSG_YEAR => new Year($measurement),
75 45
            default => throw new UnknownUnitOfMeasureException($srid),
76 45
        };
77
    }
78
79 27
    public static function getSupportedSRIDs(): array
80
    {
81 27
        if (!self::$supportedCache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::supportedCache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
            foreach (static::$sridData as $srid => $data) {
83
                self::$supportedCache[$srid] = $data['name'];
84
            }
85
        }
86
87 27
        return self::$supportedCache;
88
    }
89
90
    public static function registerCustomUnit(string $srid, string $name, string $implementingClassFQCN): void
91
    {
92
        self::$customSridData[$srid] = ['name' => $name, 'fqcn' => $implementingClassFQCN];
93
        self::getSupportedSRIDs(); // init cache if not already
94
        self::$supportedCache[$srid] = $name; // update cache
95
    }
96
97 18
    public function __toString(): string
98
    {
99 18
        return (string) $this->getValue();
100
    }
101
}
102