Completed
Push — master ( ffb45a...dc303d )
by Marcus
02:12
created

Coordinate::getPoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Coordinate Implementation
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\Distance\DistanceInterface;
16
use Location\Formatter\Coordinate\FormatterInterface;
17
18
/**
19
 * Coordinate Implementation
20
 *
21
 * @category Location
22
 * @author   Marcus Jaschen <[email protected]>
23
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
24
 * @link     https://github.com/mjaschen/phpgeo
25
 */
26
class Coordinate implements GeometryInterface
27
{
28
    /**
29
     * @var float
30
     */
31
    protected $lat;
32
33
    /**
34
     * @var float
35
     */
36
    protected $lng;
37
38
    /**
39
     * @var Ellipsoid
40
     */
41
    protected $ellipsoid;
42
43
    /**
44
     * @param float $lat           -90.0 .. +90.0
45
     * @param float $lng           -180.0 .. +180.0
46
     * @param Ellipsoid $ellipsoid if omitted, WGS-84 is used
47
     *
48
     * @throws \InvalidArgumentException
49
     */
50
    public function __construct($lat, $lng, Ellipsoid $ellipsoid = null)
51
    {
52
        if (! $this->isValidLatitude($lat)) {
53
            throw new \InvalidArgumentException("Latitude value must be numeric -90.0 .. +90.0 (given: {$lat})");
54
        }
55
56
        if (! $this->isValidLongitude($lng)) {
57
            throw new \InvalidArgumentException("Longitude value must be numeric -180.0 .. +180.0 (given: {$lng})");
58
        }
59
60
        $this->lat = doubleval($lat);
61
        $this->lng = doubleval($lng);
62
63
        if ($ellipsoid !== null) {
64
            $this->ellipsoid = $ellipsoid;
65
        } else {
66
            $this->ellipsoid = Ellipsoid::createDefault();
67
        }
68
    }
69
70
    /**
71
     * @return float
72
     */
73
    public function getLat()
74
    {
75
        return $this->lat;
76
    }
77
78
    /**
79
     * @return float
80
     */
81
    public function getLng()
82
    {
83
        return $this->lng;
84
    }
85
86
    /**
87
     * Returns an array containing the point
88
     *
89
     * @return array
90
     */
91
    public function getPoints()
92
    {
93
        return [$this];
94
    }
95
96
    /**
97
     * @return Ellipsoid
98
     */
99
    public function getEllipsoid()
100
    {
101
        return $this->ellipsoid;
102
    }
103
104
    /**
105
     * Calculates the distance between the given coordinate
106
     * and this coordinate.
107
     *
108
     * @param Coordinate $coordinate
109
     * @param DistanceInterface $calculator instance of distance calculation class
110
     *
111
     * @return float
112
     */
113
    public function getDistance(Coordinate $coordinate, DistanceInterface $calculator)
114
    {
115
        return $calculator->getDistance($this, $coordinate);
116
    }
117
118
    /**
119
     * @param FormatterInterface $formatter
120
     *
121
     * @return mixed
122
     */
123
    public function format(FormatterInterface $formatter)
124
    {
125
        return $formatter->format($this);
126
    }
127
128
    /**
129
     * Validates latitude
130
     *
131
     * @param mixed $latitude
132
     *
133
     * @return bool
134
     */
135
    protected function isValidLatitude($latitude)
136
    {
137
        return $this->isNumericInBounds($latitude, - 90.0, 90.0);
138
    }
139
140
    /**
141
     * Validates longitude
142
     *
143
     * @param mixed $longitude
144
     *
145
     * @return bool
146
     */
147
    protected function isValidLongitude($longitude)
148
    {
149
        return $this->isNumericInBounds($longitude, - 180.0, 180.0);
150
    }
151
152
    /**
153
     * Checks if the given value is (1) numeric, and (2) between lower
154
     * and upper bounds (including the bounds values).
155
     *
156
     * @param float $value
157
     * @param float $lower
158
     * @param float $upper
159
     *
160
     * @return bool
161
     */
162
    protected function isNumericInBounds($value, $lower, $upper)
163
    {
164
        if (! is_numeric($value)) {
165
            return false;
166
        }
167
168
        if ($value < $lower || $value > $upper) {
169
            return false;
170
        }
171
172
        return true;
173
    }
174
}
175