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