|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DataValues\Geo\Values; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\DataValueObject; |
|
6
|
|
|
use DataValues\IllegalValueException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class representing a geographical coordinate value. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 0.1 |
|
12
|
|
|
* |
|
13
|
|
|
* @license GPL-2.0-or-later |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
* @author Thiemo Kreuz |
|
16
|
|
|
*/ |
|
17
|
|
|
class GlobeCoordinateValue extends DataValueObject { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var LatLongValue |
|
21
|
|
|
*/ |
|
22
|
|
|
private $latLong; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The precision of the coordinate in degrees, e.g. 0.01. |
|
26
|
|
|
* |
|
27
|
|
|
* @var float|null |
|
28
|
|
|
*/ |
|
29
|
|
|
private $precision; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* IRI of the globe on which the location resides. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $globe; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Wikidata concept URI for the Earth. Used as default value when no other globe was specified. |
|
40
|
|
|
*/ |
|
41
|
|
|
public const GLOBE_EARTH = 'http://www.wikidata.org/entity/Q2'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param LatLongValue $latLong |
|
45
|
|
|
* @param float|int|null $precision in degrees, e.g. 0.01. |
|
46
|
|
|
* @param string|null $globe IRI, defaults to 'http://www.wikidata.org/entity/Q2'. |
|
47
|
|
|
* |
|
48
|
|
|
* @throws IllegalValueException |
|
49
|
|
|
*/ |
|
50
|
71 |
|
public function __construct( LatLongValue $latLong, float $precision = null, string $globe = null ) { |
|
51
|
71 |
|
$this->assertIsPrecision( $precision ); |
|
52
|
|
|
|
|
53
|
69 |
|
if ( $globe === null ) { |
|
54
|
13 |
|
$globe = self::GLOBE_EARTH; |
|
55
|
56 |
|
} elseif ( $globe === '' ) { |
|
56
|
1 |
|
throw new IllegalValueException( '$globe must be a non-empty string or null' ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
68 |
|
$this->latLong = $latLong; |
|
60
|
68 |
|
$this->precision = $precision; |
|
61
|
68 |
|
$this->globe = $globe; |
|
62
|
68 |
|
} |
|
63
|
|
|
|
|
64
|
71 |
|
private function assertIsPrecision( ?float $precision ) { |
|
65
|
71 |
|
if ( is_float( $precision ) && ( $precision < -360 || $precision > 360 ) ) { |
|
66
|
2 |
|
throw new IllegalValueException( '$precision needs to be between -360 and 360' ); |
|
67
|
|
|
} |
|
68
|
69 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @see Serializable::serialize |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
48 |
|
public function serialize(): string { |
|
76
|
48 |
|
return json_encode( array_values( $this->getArrayValue() ) ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @see Serializable::unserialize |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $value |
|
83
|
|
|
* |
|
84
|
|
|
* @throws IllegalValueException |
|
85
|
|
|
*/ |
|
86
|
49 |
|
public function unserialize( $value ) { |
|
87
|
49 |
|
list( $latitude, $longitude, $altitude, $precision, $globe ) = json_decode( $value ); |
|
|
|
|
|
|
88
|
49 |
|
$this->__construct( new LatLongValue( $latitude, $longitude ), $precision, $globe ); |
|
89
|
49 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @see DataValue::getType |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
32 |
|
public static function getType(): string { |
|
97
|
32 |
|
return 'globecoordinate'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @see DataValue::getSortKey |
|
102
|
|
|
* |
|
103
|
|
|
* @return float |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getSortKey(): float { |
|
106
|
|
|
return $this->getLatitude(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
18 |
|
public function getLatitude(): float { |
|
110
|
18 |
|
return $this->latLong->getLatitude(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
18 |
|
public function getLongitude(): float { |
|
114
|
18 |
|
return $this->latLong->getLongitude(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @see DataValue::getValue |
|
119
|
|
|
* |
|
120
|
|
|
* @return self |
|
121
|
|
|
*/ |
|
122
|
16 |
|
public function getValue(): self { |
|
123
|
16 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function getLatLong(): LatLongValue { |
|
127
|
1 |
|
return $this->latLong; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns the precision of the coordinate in degrees, e.g. 0.01. |
|
132
|
|
|
* |
|
133
|
|
|
* @return float|int|null |
|
134
|
|
|
*/ |
|
135
|
18 |
|
public function getPrecision(): ?float { |
|
136
|
18 |
|
return $this->precision; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns the IRI of the globe on which the location resides. |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
18 |
|
public function getGlobe(): string { |
|
145
|
18 |
|
return $this->globe; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @see Hashable::getHash |
|
150
|
|
|
* |
|
151
|
|
|
* @since 2.0 |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
17 |
|
public function getHash(): string { |
|
156
|
17 |
|
return md5( $this->latLong->getLatitude() . '|' |
|
157
|
17 |
|
. $this->latLong->getLongitude() . '|' |
|
158
|
17 |
|
. $this->precision . '|' |
|
159
|
17 |
|
. $this->globe ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @see DataValue::getArrayValue |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
80 |
|
public function getArrayValue(): array { |
|
168
|
|
|
return [ |
|
169
|
80 |
|
'latitude' => $this->latLong->getLatitude(), |
|
170
|
80 |
|
'longitude' => $this->latLong->getLongitude(), |
|
171
|
|
|
|
|
172
|
|
|
// The altitude field is no longer used in this class. |
|
173
|
|
|
// It is kept here for compatibility reasons. |
|
174
|
|
|
'altitude' => null, |
|
175
|
|
|
|
|
176
|
80 |
|
'precision' => $this->precision, |
|
177
|
80 |
|
'globe' => $this->globe, |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
|
183
|
|
|
* This is expected to round-trip with @see getArrayValue. |
|
184
|
|
|
* |
|
185
|
|
|
* @deprecated since 2.0.1. Static DataValue::newFromArray constructors like this are |
|
186
|
|
|
* underspecified (not in the DataValue interface), and misleadingly named (should be named |
|
187
|
|
|
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
|
188
|
|
|
* |
|
189
|
|
|
* @param mixed $data Warning! Even if this is expected to be a value as returned by |
|
190
|
|
|
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
|
191
|
|
|
* this. This is not even guaranteed to be an array! |
|
192
|
|
|
* |
|
193
|
|
|
* @throws IllegalValueException if $data is not in the expected format. Subclasses of |
|
194
|
|
|
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
|
195
|
|
|
* @return self |
|
196
|
|
|
*/ |
|
197
|
6 |
|
public static function newFromArray( $data ): self { |
|
198
|
6 |
|
self::requireArrayFields( $data, [ 'latitude', 'longitude' ] ); |
|
199
|
|
|
|
|
200
|
1 |
|
return new static( |
|
201
|
1 |
|
new LatLongValue( |
|
202
|
1 |
|
$data['latitude'], |
|
203
|
1 |
|
$data['longitude'] |
|
204
|
|
|
), |
|
205
|
1 |
|
( isset( $data['precision'] ) ) ? $data['precision'] : null, |
|
206
|
1 |
|
( isset( $data['globe'] ) ) ? $data['globe'] : null |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.