Passed
Push — master ( 8590ae...ac39f2 )
by Doug
62:11
created

ProjectedBase::getSupportedSRIDsWithHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * PHPCoord.
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace PHPCoord\CoordinateReferenceSystem;
11
12
use PHPCoord\CoordinateSystem\Cartesian;
13
use PHPCoord\CoordinateSystem\CoordinateSystem;
14
use PHPCoord\Datum\Datum;
15
use PHPCoord\Exception\UnknownCoordinateReferenceSystemException;
16
use PHPCoord\Geometry\BoundingArea;
17
18
use function assert;
19
use function count;
20
use function array_map;
21
22
class ProjectedBase extends CoordinateReferenceSystem
23
{
24
    use ProjectedSRIDData;
0 ignored issues
show
Bug introduced by
The type PHPCoord\CoordinateRefer...ystem\ProjectedSRIDData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
    protected Geographic2D|Geographic3D $baseCRS;
27
28
    protected ?string $derivingConversion;
29
30
    /**
31
     * @var array<string, Projected>
32
     */
33
    private static array $cachedObjects = [
34
    ];
35
36
    public function __construct(string $srid, CoordinateSystem $coordinateSystem, Datum $datum, BoundingArea $boundingArea, string $name = '', Geographic2D|Geographic3D|null $baseCRS = null, ?string $derivingConversion = null)
37
    {
38
        $this->srid = $srid;
39
        $this->coordinateSystem = $coordinateSystem;
40
        $this->datum = $datum;
41
        $this->boundingArea = $boundingArea;
42
        $this->name = $name;
43
        $this->baseCRS = $baseCRS ?? Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84);
44
        $this->derivingConversion = $derivingConversion;
45
        assert(count($coordinateSystem->getAxes()) === 2 || count($coordinateSystem->getAxes()) === 3);
46
    }
47
48
    public function getBaseCRS(): Geographic2D|Geographic3D
49
    {
50
        return $this->baseCRS;
51
    }
52
53
    public function getDerivingConversion(): string
54
    {
55
        return $this->derivingConversion;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->derivingConversion could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58
    public static function fromSRID(string $srid): Projected
59
    {
60
        if (!isset(static::$sridData[$srid])) {
61
            throw new UnknownCoordinateReferenceSystemException($srid);
62
        }
63
        if (!isset(self::$cachedObjects[$srid])) {
64
            $data = static::$sridData[$srid];
65
            $baseCRS = Geographic::fromSRID($data['base_crs']);
66
            $extent = $data['extent'] instanceof BoundingArea ? $data['extent'] : BoundingArea::createFromExtentCodes($data['extent']);
67
            self::$cachedObjects[$srid] = new Projected($srid, Cartesian::fromSRID($data['coordinate_system']), $baseCRS->getDatum(), $extent, $data['name'], $baseCRS, $data['deriving_conversion']);
68
        }
69
70
        return self::$cachedObjects[$srid];
71
    }
72
73
    /**
74
     * @return array<string, string>
75
     */
76
    public static function getSupportedSRIDs(): array
77
    {
78
        return array_map(fn (array $data) => $data['name'], static::$sridData);
79
    }
80
81
    /**
82
     * @return array<string, array{name: string, extent_description: string, help: string}>
83
     */
84
    public static function getSupportedSRIDsWithHelp(): array
85
    {
86
        return array_map(fn (array $data) => [
87
            'name' => $data['name'],
88
            'extent_description' => $data['name'],
89
            'help' => $data['help'],
90
        ], static::$sridData);
91
    }
92
93
    public static function registerCustomCRS(string $srid, string $name, string $baseCRSSrid, string $derivingConversionSrid, string $coordinateSystemSrid, BoundingArea $extent, string $help = ''): void
94
    {
95
        self::$sridData[$srid] = [
0 ignored issues
show
Bug Best Practice introduced by
The property sridData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
            'name' => $name,
97
            'coordinate_system' => $coordinateSystemSrid,
98
            'base_crs' => $baseCRSSrid,
99
            'deriving_conversion' => $derivingConversionSrid,
100
            'extent' => $extent,
101
            'extent_description' => '',
102
            'help' => $help,
103
        ];
104
    }
105
}
106