Completed
Pull Request — master (#52)
by
unknown
01:43 queued 13s
created

Coordinate::isNumericInBounds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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