Passed
Push — master ( 1ad806...2634d1 )
by Doug
63:26
created

ProjectedBase::getDerivingConversion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
/**
23
 * @internal use all methods and constants directly from the Projected class e.g. Projected::EPSG_*, Projected::fromSRID()
24
 * This will be removed and folded back into the main class once support for PHP <8.2 is dropped
25
 */
26
class ProjectedBase extends CoordinateReferenceSystem
27
{
28
    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...
29
30
    protected Geographic2D|Geographic3D $baseCRS;
31
32
    protected ?string $derivingConversion;
33
34
    /**
35
     * @var array<string, Projected>
36
     */
37
    private static array $cachedObjects = [
38
    ];
39
40
    public function __construct(string $srid, CoordinateSystem $coordinateSystem, Datum $datum, BoundingArea $boundingArea, string $name = '', Geographic2D|Geographic3D|null $baseCRS = null, ?string $derivingConversion = null)
41
    {
42
        $this->srid = $srid;
43
        $this->coordinateSystem = $coordinateSystem;
44
        $this->datum = $datum;
45
        $this->boundingArea = $boundingArea;
46
        $this->name = $name;
47
        $this->baseCRS = $baseCRS ?? Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84);
48
        $this->derivingConversion = $derivingConversion;
49
        assert(count($coordinateSystem->getAxes()) === 2 || count($coordinateSystem->getAxes()) === 3);
50
    }
51
52
    public function getBaseCRS(): Geographic2D|Geographic3D
53
    {
54
        return $this->baseCRS;
55
    }
56
57
    public function getDerivingConversion(): string
58
    {
59
        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...
60
    }
61
62
    public static function fromSRID(string $srid): Projected
63
    {
64
        if (!isset(static::$sridData[$srid])) {
65
            throw new UnknownCoordinateReferenceSystemException($srid);
66
        }
67
        if (!isset(self::$cachedObjects[$srid])) {
68
            $data = static::$sridData[$srid];
69
            $baseCRS = Geographic::fromSRID($data['base_crs']);
70
            $extent = $data['extent'] instanceof BoundingArea ? $data['extent'] : BoundingArea::createFromExtentCodes($data['extent']);
71
            self::$cachedObjects[$srid] = new Projected($srid, Cartesian::fromSRID($data['coordinate_system']), $baseCRS->getDatum(), $extent, $data['name'], $baseCRS, $data['deriving_conversion']);
72
        }
73
74
        return self::$cachedObjects[$srid];
75
    }
76
77
    /**
78
     * @return array<string, string>
79
     */
80
    public static function getSupportedSRIDs(): array
81
    {
82
        return array_map(fn (array $data) => $data['name'], static::$sridData);
83
    }
84
85
    /**
86
     * @return array<string, array{name: string, extent_description: string, help: string}>
87
     */
88
    public static function getSupportedSRIDsWithHelp(): array
89
    {
90
        return array_map(fn (array $data) => [
91
            'name' => $data['name'],
92
            'extent_description' => $data['name'],
93
            'help' => $data['help'],
94
        ], static::$sridData);
95
    }
96
97
    public static function registerCustomCRS(string $srid, string $name, string $baseCRSSrid, string $derivingConversionSrid, string $coordinateSystemSrid, BoundingArea $extent, string $help = ''): void
98
    {
99
        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...
100
            'name' => $name,
101
            'coordinate_system' => $coordinateSystemSrid,
102
            'base_crs' => $baseCRSSrid,
103
            'deriving_conversion' => $derivingConversionSrid,
104
            'extent' => $extent,
105
            'extent_description' => '',
106
            'help' => $help,
107
        ];
108
    }
109
}
110