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
|
87 |
|
public function __construct(Coordinate $latitude, Coordinate $longitude) |
55
|
|
|
{ |
56
|
87 |
|
$this->setLatitude($latitude); |
57
|
87 |
|
$this->setLongitude($longitude); |
58
|
87 |
|
} |
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
|
66 |
|
public function getLatitude() |
71
|
|
|
{ |
72
|
66 |
|
return $this->latitude; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the latitude coordinate (alias). |
77
|
|
|
* |
78
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Coordinate |
79
|
|
|
*/ |
80
|
42 |
|
public function lat() |
81
|
|
|
{ |
82
|
42 |
|
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
|
87 |
|
public function setLatitude(Coordinate $latitude) |
93
|
|
|
{ |
94
|
87 |
|
$this->latitude = $latitude; |
95
|
|
|
|
96
|
87 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the longitude coordinate. |
101
|
|
|
* |
102
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Coordinate |
103
|
|
|
*/ |
104
|
66 |
|
public function getLongitude() |
105
|
|
|
{ |
106
|
66 |
|
return $this->longitude; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the longitude coordinate (alias). |
111
|
|
|
* |
112
|
|
|
* @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Coordinate |
113
|
|
|
*/ |
114
|
42 |
|
public function lng() |
115
|
|
|
{ |
116
|
42 |
|
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
|
87 |
|
public function setLongitude(Coordinate $longitude) |
127
|
|
|
{ |
128
|
87 |
|
$this->longitude = $longitude; |
129
|
|
|
|
130
|
87 |
|
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
|
60 |
|
public static function create($latitude, $longitude) |
147
|
|
|
{ |
148
|
60 |
|
return new static( |
149
|
60 |
|
new Latitude($latitude), |
150
|
60 |
|
new Longitude($longitude) |
151
|
20 |
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create position instance from array. |
156
|
|
|
* |
157
|
|
|
* @param array $location |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
36 |
|
public static function createFromArray(array $location) |
162
|
|
|
{ |
163
|
36 |
|
return static::create($location['lat'], $location['lng']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the instance as an array. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
27 |
|
public function toArray() |
172
|
|
|
{ |
173
|
|
|
return [ |
174
|
27 |
|
'lat' => $this->getLatitude()->value(), |
175
|
27 |
|
'lng' => $this->getLongitude()->value(), |
176
|
9 |
|
]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|