Position::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 4
c 3
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.6111
cc 5
nc 3
nop 3
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Geometry\Position;
6
7
use PrinsFrank\PhpGeoSVG\Exception\InvalidPositionException;
8
9
class Position
10
{
11
    public const MIN_LONGITUDE       = -180;
12
    public const MAX_LONGITUDE       = 180;
13
    public const LONGITUDE_THRESHOLD = 1;
14
    public const TOTAL_LONGITUDE     = self::MAX_LONGITUDE - self::MIN_LONGITUDE;
15
16
    public const MIN_LATITUDE       = -90;
17
    public const MAX_LATITUDE       = 90;
18
    public const LATITUDE_THRESHOLD = 1;
19
    public const TOTAL_LATITUDE     = self::MAX_LATITUDE - self::MIN_LATITUDE;
20
21
    /**
22
     * The coordinate reference system for all GeoJSON coordinates is a
23
     * geographic coordinate reference system, using the World Geodetic
24
     * System 1984 (WGS 84) [WGS84] datum, with longitude and latitude units
25
     * of decimal degrees.  This is equivalent to the coordinate reference
26
     * system identified by the Open Geospatial Consortium (OGC) URN
27
     * urn:ogc:def:crs:OGC::CRS84.  An OPTIONAL third-position element SHALL
28
     * be the height in meters above or below the WGS 84 reference
29
     * ellipsoid.  In the absence of elevation values, applications
30
     * sensitive to height or depth SHOULD interpret positions as being at
31
     * local ground or sea level
32
     *
33
     * @throws InvalidPositionException
34
     */
35 6
    public function __construct(public float $longitude, public float $latitude, public ?float $elevation = null)
36
    {
37 6
        if ($this->longitude > (static::MAX_LONGITUDE + static::LONGITUDE_THRESHOLD) || $this->longitude < (static::MIN_LONGITUDE - static::LONGITUDE_THRESHOLD)) {
38 2
            throw new InvalidPositionException('The longitude should be between ' . static::MIN_LONGITUDE . ' and ' . static::MAX_LONGITUDE . ', "' . $this->longitude . '" provided.');
39
        }
40
41 4
        if ($this->latitude > (static::MAX_LATITUDE + static::LATITUDE_THRESHOLD) || $this->latitude < (static::MIN_LATITUDE - static::LATITUDE_THRESHOLD)) {
42 2
            throw new InvalidPositionException('The latitude should be between ' . static::MIN_LATITUDE . ' and ' . static::MAX_LATITUDE . ', "' . $this->latitude . '" provided.');
43
        }
44
    }
45
46 1
    public function hasElevation(): bool
47
    {
48 1
        return null !== $this->elevation && 0.0 !== $this->elevation;
49
    }
50
}
51