Passed
Push — 4.x ( 1d49e7...4cde9a )
by Doug
06:44
created

Rate::makeUnit()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 2
dl 0
loc 24
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
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;
10
11
use PHPCoord\Exception\InvalidRateException;
12
use PHPCoord\Exception\UnknownUnitOfMeasureException;
13
use PHPCoord\UnitOfMeasure\Angle\Angle;
14
use PHPCoord\UnitOfMeasure\Angle\ArcSecond;
15
use PHPCoord\UnitOfMeasure\Length\Length;
16
use PHPCoord\UnitOfMeasure\Length\Metre;
17
use PHPCoord\UnitOfMeasure\Scale\PartsPerBillion;
18
use PHPCoord\UnitOfMeasure\Scale\PartsPerMillion;
19
use PHPCoord\UnitOfMeasure\Time\Time;
20
use PHPCoord\UnitOfMeasure\Time\Year;
21
22
class Rate implements UnitOfMeasure
23
{
24
    /**
25
     * arc-seconds per year
26
     * =((pi/180) / 3600) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM code
27
     * 1029.
28
     */
29
    public const EPSG_ARC_SECONDS_PER_YEAR = 'urn:ogc:def:uom:EPSG::1043';
30
31
    /**
32
     * centimetres per year
33
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
34
     */
35
    public const EPSG_CENTIMETRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1034';
36
37
    /**
38
     * metres per year
39
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
40
     */
41
    public const EPSG_METRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1042';
42
43
    /**
44
     * milliarc-seconds per year
45
     * = ((pi/180) / 3600 / 1000) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM
46
     * code 1029.
47
     */
48
    public const EPSG_MILLIARC_SECONDS_PER_YEAR = 'urn:ogc:def:uom:EPSG::1032';
49
50
    /**
51
     * millimetres per year
52
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
53
     */
54
    public const EPSG_MILLIMETRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1027';
55
56
    /**
57
     * parts per billion per year
58
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029. Billion is internationally
59
     * ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be 1E+9.
60
     */
61
    public const EPSG_PARTS_PER_BILLION_PER_YEAR = 'urn:ogc:def:uom:EPSG::1030';
62
63
    /**
64
     * parts per million per year
65
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
66
     */
67
    public const EPSG_PARTS_PER_MILLION_PER_YEAR = 'urn:ogc:def:uom:EPSG::1041';
68
69
    protected static array $sridData = [
70
        'urn:ogc:def:uom:EPSG::1027' => [
71
            'name' => 'millimetres per year',
72
        ],
73
        'urn:ogc:def:uom:EPSG::1030' => [
74
            'name' => 'parts per billion per year',
75
        ],
76
        'urn:ogc:def:uom:EPSG::1032' => [
77
            'name' => 'milliarc-seconds per year',
78
        ],
79
        'urn:ogc:def:uom:EPSG::1034' => [
80
            'name' => 'centimetres per year',
81
        ],
82
        'urn:ogc:def:uom:EPSG::1041' => [
83
            'name' => 'parts per million per year',
84
        ],
85
        'urn:ogc:def:uom:EPSG::1042' => [
86
            'name' => 'metres per year',
87
        ],
88
        'urn:ogc:def:uom:EPSG::1043' => [
89
            'name' => 'arc-seconds per year',
90
        ],
91
    ];
92
93
    private UnitOfMeasure $change;
94
95
    private Time $time;
96
97 20
    public function __construct(UnitOfMeasure $change, Time $time)
98
    {
99 20
        if ($change instanceof Time) {
100
            throw new InvalidRateException('A rate is a change per unit of time, the change cannot be time');
101
        }
102
103 20
        $this->change = $change;
104 20
        $this->time = $time;
105 20
    }
106
107 1
    public function getChange(): UnitOfMeasure
108
    {
109 1
        return $this->change;
110
    }
111
112 2
    public function getChangePerYear(): UnitOfMeasure
113
    {
114 2
        return $this->change->divide($this->time->asYears()->getValue());
0 ignored issues
show
Bug introduced by
The method divide() does not exist on PHPCoord\UnitOfMeasure\UnitOfMeasure. It seems like you code against a sub-type of said class. However, the method does not exist in PHPCoord\UnitOfMeasure\Rate. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        return $this->change->/** @scrutinizer ignore-call */ divide($this->time->asYears()->getValue());
Loading history...
115
    }
116
117 1
    public function getTime(): Time
118
    {
119 1
        return $this->time;
120
    }
121
122 2
    public function getValue(): float
123
    {
124 2
        return $this->change->getValue();
125
    }
126
127 1
    public function getUnitName(): string
128
    {
129 1
        return $this->change->getUnitName() . ' per ' . $this->time->getUnitName();
130
    }
131
132 1
    public function __toString(): string
133
    {
134 1
        return (string) $this->getValue();
135
    }
136
137 15
    public static function makeUnit(float $measurement, string $srid): self
138
    {
139 15
        if (!isset(static::$sridData[$srid])) {
140 1
            throw new UnknownUnitOfMeasureException($srid);
141
        }
142
143
        switch ($srid) {
144 14
            case self::EPSG_ARC_SECONDS_PER_YEAR:
145 2
                return new self(new ArcSecond($measurement), new Year(1));
146 12
            case self::EPSG_MILLIARC_SECONDS_PER_YEAR:
147 2
                return new self(Angle::makeUnit($measurement, Angle::EPSG_MILLIARC_SECOND), new Year(1));
148 10
            case self::EPSG_METRES_PER_YEAR:
149 2
                return new self(new Metre($measurement), new Year(1));
150 8
            case self::EPSG_MILLIMETRES_PER_YEAR:
151 2
                return new self(Length::makeUnit($measurement, Length::EPSG_MILLIMETRE), new Year(1));
152 6
            case self::EPSG_CENTIMETRES_PER_YEAR:
153 2
                return new self(Length::makeUnit($measurement, Length::EPSG_CENTIMETRE), new Year(1));
154 4
            case self::EPSG_PARTS_PER_BILLION_PER_YEAR:
155 2
                return new self(new PartsPerBillion($measurement), new Year(1));
156 2
            case self::EPSG_PARTS_PER_MILLION_PER_YEAR:
157 2
                return new self(new PartsPerMillion($measurement), new Year(1));
158
        }
159
160
        throw new UnknownUnitOfMeasureException($srid); //@codeCoverageIgnore
161
    }
162
163 9
    public static function getSupportedSRIDs(): array
164
    {
165 9
        return array_map(function ($sridData) {return $sridData['name']; }, static::$sridData);
166
    }
167
}
168