1 | <?php |
||
22 | class Coordinate implements CoordinateInterface, \JsonSerializable |
||
23 | { |
||
24 | /** |
||
25 | * The latitude of the coordinate. |
||
26 | * |
||
27 | * @var double |
||
28 | */ |
||
29 | protected $latitude; |
||
30 | |||
31 | /** |
||
32 | * The longitude of the coordinate. |
||
33 | * |
||
34 | * @var double |
||
35 | */ |
||
36 | protected $longitude; |
||
37 | |||
38 | /** |
||
39 | * The selected ellipsoid. |
||
40 | * |
||
41 | * @var Ellipsoid |
||
42 | */ |
||
43 | protected $ellipsoid; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * The precision to use to compare big numbers |
||
48 | * |
||
49 | * @var integer |
||
50 | */ |
||
51 | private $precision = 8; |
||
52 | |||
53 | /** |
||
54 | 231 | * Set the latitude and the longitude of the coordinates into an selected ellipsoid. |
|
55 | * |
||
56 | 231 | * @param Address|array|string $coordinates The coordinates. |
|
57 | 37 | * @param Ellipsoid $ellipsoid The selected ellipsoid (WGS84 by default). |
|
58 | 37 | * |
|
59 | 231 | * @throws InvalidArgumentException |
|
60 | 41 | */ |
|
61 | 41 | public function __construct($coordinates, Ellipsoid $ellipsoid = null) |
|
62 | 194 | { |
|
63 | 154 | if ($coordinates instanceof Address) { |
|
64 | 128 | $this->setLatitude($coordinates->getLatitude()); |
|
65 | 5 | $this->setLongitude($coordinates->getLongitude()); |
|
66 | } elseif (is_array($coordinates) && 2 === count($coordinates)) { |
||
67 | 5 | $this->setLatitude($coordinates[0]); |
|
68 | $this->setLongitude($coordinates[1]); |
||
69 | } elseif (is_string($coordinates)) { |
||
70 | 200 | $this->setFromString($coordinates); |
|
71 | 200 | } else { |
|
72 | throw new InvalidArgumentException( |
||
73 | 'It should be a string, an array or a class which implements Geocoder\Model\Address !' |
||
74 | ); |
||
75 | } |
||
76 | 200 | ||
77 | $this->ellipsoid = $ellipsoid ?: Ellipsoid::createFromName(Ellipsoid::WGS84); |
||
78 | 200 | } |
|
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public function normalizeLatitude($latitude) |
||
87 | 3 | ||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | 198 | */ |
|
91 | 198 | public function normalizeLongitude($longitude) |
|
92 | { |
||
93 | 198 | if (180 === $longitude % 360) { |
|
94 | return 180.0; |
||
95 | } |
||
96 | |||
97 | $mod = fmod($longitude, 360); |
||
98 | $longitude = $mod < -180 ? $mod + 360 : ($mod > 180 ? $mod - 360 : $mod); |
||
99 | 200 | ||
100 | return (double) $longitude; |
||
101 | 200 | } |
|
102 | 200 | ||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | public function setLatitude($latitude) |
||
107 | 172 | { |
|
108 | $this->latitude = $this->normalizeLatitude($latitude); |
||
109 | 172 | } |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | public function getLatitude() |
||
118 | 200 | ||
119 | /** |
||
120 | * {@inheritDoc} |
||
121 | */ |
||
122 | public function setLongitude($longitude) |
||
123 | 172 | { |
|
124 | $this->longitude = $this->normalizeLongitude($longitude); |
||
125 | 172 | } |
|
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | public function getLongitude() |
||
134 | |||
135 | /** |
||
136 | * {@inheritDoc} |
||
137 | */ |
||
138 | public function getEllipsoid() |
||
142 | |||
143 | 157 | /** |
|
144 | * Creates a valid and acceptable geographic coordinates. |
||
145 | 157 | * |
|
146 | 1 | * @param string $coordinates |
|
147 | * |
||
148 | * @throws InvalidArgumentException |
||
149 | */ |
||
150 | 156 | public function setFromString($coordinates) |
|
164 | |||
165 | /** |
||
166 | * @return integer |
||
167 | */ |
||
168 | public function getPrecision() |
||
172 | 156 | ||
173 | 45 | /** |
|
174 | * @param integer $precision |
||
175 | * @return $this |
||
176 | */ |
||
177 | 111 | public function setPrecision($precision) |
|
183 | 55 | ||
184 | 55 | ||
185 | /** |
||
186 | * Converts a valid and acceptable geographic coordinates to decimal degrees coordinate. |
||
187 | * |
||
188 | 94 | * @param string $coordinates A valid and acceptable geographic coordinates. |
|
189 | * |
||
190 | 6 | * @return array An array of coordinate in decimal degree. |
|
191 | 6 | * |
|
192 | 6 | * @throws InvalidArgumentException |
|
193 | * |
||
194 | * @see http://en.wikipedia.org/wiki/Geographic_coordinate_conversion |
||
195 | */ |
||
196 | private function toDecimalDegrees($coordinates) |
||
254 | |||
255 | /** |
||
256 | * {@inheritDoc} |
||
257 | */ |
||
258 | public function jsonSerialize() |
||
262 | |||
263 | /** |
||
264 | * Returns a boolean determining coordinates equality |
||
265 | * @param Coordinate $coordinate |
||
266 | * @return boolean |
||
267 | */ |
||
268 | public function isEqual(Coordinate $coordinate) { |
||
271 | } |
||
272 |