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