1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Object |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Object\Domain\Model\Properties; |
38
|
|
|
|
39
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Object location |
43
|
|
|
* |
44
|
|
|
* @package Apparat\Object |
45
|
|
|
* @subpackage Apparat\Object\Domain |
46
|
|
|
*/ |
47
|
|
|
class LocationProperties extends AbstractProperties |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Latitude |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
const LATITUDE = 'latitude'; |
55
|
|
|
/** |
56
|
|
|
* Latitude |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
const LONGITUDE = 'longitude'; |
61
|
|
|
/** |
62
|
|
|
* Elevation |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
const ELEVATION = 'elevation'; |
67
|
|
|
/** |
68
|
|
|
* Latitude |
69
|
|
|
* |
70
|
|
|
* @var float |
71
|
|
|
*/ |
72
|
|
|
protected $latitude = null; |
73
|
|
|
/** |
74
|
|
|
* Longitude |
75
|
|
|
* |
76
|
|
|
* @var float |
77
|
|
|
*/ |
78
|
|
|
protected $longitude = null; |
79
|
|
|
/** |
80
|
|
|
* Elevation |
81
|
|
|
* |
82
|
|
|
* @var float |
83
|
|
|
*/ |
84
|
|
|
protected $elevation = null; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Location constructor |
88
|
|
|
* |
89
|
|
|
* @param array $data Property data |
90
|
|
|
* @param ObjectInterface $object Owner object |
91
|
|
|
*/ |
92
|
22 |
|
public function __construct(array $data, ObjectInterface $object) |
93
|
|
|
{ |
94
|
22 |
|
parent::__construct($data, $object); |
95
|
|
|
|
96
|
|
|
// Set the latitude |
97
|
22 |
|
if (!empty($data[self::LATITUDE]) && $this->validateLocationProperty($data[self::LATITUDE])) { |
98
|
|
|
$this->latitude = $data[self::LATITUDE]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Set the longitude |
102
|
22 |
|
if (!empty($data[self::LONGITUDE]) && $this->validateLocationProperty($data[self::LONGITUDE])) { |
103
|
|
|
$this->longitude = $data[self::LONGITUDE]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Set the elevation |
107
|
22 |
|
if (!empty($data[self::ELEVATION]) && $this->validateLocationProperty($data[self::ELEVATION])) { |
108
|
|
|
$this->elevation = $data[self::ELEVATION]; |
109
|
|
|
} |
110
|
22 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Validate a location value |
114
|
|
|
* |
115
|
|
|
* @param float|null $value Location property value |
116
|
|
|
* @return boolean Location value validity |
117
|
|
|
* @throws InvalidArgumentException If the location property value is not a float |
118
|
|
|
*/ |
119
|
1 |
|
protected function validateLocationProperty($value) |
120
|
|
|
{ |
121
|
|
|
// If the location property value is not a float |
122
|
1 |
|
if (($value !== null) && !is_float($value)) { |
123
|
1 |
|
throw new InvalidArgumentException( |
124
|
1 |
|
sprintf('Invalid property location value "%s"', $value), |
125
|
1 |
|
InvalidArgumentException::INVALID_LOCATION_PROPERTY_VALUE |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return the latitude |
134
|
|
|
* |
135
|
|
|
* @return float Latitude |
136
|
|
|
*/ |
137
|
1 |
|
public function getLatitude() |
138
|
|
|
{ |
139
|
1 |
|
return $this->latitude; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the latitude |
144
|
|
|
* |
145
|
|
|
* @param float $latitude Latitude |
146
|
|
|
* @return LocationProperties Self reference |
147
|
|
|
*/ |
148
|
1 |
|
public function setLatitude($latitude) |
149
|
|
|
{ |
150
|
1 |
|
$this->validateLocationProperty($latitude); |
151
|
1 |
|
return $this->mutateFloatProperty(self::LATITUDE, $latitude); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return the longitude |
156
|
|
|
* |
157
|
|
|
* @return float Longitude |
158
|
|
|
*/ |
159
|
1 |
|
public function getLongitude() |
160
|
|
|
{ |
161
|
1 |
|
return $this->longitude; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set the longitude |
166
|
|
|
* |
167
|
|
|
* @param float $longitude Longitude |
168
|
|
|
* @return LocationProperties Self reference |
169
|
|
|
*/ |
170
|
1 |
|
public function setLongitude($longitude) |
171
|
|
|
{ |
172
|
1 |
|
$this->validateLocationProperty($longitude); |
173
|
1 |
|
return $this->mutateFloatProperty(self::LONGITUDE, $longitude); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return the elevation |
178
|
|
|
* |
179
|
|
|
* @return float Elevation |
180
|
|
|
*/ |
181
|
|
|
public function getElevation() |
182
|
|
|
{ |
183
|
|
|
return $this->elevation; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the elevation |
188
|
|
|
* |
189
|
|
|
* @param float $elevation |
190
|
|
|
* @return LocationProperties Self reference |
191
|
|
|
*/ |
192
|
1 |
|
public function setElevation($elevation) |
193
|
|
|
{ |
194
|
1 |
|
$this->validateLocationProperty($elevation); |
195
|
|
|
return $this->mutateFloatProperty(self::ELEVATION, $elevation); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return the location values as array |
200
|
|
|
* |
201
|
|
|
* @return array LocationProperties values |
202
|
|
|
*/ |
203
|
5 |
|
public function toArray() |
204
|
|
|
{ |
205
|
5 |
|
return array_filter([ |
206
|
5 |
|
self::LATITUDE => $this->latitude, |
207
|
5 |
|
self::LONGITUDE => $this->longitude, |
208
|
5 |
|
self::ELEVATION => $this->elevation, |
209
|
|
|
]); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|