Completed
Push — develop-1.0 ( 836cf2...7f9527 )
by Marcus
01:49
created

Bounds   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 111
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNorthWest() 0 4 1
A getSouthEast() 0 4 1
A getNorth() 0 4 1
A getSouth() 0 4 1
A getWest() 0 4 1
A getEast() 0 4 1
A getCenter() 0 6 1
B getCenterLng() 0 20 8
1
<?php
2
/**
3
 * Coordinate Bounds Class
4
 *
5
 * PHP version 5.3
6
 *
7
 * @category  Location
8
 * @author    Marcus Jaschen <[email protected]>
9
 * @license   https://opensource.org/licenses/GPL-3.0 GPL
10
 * @link      https://github.com/mjaschen/phpgeo
11
 */
12
13
namespace Location;
14
15
use Location\Coordinate;
16
17
/**
18
 * Coordinate Bounds Class
19
 *
20
 * @category Location
21
 * @author   Marcus Jaschen <[email protected]>
22
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
23
 * @link     https://github.com/mjaschen/phpgeo
24
 */
25
class Bounds
26
{
27
    /**
28
     * @var Coordinate
29
     */
30
    protected $northWest;
31
32
    /**
33
     * @var Coordinate
34
     */
35
    protected $southEast;
36
37
    /**
38
     * @param Coordinate $northWest
39
     * @param Coordinate $southEast
40
     */
41
    public function __construct(Coordinate $northWest, Coordinate $southEast)
42
    {
43
        $this->northWest = $northWest;
44
        $this->southEast = $southEast;
45
    }
46
47
    /**
48
     * Getter
49
     *
50
     * @return Coordinate
51
     */
52
    public function getNorthWest()
53
    {
54
        return $this->northWest;
55
    }
56
57
    /**
58
     * Getter
59
     *
60
     * @return Coordinate
61
     */
62
    public function getSouthEast()
63
    {
64
        return $this->southEast;
65
    }
66
67
    /**
68
     * @return float
69
     */
70
    public function getNorth()
71
    {
72
        return $this->northWest->getLat();
73
    }
74
75
    /**
76
     * @return float
77
     */
78
    public function getSouth()
79
    {
80
        return $this->southEast->getLat();
81
    }
82
83
    /**
84
     * @return float
85
     */
86
    public function getWest()
87
    {
88
        return $this->northWest->getLng();
89
    }
90
91
    /**
92
     * @return float
93
     */
94
    public function getEast()
95
    {
96
        return $this->southEast->getLng();
97
    }
98
99
    /**
100
     * Calculates the center of this bounds object and returns it as a
101
     * Coordinate instance.
102
     *
103
     * @return Coordinate
104
     */
105
    public function getCenter()
106
    {
107
        $centerLat = ($this->getNorth() + $this->getSouth()) / 2;
108
109
        return new Coordinate($centerLat, $this->getCenterLng());
110
    }
111
112
    /**
113
     * @return float
114
     */
115
    protected function getCenterLng()
116
    {
117
        $centerLng = ($this->getEast() + $this->getWest()) / 2;
118
119
        $overlap = $this->getWest() > 0 && $this->getEast() < 0;
120
121
        if ($overlap && $centerLng > 0) {
122
            return -180 + $centerLng;
123
        }
124
125
        if ($overlap && $centerLng < 0) {
126
            return 180 + $centerLng;
127
        }
128
129
        if ($overlap && $centerLng == 0) {
130
            return 180;
131
        }
132
133
        return $centerLng;
134
    }
135
}
136