1
|
|
|
<?php |
2
|
|
|
namespace Ballen\Distical\Entities; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Distical |
6
|
|
|
* |
7
|
|
|
* Distical is a simple distance calculator library for PHP 5.3+ which |
8
|
|
|
* amongst other things can calculate the distance between two or more lat/long |
9
|
|
|
* coordinates. |
10
|
|
|
* |
11
|
|
|
* @author Bobby Allen <[email protected]> |
12
|
|
|
* @license http://opensource.org/licenses/MIT |
13
|
|
|
* @link https://github.com/allebb/distical |
14
|
|
|
* @link http://bobbyallen.me |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class LatLong |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Validation regex for Latitude parameters. |
22
|
|
|
*/ |
23
|
|
|
const LAT_VALIDATION_REGEX = "/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/"; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Validation regex for Longitude parameters. |
27
|
|
|
*/ |
28
|
|
|
const LNG_VALIDATION_REGEX = "/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The latitude coordinate. |
32
|
|
|
* @var double |
33
|
|
|
*/ |
34
|
|
|
protected $latitude; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The longitude coordinate. |
38
|
|
|
* @var double |
39
|
|
|
*/ |
40
|
|
|
protected $longitude; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new latitude and longitude object. |
44
|
|
|
* @param double $lat The latitude coordinate. |
45
|
|
|
* @param double $lng The longitude coordinate. |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
*/ |
48
|
31 |
|
public function __construct($lat, $lng) |
49
|
|
|
{ |
50
|
31 |
|
$this->latitude = $lat; |
51
|
31 |
|
$this->longitude = $lng; |
52
|
31 |
|
if (!$this->validateLat()) { |
53
|
2 |
|
throw new \Ballen\Distical\Exceptions\InvalidLatitudeFormatException('The latitude parameter is invalid, value must be between -90 and 90'); |
54
|
|
|
} |
55
|
31 |
|
if (!$this->validateLng()) { |
56
|
1 |
|
throw new \Ballen\Distical\Exceptions\InvalidLongitudeFormatException('The longitude parameter is invalid, value must be between -180 and 180'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Validates the Latitude value. |
62
|
|
|
* |
63
|
|
|
* @return boolean |
64
|
|
|
*/ |
65
|
31 |
|
private function validateLat() |
66
|
|
|
{ |
67
|
31 |
|
if (($this->latitude >= -90) && ($this->latitude <= 90)) { |
68
|
31 |
|
return true; |
69
|
|
|
} |
70
|
2 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Validates the Longitude value. |
75
|
|
|
* @return boolean True if validation passes. |
76
|
|
|
*/ |
77
|
31 |
|
private function validateLng() |
78
|
|
|
{ |
79
|
31 |
|
if (($this->longitude >= -180) && ($this->longitude <= 180)) { |
80
|
31 |
|
return true; |
81
|
|
|
} |
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the current Latitude coordinate. |
87
|
|
|
* @return double |
88
|
|
|
*/ |
89
|
20 |
|
public function getLatitude() |
90
|
|
|
{ |
91
|
20 |
|
return $this->latitude; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the current Longitude coordinate. |
96
|
|
|
* @return double |
97
|
|
|
*/ |
98
|
20 |
|
public function getLongitude() |
99
|
|
|
{ |
100
|
20 |
|
return $this->longitude; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Alias of getLatitude |
105
|
|
|
* @return double |
106
|
|
|
*/ |
107
|
19 |
|
public function lat() |
108
|
|
|
{ |
109
|
19 |
|
return $this->getLatitude(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Alias of getLongitude |
114
|
|
|
* @return double |
115
|
|
|
*/ |
116
|
19 |
|
public function lng() |
117
|
|
|
{ |
118
|
19 |
|
return $this->getLongitude(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|