1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Geo\Values; |
4
|
|
|
|
5
|
|
|
use DataValues\DataValueObject; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use OutOfRangeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Object representing a geographic point. |
11
|
|
|
* |
12
|
|
|
* Latitude is specified in degrees within the range [-360, 360]. |
13
|
|
|
* Longitude is specified in degrees within the range [-360, 360]. |
14
|
|
|
* |
15
|
|
|
* @since 0.1 |
16
|
|
|
* |
17
|
|
|
* @license GPL-2.0+ |
18
|
|
|
* @author Jeroen De Dauw < [email protected] > |
19
|
|
|
*/ |
20
|
|
|
class LatLongValue extends DataValueObject { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The locations latitude. |
24
|
|
|
* |
25
|
|
|
* @var float |
26
|
|
|
*/ |
27
|
|
|
protected $latitude; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The locations longitude. |
31
|
|
|
* |
32
|
|
|
* @var float |
33
|
|
|
*/ |
34
|
|
|
protected $longitude; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param float|int $latitude |
38
|
|
|
* @param float|int $longitude |
39
|
|
|
* |
40
|
|
|
* @throws InvalidArgumentException |
41
|
|
|
*/ |
42
|
64 |
|
public function __construct( $latitude, $longitude ) { |
43
|
64 |
|
if ( is_int( $latitude ) ) { |
44
|
15 |
|
$latitude = (float)$latitude; |
45
|
|
|
} |
46
|
|
|
|
47
|
64 |
|
if ( is_int( $longitude ) ) { |
48
|
13 |
|
$longitude = (float)$longitude; |
49
|
|
|
} |
50
|
|
|
|
51
|
64 |
|
$this->assertIsLatitude( $latitude ); |
52
|
50 |
|
$this->assertIsLongitude( $longitude ); |
53
|
|
|
|
54
|
40 |
|
$this->latitude = $latitude; |
55
|
40 |
|
$this->longitude = $longitude; |
56
|
40 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param float $latitude |
60
|
|
|
*/ |
61
|
64 |
|
protected function assertIsLatitude( $latitude ) { |
62
|
64 |
|
if ( !is_float( $latitude ) ) { |
63
|
11 |
|
throw new InvalidArgumentException( 'Can only construct LatLongValue with a numeric latitude' ); |
64
|
|
|
} |
65
|
|
|
|
66
|
53 |
|
if ( $latitude < -360 || $latitude > 360 ) { |
67
|
3 |
|
throw new OutOfRangeException( 'Latitude needs to be between -360 and 360' ); |
68
|
|
|
} |
69
|
50 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param float $longitude |
73
|
|
|
*/ |
74
|
50 |
|
protected function assertIsLongitude( $longitude ) { |
75
|
50 |
|
if ( !is_float( $longitude ) ) { |
76
|
8 |
|
throw new InvalidArgumentException( 'Can only construct LatLongValue with a numeric longitude' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
42 |
|
if ( $longitude < -360 || $longitude > 360 ) { |
80
|
2 |
|
throw new OutOfRangeException( 'Longitude needs to be between -360 and 360' ); |
81
|
|
|
} |
82
|
40 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @see Serializable::serialize |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
30 |
|
public function serialize() { |
90
|
|
|
$data = array( |
91
|
30 |
|
$this->latitude, |
92
|
30 |
|
$this->longitude |
93
|
|
|
); |
94
|
|
|
|
95
|
30 |
|
return implode( '|', $data ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @see Serializable::unserialize |
100
|
|
|
* |
101
|
|
|
* @param string $value |
102
|
|
|
* |
103
|
|
|
* @throws InvalidArgumentException |
104
|
|
|
*/ |
105
|
30 |
|
public function unserialize( $value ) { |
106
|
30 |
|
$data = explode( '|', $value, 2 ); |
107
|
|
|
|
108
|
30 |
|
if ( count( $data ) < 2 ) { |
109
|
|
|
throw new InvalidArgumentException( 'Invalid serialization provided in ' . __METHOD__ ); |
110
|
|
|
} |
111
|
|
|
|
112
|
30 |
|
$this->__construct( (float)$data[0], (float)$data[1] ); |
113
|
30 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @see DataValue::getType |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
20 |
|
public static function getType() { |
121
|
|
|
// TODO: This really should be 'latlong' but serializations may explode if we rename it. |
122
|
20 |
|
return 'geocoordinate'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @see DataValue::getSortKey |
127
|
|
|
* |
128
|
|
|
* @return float |
129
|
|
|
*/ |
130
|
|
|
public function getSortKey() { |
131
|
|
|
return $this->latitude; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @see DataValue::getValue |
136
|
|
|
* |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
10 |
|
public function getValue() { |
140
|
10 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return float |
145
|
|
|
*/ |
146
|
10 |
|
public function getLatitude() { |
147
|
10 |
|
return $this->latitude; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return float |
152
|
|
|
*/ |
153
|
10 |
|
public function getLongitude() { |
154
|
10 |
|
return $this->longitude; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @see DataValue::getArrayValue |
159
|
|
|
* |
160
|
|
|
* @return float[] |
161
|
|
|
*/ |
162
|
20 |
|
public function getArrayValue() { |
163
|
|
|
return array( |
164
|
20 |
|
'latitude' => $this->latitude, |
165
|
20 |
|
'longitude' => $this->longitude |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
171
|
|
|
* This is expected to round-trip with @see getArrayValue. |
172
|
|
|
* |
173
|
|
|
* @deprecated since 2.0.1. Static DataValue::newFromArray constructors like this are |
174
|
|
|
* underspecified (not in the DataValue interface), and misleadingly named (should be named |
175
|
|
|
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
176
|
|
|
* |
177
|
|
|
* @param mixed $data Warning! Even if this is expected to be a value as returned by |
178
|
|
|
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
179
|
|
|
* this. This is not even guaranteed to be an array! |
180
|
|
|
* |
181
|
|
|
* @throws InvalidArgumentException if $data is not in the expected format. Subclasses of |
182
|
|
|
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
183
|
|
|
* @return self |
184
|
|
|
*/ |
185
|
|
|
public static function newFromArray( $data ) { |
186
|
|
|
self::requireArrayFields( $data, [ 'latitude', 'longitude' ] ); |
187
|
|
|
|
188
|
|
|
return new static( $data['latitude'], $data['longitude'] ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|