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-or-later |
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
|
|
|
private $latitude; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The locations longitude. |
31
|
|
|
* |
32
|
|
|
* @var float |
33
|
|
|
*/ |
34
|
|
|
private $longitude; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param float|int $latitude |
38
|
|
|
* @param float|int $longitude |
39
|
|
|
* |
40
|
|
|
* @throws InvalidArgumentException |
41
|
|
|
*/ |
42
|
45 |
|
public function __construct( float $latitude, float $longitude ) { |
43
|
45 |
|
if ( is_int( $latitude ) ) { |
44
|
|
|
$latitude = (float)$latitude; |
45
|
|
|
} |
46
|
|
|
|
47
|
45 |
|
if ( is_int( $longitude ) ) { |
48
|
|
|
$longitude = (float)$longitude; |
49
|
|
|
} |
50
|
|
|
|
51
|
45 |
|
$this->assertIsLatitude( $latitude ); |
52
|
42 |
|
$this->assertIsLongitude( $longitude ); |
53
|
|
|
|
54
|
40 |
|
$this->latitude = $latitude; |
55
|
40 |
|
$this->longitude = $longitude; |
56
|
40 |
|
} |
57
|
|
|
|
58
|
45 |
|
private function assertIsLatitude( float $latitude ) { |
59
|
45 |
|
if ( $latitude < -360 || $latitude > 360 ) { |
60
|
3 |
|
throw new OutOfRangeException( 'Latitude needs to be between -360 and 360' ); |
61
|
|
|
} |
62
|
42 |
|
} |
63
|
|
|
|
64
|
42 |
|
private function assertIsLongitude( float $longitude ) { |
65
|
42 |
|
if ( $longitude < -360 || $longitude > 360 ) { |
66
|
2 |
|
throw new OutOfRangeException( 'Longitude needs to be between -360 and 360' ); |
67
|
|
|
} |
68
|
40 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @see Serializable::serialize |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
30 |
|
public function serialize(): string { |
76
|
|
|
$data = [ |
77
|
30 |
|
$this->latitude, |
78
|
30 |
|
$this->longitude |
79
|
|
|
]; |
80
|
|
|
|
81
|
30 |
|
return implode( '|', $data ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @see Serializable::unserialize |
86
|
|
|
* |
87
|
|
|
* @param string $value |
88
|
|
|
* |
89
|
|
|
* @throws InvalidArgumentException |
90
|
|
|
*/ |
91
|
30 |
|
public function unserialize( $value ) { |
92
|
30 |
|
$data = explode( '|', $value, 2 ); |
93
|
|
|
|
94
|
30 |
|
if ( count( $data ) < 2 ) { |
95
|
|
|
throw new InvalidArgumentException( 'Invalid serialization provided in ' . __METHOD__ ); |
96
|
|
|
} |
97
|
|
|
|
98
|
30 |
|
$this->__construct( (float)$data[0], (float)$data[1] ); |
99
|
30 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see DataValue::getType |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
20 |
|
public static function getType(): string { |
107
|
|
|
// TODO: This really should be 'latlong' but serializations may explode if we rename it. |
108
|
20 |
|
return 'geocoordinate'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @see DataValue::getSortKey |
113
|
|
|
* |
114
|
|
|
* @return float |
115
|
|
|
*/ |
116
|
|
|
public function getSortKey(): float { |
117
|
|
|
return $this->latitude; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @see DataValue::getValue |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
10 |
|
public function getValue(): self { |
126
|
10 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
10 |
|
public function getLatitude(): float { |
130
|
10 |
|
return $this->latitude; |
131
|
|
|
} |
132
|
|
|
|
133
|
10 |
|
public function getLongitude(): float { |
134
|
10 |
|
return $this->longitude; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @see DataValue::getArrayValue |
139
|
|
|
* |
140
|
|
|
* @return float[] |
141
|
|
|
*/ |
142
|
20 |
|
public function getArrayValue(): array { |
143
|
|
|
return [ |
144
|
20 |
|
'latitude' => $this->latitude, |
145
|
20 |
|
'longitude' => $this->longitude |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
151
|
|
|
* This is expected to round-trip with @see getArrayValue. |
152
|
|
|
* |
153
|
|
|
* @deprecated since 2.0.1. Static DataValue::newFromArray constructors like this are |
154
|
|
|
* underspecified (not in the DataValue interface), and misleadingly named (should be named |
155
|
|
|
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
156
|
|
|
* |
157
|
|
|
* @param mixed $data Warning! Even if this is expected to be a value as returned by |
158
|
|
|
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
159
|
|
|
* this. This is not even guaranteed to be an array! |
160
|
|
|
* |
161
|
|
|
* @throws InvalidArgumentException if $data is not in the expected format. Subclasses of |
162
|
|
|
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
163
|
|
|
* @return self |
164
|
|
|
*/ |
165
|
|
|
public static function newFromArray( $data ): self { |
166
|
|
|
self::requireArrayFields( $data, [ 'latitude', 'longitude' ] ); |
167
|
|
|
|
168
|
|
|
return new static( $data['latitude'], $data['longitude'] ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|