1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPCoord. |
5
|
|
|
* |
6
|
|
|
* @author Doug Wright |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace PHPCoord\UnitOfMeasure\Time; |
11
|
|
|
|
12
|
|
|
use PHPCoord\Exception\UnknownUnitOfMeasureException; |
13
|
|
|
use PHPCoord\UnitOfMeasure\UnitOfMeasure; |
14
|
|
|
|
15
|
|
|
use function array_map; |
16
|
|
|
|
17
|
|
|
abstract class Time implements UnitOfMeasure |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Year. |
21
|
|
|
*/ |
22
|
|
|
public const EPSG_YEAR = 'urn:ogc:def:uom:EPSG::1029'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array<string, array{name: string, fqcn?: class-string<self>, help: string}> |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
protected static array $sridData = [ |
28
|
|
|
'urn:ogc:def:uom:EPSG::1029' => [ |
29
|
|
|
'name' => 'year', |
30
|
|
|
'help' => '', |
31
|
|
|
], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
abstract public function __construct(float $time); |
35
|
|
|
|
36
|
|
|
abstract public function asYears(): Year; |
37
|
|
|
|
38
|
9 |
|
public function add(self $unit): self |
39
|
|
|
{ |
40
|
9 |
|
if ($this::class === $unit::class) { |
41
|
9 |
|
return new static($this->getValue() + $unit->getValue()); |
42
|
|
|
} |
43
|
|
|
$resultAsYears = new Year($this->asYears()->getValue() + $unit->asYears()->getValue()); |
44
|
|
|
$conversionRatio = (new static(1))->asYears()->getValue(); |
45
|
|
|
|
46
|
|
|
return new static($resultAsYears->getValue() / $conversionRatio); |
47
|
|
|
} |
48
|
|
|
|
49
|
165 |
|
public function subtract(self $unit): self |
50
|
|
|
{ |
51
|
165 |
|
if ($this::class === $unit::class) { |
52
|
165 |
|
return new static($this->getValue() - $unit->getValue()); |
53
|
|
|
} |
54
|
|
|
$resultAsYears = new Year($this->asYears()->getValue() - $unit->asYears()->getValue()); |
55
|
|
|
$conversionRatio = (new static(1))->asYears()->getValue(); |
56
|
|
|
|
57
|
|
|
return new static($resultAsYears->getValue() / $conversionRatio); |
58
|
|
|
} |
59
|
|
|
|
60
|
9 |
|
public function multiply(float $multiplicand): self |
61
|
|
|
{ |
62
|
9 |
|
return new static($this->getValue() * $multiplicand); |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
public function divide(float $divisor): self |
66
|
|
|
{ |
67
|
9 |
|
return new static($this->getValue() / $divisor); |
68
|
|
|
} |
69
|
|
|
|
70
|
147 |
|
public static function makeUnit(float $measurement, string $srid): self |
71
|
|
|
{ |
72
|
147 |
|
if (isset(self::$sridData[$srid]['fqcn'])) { |
73
|
|
|
return new self::$sridData[$srid]['fqcn']($measurement); |
74
|
|
|
} |
75
|
|
|
|
76
|
147 |
|
return match ($srid) { |
77
|
138 |
|
self::EPSG_YEAR => new Year($measurement), |
78
|
147 |
|
default => throw new UnknownUnitOfMeasureException($srid), |
79
|
147 |
|
}; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array<string, string> |
84
|
|
|
*/ |
85
|
27 |
|
public static function getSupportedSRIDs(): array |
86
|
|
|
{ |
87
|
27 |
|
return array_map(fn ($supportedSrid) => $supportedSrid['name'], self::$sridData); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array<string, array{name: string, help: string}> |
92
|
|
|
*/ |
93
|
9 |
|
public static function getSupportedSRIDsWithHelp(): array |
94
|
|
|
{ |
95
|
9 |
|
return array_map(fn (array $data) => [ |
96
|
9 |
|
'name' => $data['name'], |
97
|
9 |
|
'help' => $data['help'], |
98
|
9 |
|
], static::$sridData); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param class-string<self> $implementingClassFQCN |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
public static function registerCustomUnit(string $srid, string $name, string $implementingClassFQCN, string $help = ''): void |
105
|
|
|
{ |
106
|
|
|
self::$sridData[$srid] = [ |
107
|
|
|
'name' => $name, |
108
|
|
|
'fqcn' => $implementingClassFQCN, |
109
|
|
|
'help' => $help, |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
18 |
|
public function __toString(): string |
114
|
|
|
{ |
115
|
18 |
|
return (string) $this->getValue(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|