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

Rate::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 0
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;
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\Centimetre;
16
use PHPCoord\UnitOfMeasure\Length\Metre;
17
use PHPCoord\UnitOfMeasure\Length\Millimetre;
18
use PHPCoord\UnitOfMeasure\Scale\PartsPerBillion;
19
use PHPCoord\UnitOfMeasure\Scale\PartsPerMillion;
20
use PHPCoord\UnitOfMeasure\Time\Time;
21
use PHPCoord\UnitOfMeasure\Time\Year;
22
23
use function array_map;
24
25
class Rate implements UnitOfMeasure
26
{
27
    /**
28
     * Arc-seconds per year
29
     * =((pi/180) / 3600) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM code
30
     * 1029.
31
     */
32
    public const EPSG_ARC_SECONDS_PER_YEAR = 'urn:ogc:def:uom:EPSG::1043';
33
34
    /**
35
     * Centimetres per year
36
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
37
     */
38
    public const EPSG_CENTIMETRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1034';
39
40
    /**
41
     * Metres per year
42
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
43
     */
44
    public const EPSG_METRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1042';
45
46
    /**
47
     * Milliarc-seconds per year
48
     * = ((pi/180) / 3600 / 1000) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM
49
     * code 1029.
50
     */
51
    public const EPSG_MILLIARC_SECONDS_PER_YEAR = 'urn:ogc:def:uom:EPSG::1032';
52
53
    /**
54
     * Millimetres per year
55
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
56
     */
57
    public const EPSG_MILLIMETRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1027';
58
59
    /**
60
     * Parts per billion per year
61
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029. Billion is internationally
62
     * ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be 1E+9.
63
     */
64
    public const EPSG_PARTS_PER_BILLION_PER_YEAR = 'urn:ogc:def:uom:EPSG::1030';
65
66
    /**
67
     * Parts per million per year
68
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
69
     */
70
    public const EPSG_PARTS_PER_MILLION_PER_YEAR = 'urn:ogc:def:uom:EPSG::1041';
71
72
    /**
73
     * @var array<string, array{name: string, help: string}>
74
     */
75
    protected static array $sridData = [
76
        'urn:ogc:def:uom:EPSG::1027' => [
77
            'name' => 'millimetres per year',
78
            'help' => 'Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.',
79
        ],
80
        'urn:ogc:def:uom:EPSG::1030' => [
81
            'name' => 'parts per billion per year',
82
            'help' => 'Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029. Billion is internationally ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be 1E+9.',
83
        ],
84
        'urn:ogc:def:uom:EPSG::1032' => [
85
            'name' => 'milliarc-seconds per year',
86
            'help' => '= ((pi/180) / 3600 / 1000) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.',
87
        ],
88
        'urn:ogc:def:uom:EPSG::1034' => [
89
            'name' => 'centimetres per year',
90
            'help' => 'Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.',
91
        ],
92
        'urn:ogc:def:uom:EPSG::1041' => [
93
            'name' => 'parts per million per year',
94
            'help' => 'Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.',
95
        ],
96
        'urn:ogc:def:uom:EPSG::1042' => [
97
            'name' => 'metres per year',
98
            'help' => 'Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.',
99
        ],
100 199
        'urn:ogc:def:uom:EPSG::1043' => [
101
            'name' => 'arc-seconds per year',
102 199
            'help' => '=((pi/180) / 3600) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.',
103
        ],
104
    ];
105
106 199
    private UnitOfMeasure $change;
107 199
108
    private Time $time;
109
110 9
    public function __construct(UnitOfMeasure $change, Time $time)
111
    {
112 9
        if ($change instanceof Time) {
113
            throw new InvalidRateException('A rate is a change per unit of time, the change cannot be time');
114
        }
115 37
116
        $this->change = $change;
117 37
        $this->time = $time;
118
    }
119
120 9
    public function getChange(): UnitOfMeasure
121
    {
122 9
        return $this->change;
123
    }
124
125 18
    public function getChangePerYear(): UnitOfMeasure
126
    {
127 18
        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

127
        return $this->change->/** @scrutinizer ignore-call */ divide($this->time->asYears()->getValue());
Loading history...
128
    }
129
130 9
    public function getTime(): Time
131
    {
132 9
        return $this->time;
133
    }
134
135 9
    public function getValue(): float
136
    {
137 9
        return $this->change->getValue();
138
    }
139
140 10
    public function getUnitName(): string
141
    {
142 10
        return $this->change->getUnitName() . ' per ' . $this->time->getUnitName();
143
    }
144
145 154
    public function __toString(): string
146
    {
147 154
        return (string) $this->getValue();
148 9
    }
149
150
    public function multiply(float $multiplicand): self
151 145
    {
152 145
        return new self($this->change->multiply($multiplicand), $this->time);
0 ignored issues
show
Bug introduced by
The method multiply() does not exist on PHPCoord\UnitOfMeasure\UnitOfMeasure. Since it exists in all sub-types, consider adding an abstract or default implementation to PHPCoord\UnitOfMeasure\UnitOfMeasure. ( Ignorable by Annotation )

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

152
        return new self($this->change->/** @scrutinizer ignore-call */ multiply($multiplicand), $this->time);
Loading history...
153 145
    }
154 145
155 145
    public static function makeUnit(float $measurement, string $srid): self
156 145
    {
157 145
        if (!isset(self::$sridData[$srid])) {
158 145
            throw new UnknownUnitOfMeasureException($srid);
159 145
        }
160 145
161
        return match ($srid) {
162
            self::EPSG_ARC_SECONDS_PER_YEAR => new self(new ArcSecond($measurement), new Year(1)),
163 9
            self::EPSG_MILLIARC_SECONDS_PER_YEAR => new self(Angle::makeUnit($measurement, Angle::EPSG_MILLIARC_SECOND), new Year(1)),
164
            self::EPSG_METRES_PER_YEAR => new self(new Metre($measurement), new Year(1)),
165 9
            self::EPSG_MILLIMETRES_PER_YEAR => new self(new Millimetre($measurement), new Year(1)),
166
            self::EPSG_CENTIMETRES_PER_YEAR => new self(new Centimetre($measurement), new Year(1)),
167
            self::EPSG_PARTS_PER_BILLION_PER_YEAR => new self(new PartsPerBillion($measurement), new Year(1)),
168
            self::EPSG_PARTS_PER_MILLION_PER_YEAR => new self(new PartsPerMillion($measurement), new Year(1)),
169
            default => throw new UnknownUnitOfMeasureException($srid),
170
        };
171 9
    }
172
173
    /**
174
     * @return array<string, string>
175
     */
176
    public static function getSupportedSRIDs(): array
177
    {
178
        return array_map(fn (array $data) => $data['name'], static::$sridData);
179
    }
180
181
    /**
182
     * @return array<string, array{name: string, help: string}>
183
     */
184
    public static function getSupportedSRIDsWithHelp(): array
185
    {
186
        return array_map(fn (array $data) => ['name' => $data['name'], 'help' => $data['help']], static::$sridData);
187
    }
188
}
189