|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCoord. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace PHPCoord\UnitOfMeasure; |
|
10
|
|
|
|
|
11
|
|
|
use PHPCoord\Exception\UnknownUnitOfMeasureException; |
|
12
|
|
|
use PHPCoord\UnitOfMeasure\Angle\Angle; |
|
13
|
|
|
use PHPCoord\UnitOfMeasure\Length\Length; |
|
14
|
|
|
use PHPCoord\UnitOfMeasure\Scale\Scale; |
|
15
|
|
|
use PHPCoord\UnitOfMeasure\Time\Time; |
|
16
|
|
|
|
|
17
|
|
|
use function array_merge; |
|
18
|
|
|
|
|
19
|
|
|
class UnitOfMeasureFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array<string, array<string, string>> |
|
23
|
|
|
*/ |
|
24
|
|
|
private static array $sridCache = []; |
|
25
|
|
|
|
|
26
|
261 |
|
public static function makeUnit($measurement, string $srid): UnitOfMeasure |
|
27
|
|
|
{ |
|
28
|
261 |
|
if (!self::$sridCache) { |
|
|
|
|
|
|
29
|
9 |
|
self::$sridCache['angle'] = Angle::getSupportedSRIDs(); |
|
30
|
9 |
|
self::$sridCache['length'] = Length::getSupportedSRIDs(); |
|
31
|
9 |
|
self::$sridCache['scale'] = Scale::getSupportedSRIDs(); |
|
32
|
9 |
|
self::$sridCache['time'] = Time::getSupportedSRIDs(); |
|
33
|
9 |
|
self::$sridCache['rate'] = Rate::getSupportedSRIDs(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
261 |
|
if (isset(self::$sridCache['angle'][$srid])) { |
|
37
|
242 |
|
return Angle::makeUnit($measurement, $srid); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
261 |
|
if (isset(self::$sridCache['length'][$srid])) { |
|
41
|
252 |
|
return Length::makeUnit((float) $measurement, $srid); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
197 |
|
if (isset(self::$sridCache['scale'][$srid])) { |
|
45
|
188 |
|
return Scale::makeUnit((float) $measurement, $srid); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
18 |
|
if (isset(self::$sridCache['time'][$srid])) { |
|
49
|
9 |
|
return Time::makeUnit((float) $measurement, $srid); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
if (isset(self::$sridCache['rate'][$srid])) { |
|
53
|
|
|
return Rate::makeUnit((float) $measurement, $srid); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
9 |
|
throw new UnknownUnitOfMeasureException($srid); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array<string, string> |
|
61
|
|
|
*/ |
|
62
|
9 |
|
public static function getSupportedSRIDs(): array |
|
63
|
|
|
{ |
|
64
|
9 |
|
return array_merge(Angle::getSupportedSRIDs(), Length::getSupportedSRIDs(), Scale::getSupportedSRIDs(), Time::getSupportedSRIDs(), Rate::getSupportedSRIDs()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
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.