|
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
|
27 |
|
public function subtract(self $unit): self |
|
50
|
|
|
{ |
|
51
|
27 |
|
if ($this::class === $unit::class) { |
|
52
|
27 |
|
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
|
18 |
|
public static function makeUnit(float $measurement, string $srid): self |
|
71
|
|
|
{ |
|
72
|
|
|
switch ($srid) { |
|
73
|
18 |
|
case self::EPSG_YEAR: |
|
74
|
9 |
|
return new Year($measurement); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
9 |
|
throw new UnknownUnitOfMeasureException($srid); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array<string, string> |
|
82
|
|
|
*/ |
|
83
|
36 |
|
public static function getSupportedSRIDs(): array |
|
84
|
|
|
{ |
|
85
|
36 |
|
return array_map(fn ($supportedSrid) => $supportedSrid['name'], self::$sridData); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array<string, array{name: string, help: string}> |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public static function getSupportedSRIDsWithHelp(): array |
|
92
|
|
|
{ |
|
93
|
9 |
|
return array_map(fn (array $data) => [ |
|
94
|
9 |
|
'name' => $data['name'], |
|
95
|
9 |
|
'help' => $data['help'], |
|
96
|
9 |
|
], static::$sridData); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
18 |
|
public function __toString(): string |
|
100
|
|
|
{ |
|
101
|
18 |
|
return (string) $this->getValue(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|