Passed
Push — main ( fff382...94723e )
by Frank
03:40 queued 01:49
created

BoundingBox::getWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Geometry\BoundingBox;
6
7
use PrinsFrank\PhpGeoSVG\Exception\InvalidBoundingBoxException;
8
use PrinsFrank\PhpGeoSVG\Geometry\Position\Position;
9
use PrinsFrank\PhpGeoSVG\Projection\Projection;
10
11
class BoundingBox
12
{
13
    /**
14
     * @throws InvalidBoundingBoxException
15
     */
16 6
    public function __construct(public BoundingBoxPosition $southWestern, public BoundingBoxPosition $northEastern)
17
    {
18 6
        if ($this->southWestern->longitude > Position::MAX_LONGITUDE) {
19 1
            throw new InvalidBoundingBoxException(
20 1
                'The bounding box is unnecessarily rotated. Use a minLongitude of "' . (($this->southWestern->longitude + 180) % 360 - 180) . '" instead to achieve the same bound.'
21
            );
22
        }
23
24 5
        if ($this->northEastern->longitude < Position::MIN_LONGITUDE) {
25 1
            throw new InvalidBoundingBoxException(
26 1
                'The bounding box is unnecessarily rotated. Use a maxLongitude of "' . (($this->northEastern->longitude - 180) % 360 + 180) . '" instead to achieve the same bound.'
27
            );
28
        }
29
30 4
        if ($this->northEastern->latitude < $this->southWestern->latitude) {
31 1
            throw new InvalidBoundingBoxException(
32 1
                'The latitude of the NorthEastern coordinate (' . $this->northEastern->latitude . ') is south of the SouthWestern coordinate (' . $this->southWestern->latitude . ')'
33
            );
34
        }
35
36 3
        if ($this->northEastern->longitude < $this->southWestern->longitude) {
37 1
            throw new InvalidBoundingBoxException(
38 1
                'The longitude of the NorthEastern coordinate (' . $this->northEastern->longitude . ') is west of the SouthWestern coordinate (' . $this->southWestern->longitude . ')'
39
            );
40
        }
41 2
    }
42
43 1
    public function getWidth(): float
44
    {
45 1
        return - $this->southWestern->longitude + $this->northEastern->longitude;
46
    }
47
48 1
    public function getHeight(): float
49
    {
50 1
        return - $this->southWestern->latitude + $this->northEastern->latitude;
51
    }
52
53 1
    public function boundX(Position $position, Projection $projection): float
54
    {
55 1
        return $projection->getX($position)
56 1
            - ($projection->getX(new Position($this->southWestern->longitude, 0))
57 1
            - $projection->getX(new Position(Position::MIN_LONGITUDE, 0)));
58
    }
59
60 1
    public function boundY(Position $position, Projection $projection): float
61
    {
62 1
        return $projection->getY($position)
63 1
            - ($projection->getY(new Position(0, $this->northEastern->latitude))
64 1
            - $projection->getY(new Position(0, $projection->getMaxLatitude())));
65
    }
66
}
67