|
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
|
|
|
* co-ordinates. |
|
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 Distance |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Converstion from Kilomters to Miles. |
|
22
|
|
|
*/ |
|
23
|
|
|
const KILOMETERS_IN_MILES = 0.621371192; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Converstion from Kilomters to Naugtical miles. |
|
27
|
|
|
*/ |
|
28
|
|
|
const KILOMETERS_INL_NAUTICAL_MILES = 0.539956803; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The distance in kilometres |
|
32
|
|
|
* @var double|int |
|
33
|
|
|
*/ |
|
34
|
|
|
private $kilometres; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Class constructor |
|
38
|
|
|
* @param mixed $kilometres The distance in kilometres. |
|
39
|
|
|
*/ |
|
40
|
27 |
|
public function __construct($kilometres = 0, $allowZero = false) |
|
41
|
|
|
{ |
|
42
|
27 |
|
$this->validateDistance($kilometres, $allowZero); |
|
43
|
27 |
|
$this->kilometres = $kilometres; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Validates the distance constructor value. |
|
48
|
|
|
* @param mixed $distance |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
27 |
|
private function validateDistance($distance, $allowZero = false) |
|
53
|
|
|
{ |
|
54
|
27 |
|
if (!is_numeric($distance)) { |
|
55
|
1 |
|
throw new \InvalidArgumentException('The distance value must be of a valid type.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
27 |
|
if (!$allowZero && $distance === 0) { |
|
59
|
1 |
|
throw new \InvalidArgumentException('The distance must be greater than zero!'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
27 |
|
if ($distance < 0) { |
|
63
|
1 |
|
throw new \InvalidArgumentException('The distance must be greater than or equals zero!'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Distance as kilometres |
|
69
|
|
|
* @return double |
|
70
|
|
|
*/ |
|
71
|
11 |
|
public function asKilometres() |
|
72
|
|
|
{ |
|
73
|
11 |
|
return $this->kilometres; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Distance as miles |
|
78
|
|
|
* @return double |
|
79
|
|
|
*/ |
|
80
|
5 |
|
public function asMiles() |
|
81
|
|
|
{ |
|
82
|
5 |
|
return $this->kilometres * self::KILOMETERS_IN_MILES; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Distance as nautical miles |
|
87
|
|
|
* @return double |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function asNauticalMiles() |
|
90
|
|
|
{ |
|
91
|
4 |
|
return $this->kilometres * self::KILOMETERS_INL_NAUTICAL_MILES; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Default __toString() method, defaults to returning the distance as kilometres. |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public function __toString() |
|
99
|
|
|
{ |
|
100
|
4 |
|
return (string) $this->asKilometres(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|