Passed
Push — 4.x ( 2e43a7...18aaa8 )
by Doug
06:37
created

Scale::getSupportedSRIDsWithHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHPCoord.
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace PHPCoord\UnitOfMeasure\Scale;
11
12
use PHPCoord\Exception\UnknownUnitOfMeasureException;
13
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
14
15
use function array_map;
16
17
abstract class Scale implements UnitOfMeasure
18
{
19
    /**
20
     * Coefficient
21
     * Used when parameters are coefficients.  They inherently take the units which depend upon the term to which the
22
     * coefficient applies.
23
     */
24
    public const EPSG_COEFFICIENT = 'urn:ogc:def:uom:EPSG::9203';
25
26
    /**
27
     * Parts per billion
28
     * Billion is internationally ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be
29
     * 1E+9.
30
     */
31
    public const EPSG_PARTS_PER_BILLION = 'urn:ogc:def:uom:EPSG::1028';
32
33
    /**
34
     * Parts per million.
35
     */
36
    public const EPSG_PARTS_PER_MILLION = 'urn:ogc:def:uom:EPSG::9202';
37
38
    /**
39
     * Unity
40
     * EPSG standard unit for scale. SI coherent derived unit (standard unit) for dimensionless quantity, expressed by
41
     * the number one but this is not explicitly shown.
42
     */
43
    public const EPSG_UNITY = 'urn:ogc:def:uom:EPSG::9201';
44
45
    /**
46
     * @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...
47
     */
48
    protected static array $sridData = [
49
        'urn:ogc:def:uom:EPSG::1028' => [
50
            'name' => 'parts per billion',
51
            'help' => 'Billion is internationally ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be 1E+9.',
52
        ],
53
        'urn:ogc:def:uom:EPSG::9201' => [
54
            'name' => 'unity',
55
            'help' => 'EPSG standard unit for scale. SI coherent derived unit (standard unit) for dimensionless quantity, expressed by the number one but this is not explicitly shown.',
56
        ],
57
        'urn:ogc:def:uom:EPSG::9202' => [
58
            'name' => 'parts per million',
59
            'help' => '',
60
        ],
61
        'urn:ogc:def:uom:EPSG::9203' => [
62
            'name' => 'coefficient',
63
            'help' => 'Used when parameters are coefficients.  They inherently take the units which depend upon the term to which the coefficient applies.',
64
        ],
65
    ];
66
67
    abstract public function __construct(float $scale);
68
69
    abstract public function asUnity(): Unity;
70
71 36
    public function add(self $unit): self
72
    {
73 36
        if ($this::class === $unit::class) {
74 36
            return new static($this->getValue() + $unit->getValue());
75
        }
76
        $resultAsUnity = new Unity($this->asUnity()->getValue() + $unit->asUnity()->getValue());
77
        $conversionRatio = (new static(1))->asUnity()->getValue();
78
79
        return new static($resultAsUnity->getValue() / $conversionRatio);
80
    }
81
82 9
    public function subtract(self $unit): self
83
    {
84 9
        if ($this::class === $unit::class) {
85 9
            return new static($this->getValue() - $unit->getValue());
86
        }
87
        $resultAsUnity = new Unity($this->asUnity()->getValue() - $unit->asUnity()->getValue());
88
        $conversionRatio = (new static(1))->asUnity()->getValue();
89
90
        return new static($resultAsUnity->getValue() / $conversionRatio);
91
    }
92
93 36
    public function multiply(float $multiplicand): self
94
    {
95 36
        return new static($this->getValue() * $multiplicand);
96
    }
97
98 27
    public function divide(float $divisor): self
99
    {
100 27
        return new static($this->getValue() / $divisor);
101
    }
102
103 197
    public static function makeUnit(float $measurement, string $srid): self
104
    {
105
        switch ($srid) {
106 197
            case self::EPSG_COEFFICIENT:
107 27
                return new Coefficient($measurement);
108 197
            case self::EPSG_PARTS_PER_BILLION:
109
                return new PartsPerBillion($measurement);
110 197
            case self::EPSG_PARTS_PER_MILLION:
111 134
                return new PartsPerMillion($measurement);
112 143
            case self::EPSG_UNITY:
113 134
                return new Unity($measurement);
114
        }
115
116 9
        throw new UnknownUnitOfMeasureException($srid);
117
    }
118
119
    /**
120
     * @return array<string, string>
121
     */
122 27
    public static function getSupportedSRIDs(): array
123
    {
124 27
        return array_map(fn ($supportedSrid) => $supportedSrid['name'], self::$sridData);
125
    }
126
127
    /**
128
     * @return array<string, array{name: string, help: string}>
129
     */
130 9
    public static function getSupportedSRIDsWithHelp(): array
131
    {
132 9
        return array_map(fn (array $data) => [
133 9
            'name' => $data['name'],
134 9
            'help' => $data['help'],
135 9
        ], static::$sridData);
136
    }
137
138 9
    public function __toString(): string
139
    {
140 9
        return (string) $this->getValue();
141
    }
142
}
143