1
|
|
|
<?php namespace Arcanedev\GeoLocation\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Coordinate; |
4
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Position as PositionContract; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Position |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\GeoLocation\Entities |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Position implements PositionContract |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The latitude coordinate. |
21
|
|
|
* |
22
|
|
|
* @var \Arcanedev\GeoLocation\Contracts\Entities\Coordinate |
23
|
|
|
*/ |
24
|
|
|
protected $latitude; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The longitude coordinate. |
28
|
|
|
* |
29
|
|
|
* @var \Arcanedev\GeoLocation\Contracts\Entities\Coordinate |
30
|
|
|
*/ |
31
|
|
|
protected $longitude; |
32
|
|
|
|
33
|
|
|
/* ----------------------------------------------------------------- |
34
|
|
|
| Constructor |
35
|
|
|
| ----------------------------------------------------------------- |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Position constructor. |
40
|
|
|
* |
41
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinate $latitude |
42
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinate $longitude |
43
|
|
|
*/ |
44
|
69 |
|
public function __construct(Coordinate $latitude, Coordinate $longitude) |
45
|
|
|
{ |
46
|
69 |
|
$this->setLatitude($latitude); |
47
|
69 |
|
$this->setLongitude($longitude); |
48
|
69 |
|
} |
49
|
|
|
|
50
|
|
|
/* ----------------------------------------------------------------- |
51
|
|
|
| Getters & Setters |
52
|
|
|
| ----------------------------------------------------------------- |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the latitude coordinate. |
57
|
|
|
* |
58
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate |
59
|
|
|
*/ |
60
|
57 |
|
public function getLatitude() |
61
|
|
|
{ |
62
|
57 |
|
return $this->latitude; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the latitude coordinate (alias). |
67
|
|
|
* |
68
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate |
69
|
|
|
*/ |
70
|
39 |
|
public function lat() |
71
|
|
|
{ |
72
|
39 |
|
return $this->getLatitude(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the latitude coordinate. |
77
|
|
|
* |
78
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinate $latitude |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
69 |
|
public function setLatitude(Coordinate $latitude) |
83
|
|
|
{ |
84
|
69 |
|
$this->latitude = $latitude; |
85
|
|
|
|
86
|
69 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the longitude coordinate. |
91
|
|
|
* |
92
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate |
93
|
|
|
*/ |
94
|
57 |
|
public function getLongitude() |
95
|
|
|
{ |
96
|
57 |
|
return $this->longitude; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the longitude coordinate (alias). |
101
|
|
|
* |
102
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate |
103
|
|
|
*/ |
104
|
39 |
|
public function lng() |
105
|
|
|
{ |
106
|
39 |
|
return $this->getLongitude(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the longitude coordinate. |
111
|
|
|
* |
112
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Coordinate $longitude |
113
|
|
|
* |
114
|
|
|
* @return self |
115
|
|
|
*/ |
116
|
69 |
|
public function setLongitude(Coordinate $longitude) |
117
|
|
|
{ |
118
|
69 |
|
$this->longitude = $longitude; |
119
|
|
|
|
120
|
69 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/* ----------------------------------------------------------------- |
124
|
|
|
| Main Methods |
125
|
|
|
| ----------------------------------------------------------------- |
126
|
|
|
*/ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create the position. |
130
|
|
|
* |
131
|
|
|
* @param float $latitude |
132
|
|
|
* @param float $longitude |
133
|
|
|
* |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
42 |
|
public static function create($latitude, $longitude) |
137
|
|
|
{ |
138
|
42 |
|
return new static( |
139
|
42 |
|
new Latitude($latitude), |
140
|
42 |
|
new Longitude($longitude) |
141
|
14 |
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
public static function createFromArray(array $location) |
145
|
|
|
{ |
146
|
3 |
|
return static::create($location['lat'], $location['lng']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Convert the object to its JSON representation. |
151
|
|
|
* |
152
|
|
|
* @param int $options |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
3 |
|
public function toJson($options = 0) |
157
|
|
|
{ |
158
|
3 |
|
return json_encode($this->jsonSerialize(), $options); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Convert the object into something JSON serializable. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
3 |
|
public function jsonSerialize() |
167
|
|
|
{ |
168
|
3 |
|
return $this->toArray(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the instance as an array. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
21 |
|
public function toArray() |
177
|
|
|
{ |
178
|
|
|
return [ |
179
|
21 |
|
'lat' => $this->getLatitude()->value(), |
180
|
21 |
|
'lng' => $this->getLongitude()->value(), |
181
|
7 |
|
]; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|