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
|
|
|
* Object representing a geographic point. |
13
|
|
|
* |
14
|
|
|
* Latitude is specified in degrees within the range [-360, 360]. |
15
|
|
|
* Longitude is specified in degrees within the range [-360, 360]. |
16
|
|
|
* |
17
|
|
|
* @since 0.1 |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0-or-later |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class LatLongValue implements DataValue { |
23
|
|
|
|
24
|
|
|
private $latitude; |
25
|
|
|
private $longitude; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param float|int $latitude Latitude in degrees within the range [-360, 360] |
29
|
|
|
* @param float|int $longitude Longitude in degrees within the range [-360, 360] |
30
|
|
|
* |
31
|
|
|
* @throws InvalidArgumentException |
32
|
|
|
*/ |
33
|
37 |
|
public function __construct( float $latitude, float $longitude ) { |
34
|
37 |
|
$this->assertIsLatitude( $latitude ); |
35
|
33 |
|
$this->assertIsLongitude( $longitude ); |
36
|
|
|
|
37
|
29 |
|
$this->latitude = $latitude; |
38
|
29 |
|
$this->longitude = $longitude; |
39
|
29 |
|
} |
40
|
|
|
|
41
|
37 |
|
private function assertIsLatitude( float $latitude ) { |
42
|
37 |
|
if ( $latitude < -360 || $latitude > 360 ) { |
43
|
4 |
|
throw new InvalidArgumentException( 'Latitude needs to be between -360 and 360' ); |
44
|
|
|
} |
45
|
33 |
|
} |
46
|
|
|
|
47
|
33 |
|
private function assertIsLongitude( float $longitude ) { |
48
|
33 |
|
if ( $longitude < -360 || $longitude > 360 ) { |
49
|
4 |
|
throw new InvalidArgumentException( 'Longitude needs to be between -360 and 360' ); |
50
|
|
|
} |
51
|
29 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @see Serializable::serialize |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
17 |
|
public function serialize(): string { |
59
|
|
|
$data = [ |
60
|
17 |
|
$this->latitude, |
61
|
17 |
|
$this->longitude |
62
|
|
|
]; |
63
|
|
|
|
64
|
17 |
|
return implode( '|', $data ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see Serializable::unserialize |
69
|
|
|
* |
70
|
|
|
* @param string $value |
71
|
|
|
* |
72
|
|
|
* @throws InvalidArgumentException |
73
|
|
|
*/ |
74
|
7 |
|
public function unserialize( $value ) { |
75
|
7 |
|
$data = explode( '|', $value, 2 ); |
76
|
|
|
|
77
|
7 |
|
if ( count( $data ) < 2 ) { |
78
|
|
|
throw new InvalidArgumentException( 'Invalid serialization provided in ' . __METHOD__ ); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
$this->__construct( (float)$data[0], (float)$data[1] ); |
82
|
7 |
|
} |
83
|
|
|
|
84
|
2 |
|
public static function getType(): string { |
85
|
2 |
|
return 'geocoordinate'; |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
public function getSortKey(): float { |
89
|
7 |
|
return $this->latitude; |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
public function getValue(): self { |
93
|
7 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
public function getLatitude(): float { |
97
|
8 |
|
return $this->latitude; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function getLongitude(): float { |
101
|
1 |
|
return $this->longitude; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return float[] |
106
|
|
|
*/ |
107
|
8 |
|
public function getArrayValue(): array { |
108
|
|
|
return [ |
109
|
8 |
|
'latitude' => $this->latitude, |
110
|
8 |
|
'longitude' => $this->longitude |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Constructs a new instance from the provided array. Round-trips with @see getArrayValue. |
116
|
|
|
* |
117
|
|
|
* @throws InvalidArgumentException |
118
|
|
|
*/ |
119
|
7 |
|
public static function newFromArray( $data ): self { |
120
|
7 |
|
if ( !is_array( $data ) ) { |
121
|
|
|
throw new IllegalValueException( 'array expected' ); |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
if ( !array_key_exists( 'latitude', $data ) ) { |
125
|
|
|
throw new IllegalValueException( 'latitude field required' ); |
126
|
|
|
} |
127
|
|
|
|
128
|
7 |
|
if ( !array_key_exists( 'longitude', $data ) ) { |
129
|
|
|
throw new IllegalValueException( 'longitude field required' ); |
130
|
|
|
} |
131
|
|
|
|
132
|
7 |
|
return new static( $data['latitude'], $data['longitude'] ); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
public function toArray(): array { |
136
|
|
|
return [ |
137
|
1 |
|
'value' => $this->getArrayValue(), |
138
|
1 |
|
'type' => $this->getType(), |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @see \Hashable::getHash |
144
|
|
|
*/ |
145
|
1 |
|
public function getHash(): string { |
146
|
1 |
|
return md5( serialize( $this ) ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @see \Comparable::equals |
151
|
|
|
* |
152
|
|
|
* @param mixed $target |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
15 |
|
public function equals( $target ): bool { |
157
|
15 |
|
if ( $this === $target ) { |
158
|
7 |
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
8 |
|
return $target instanceof self |
162
|
8 |
|
&& serialize( $this ) === serialize( $target ); |
163
|
|
|
} |
164
|
|
|
|
165
|
9 |
|
public function getCopy(): self { |
166
|
9 |
|
return new self( $this->latitude, $this->longitude ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|