Completed
Push — milestone/4.0 ( 4a7219...1803cd )
by Marcus
01:48
created

Bounds::getCenterLng()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpgeo;
6
7
use InvalidArgumentException;
8
9
/**
10
 * Bounds Class.
11
 *
12
 * @author Marcus Jaschen <[email protected]>
13
 */
14
class Bounds
15
{
16
    /**
17
     * @var Point
18
     */
19
    protected $northWest;
20
21
    /**
22
     * @var Point
23
     */
24
    protected $southEast;
25
26
    /**
27
     * @param Point $northWest
28
     * @param Point $southEast
29
     */
30
    public function __construct(Point $northWest, Point $southEast)
31
    {
32
        $this->northWest = $northWest;
33
        $this->southEast = $southEast;
34
    }
35
36
    /**
37
     * Getter
38
     *
39
     * @return Point
40
     */
41
    public function getNorthWest(): Point
42
    {
43
        return $this->northWest;
44
    }
45
46
    /**
47
     * Getter
48
     *
49
     * @return Point
50
     */
51
    public function getSouthEast(): Point
52
    {
53
        return $this->southEast;
54
    }
55
56
    /**
57
     * @return float
58
     */
59
    public function getNorth(): float
60
    {
61
        return $this->northWest->getLat();
62
    }
63
64
    /**
65
     * @return float
66
     */
67
    public function getSouth(): float
68
    {
69
        return $this->southEast->getLat();
70
    }
71
72
    /**
73
     * @return float
74
     */
75
    public function getWest(): float
76
    {
77
        return $this->northWest->getLng();
78
    }
79
80
    /**
81
     * @return float
82
     */
83
    public function getEast(): float
84
    {
85
        return $this->southEast->getLng();
86
    }
87
88
    /**
89
     * Calculates the center of this bounds object and returns it as a
90
     * Point instance.
91
     *
92
     * @return Point
93
     * @throws InvalidArgumentException
94
     */
95
    public function getCenter(): Point
96
    {
97
        $centerLat = ($this->getNorth() + $this->getSouth()) / 2;
98
99
        return new Point($centerLat, $this->getCenterLng());
100
    }
101
102
    /**
103
     * @return float
104
     */
105
    protected function getCenterLng(): float
106
    {
107
        $centerLng = ($this->getEast() + $this->getWest()) / 2;
108
109
        $overlap = $this->getWest() > 0 && $this->getEast() < 0;
110
111
        if ($overlap && $centerLng > 0) {
112
            return -180.0 + $centerLng;
113
        }
114
115
        if ($overlap && $centerLng < 0) {
116
            return 180.0 + $centerLng;
117
        }
118
119
        if ($overlap && $centerLng == 0) {
120
            return 180.0;
121
        }
122
123
        return $centerLng;
124
    }
125
}
126