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