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