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