1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wolnosciowiec\CoordinatesBundle\Model\Entity\Coordinates; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Coordinates |
7
|
|
|
* =========== |
8
|
|
|
* |
9
|
|
|
* @package Wolnosciowiec\CoordinatesBundle\Model\Entity\Coordinates |
10
|
|
|
*/ |
11
|
|
|
class Coordinates |
12
|
|
|
{ |
13
|
|
|
/** @var float $lat */ |
14
|
|
|
protected $lat; |
15
|
|
|
|
16
|
|
|
/** @var float $lon */ |
17
|
|
|
protected $lon; |
18
|
|
|
|
19
|
|
|
/** @var float $distance */ |
20
|
|
|
protected $distance = 0.0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param float $lat Latitude |
24
|
|
|
* @param float $lon Longitude |
25
|
|
|
* @param int $distance Defaults to 20 km |
26
|
|
|
*/ |
27
|
|
|
public function __construct($lat, $lon, $distance = 20) |
28
|
|
|
{ |
29
|
|
|
if (!is_float($lat) || !is_float($lon) || $lat <= -90.0 || $lon <= -90.0) { |
30
|
|
|
throw new \InvalidArgumentException('$lat and $lon should be float, and in acceptable range, got $lat = ' . $lat . ', $lon = ' . $lon); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->lat = $lat; |
34
|
|
|
$this->lon = $lon; |
35
|
|
|
$this->distance = $distance; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return float |
40
|
|
|
*/ |
41
|
|
|
public function getLatitude() |
42
|
|
|
{ |
43
|
|
|
return $this->lat; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return float |
48
|
|
|
*/ |
49
|
|
|
public function getLongitude() |
50
|
|
|
{ |
51
|
|
|
return $this->lon; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Coordinates $coordinates |
56
|
|
|
* @return Distance |
57
|
|
|
*/ |
58
|
|
|
public function compareWith(Coordinates $coordinates) |
59
|
|
|
{ |
60
|
|
|
return new Distance( |
61
|
|
|
$this->getLatitude(), |
62
|
|
|
$this->getLongitude(), |
63
|
|
|
|
64
|
|
|
$coordinates->getLatitude(), |
65
|
|
|
$coordinates->getLongitude() |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return BoundingBox |
71
|
|
|
*/ |
72
|
|
|
public function getBoundingSquare() |
73
|
|
|
{ |
74
|
|
|
return new BoundingBox( |
75
|
|
|
$this->getLatitude(), |
76
|
|
|
$this->getLongitude(), |
77
|
|
|
$this->getMaxAcceptedDistance() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return float|int |
83
|
|
|
*/ |
84
|
|
|
public function getMaxAcceptedDistance() |
85
|
|
|
{ |
86
|
|
|
return $this->distance; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return float|int |
91
|
|
|
*/ |
92
|
|
|
public function getDistanceInMeters() |
93
|
|
|
{ |
94
|
|
|
return $this->getMaxAcceptedDistance() * 1000; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return float|int |
99
|
|
|
*/ |
100
|
|
|
public function getLongtitudeUnit() |
101
|
|
|
{ |
102
|
|
|
return 2 * M_PI * 6371000 / 360; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return float|int |
107
|
|
|
*/ |
108
|
|
|
public function getLatitudeInMeters() |
109
|
|
|
{ |
110
|
|
|
return (1 - $this->getLatitude() / 90) * 2 *M_PI * 6371000 / 360; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param float $distance |
115
|
|
|
*/ |
116
|
|
|
public function setMaxAcceptedDistance(float $distance) |
117
|
|
|
{ |
118
|
|
|
$this->distance = $distance; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.