1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCoord. |
4
|
|
|
* |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace PHPCoord\UnitOfMeasure\Time; |
10
|
|
|
|
11
|
|
|
use PHPCoord\Exception\UnknownUnitOfMeasureException; |
12
|
|
|
use PHPCoord\UnitOfMeasure\UnitOfMeasure; |
13
|
|
|
|
14
|
|
|
abstract class Time implements UnitOfMeasure |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Year. |
18
|
|
|
*/ |
19
|
|
|
public const EPSG_YEAR = 'urn:ogc:def:uom:EPSG::1029'; |
20
|
|
|
|
21
|
|
|
protected static array $sridData = [ |
22
|
|
|
'urn:ogc:def:uom:EPSG::1029' => [ |
23
|
|
|
'name' => 'year', |
24
|
|
|
], |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected static array $customSridData = []; |
28
|
|
|
|
29
|
|
|
private static array $supportedCache = []; |
30
|
|
|
|
31
|
|
|
abstract public function __construct(float $time); |
32
|
|
|
|
33
|
|
|
abstract public function asYears(): Year; |
34
|
|
|
|
35
|
9 |
|
public function add(self $unit): self |
36
|
|
|
{ |
37
|
9 |
|
if ($this::class === $unit::class) { |
38
|
9 |
|
return new static($this->getValue() + $unit->getValue()); |
39
|
|
|
} |
40
|
|
|
$resultAsYears = new Year($this->asYears()->getValue() + $unit->asYears()->getValue()); |
41
|
|
|
$conversionRatio = (new static(1))->asYears()->getValue(); |
42
|
|
|
|
43
|
|
|
return new static($resultAsYears->getValue() / $conversionRatio); |
44
|
|
|
} |
45
|
|
|
|
46
|
45 |
|
public function subtract(self $unit): self |
47
|
|
|
{ |
48
|
45 |
|
if ($this::class === $unit::class) { |
49
|
45 |
|
return new static($this->getValue() - $unit->getValue()); |
50
|
|
|
} |
51
|
|
|
$resultAsYears = new Year($this->asYears()->getValue() - $unit->asYears()->getValue()); |
52
|
|
|
$conversionRatio = (new static(1))->asYears()->getValue(); |
53
|
|
|
|
54
|
|
|
return new static($resultAsYears->getValue() / $conversionRatio); |
55
|
|
|
} |
56
|
|
|
|
57
|
9 |
|
public function multiply(float $multiplicand): self |
58
|
|
|
{ |
59
|
9 |
|
return new static($this->getValue() * $multiplicand); |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
public function divide(float $divisor): self |
63
|
|
|
{ |
64
|
9 |
|
return new static($this->getValue() / $divisor); |
65
|
|
|
} |
66
|
|
|
|
67
|
45 |
|
public static function makeUnit(float $measurement, string $srid): self |
68
|
|
|
{ |
69
|
45 |
|
if (isset(self::$customSridData[$srid])) { |
70
|
|
|
return new self::$customSridData[$srid]['fqcn']($measurement); |
71
|
|
|
} |
72
|
|
|
|
73
|
45 |
|
return match ($srid) { |
74
|
45 |
|
self::EPSG_YEAR => new Year($measurement), |
75
|
45 |
|
default => throw new UnknownUnitOfMeasureException($srid), |
76
|
45 |
|
}; |
77
|
|
|
} |
78
|
|
|
|
79
|
27 |
|
public static function getSupportedSRIDs(): array |
80
|
|
|
{ |
81
|
27 |
|
if (!self::$supportedCache) { |
|
|
|
|
82
|
|
|
foreach (static::$sridData as $srid => $data) { |
83
|
|
|
self::$supportedCache[$srid] = $data['name']; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
27 |
|
return self::$supportedCache; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function registerCustomUnit(string $srid, string $name, string $implementingClassFQCN): void |
91
|
|
|
{ |
92
|
|
|
self::$customSridData[$srid] = ['name' => $name, 'fqcn' => $implementingClassFQCN]; |
93
|
|
|
self::getSupportedSRIDs(); // init cache if not already |
94
|
|
|
self::$supportedCache[$srid] = $name; // update cache |
95
|
|
|
} |
96
|
|
|
|
97
|
18 |
|
public function __toString(): string |
98
|
|
|
{ |
99
|
18 |
|
return (string) $this->getValue(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.