1 | <?php |
||
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 | 80 | public function __construct( LatLongValue $latLong, float $precision = null, string $globe = null ) { |
|
61 | |||
62 | 80 | private function assertIsPrecision( ?float $precision ) { |
|
67 | |||
68 | 1 | public function getLatLong(): LatLongValue { |
|
69 | 1 | return $this->latLong; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns the precision of the coordinate in degrees, e.g. 0.01. |
||
74 | */ |
||
75 | 14 | public function getPrecision(): ?float { |
|
76 | 14 | return $this->precision; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Returns the IRI of the globe on which the location resides. |
||
81 | */ |
||
82 | 8 | public function getGlobe(): string { |
|
83 | 8 | return $this->globe; |
|
84 | } |
||
85 | |||
86 | 19 | public function getLatitude(): float { |
|
87 | 19 | return $this->latLong->getLatitude(); |
|
88 | } |
||
89 | |||
90 | 3 | public function getLongitude(): float { |
|
91 | 3 | return $this->latLong->getLongitude(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @see \Comparable::equals |
||
96 | */ |
||
97 | 32 | public function equals( $target ): bool { |
|
98 | 32 | return $target instanceof self |
|
99 | 32 | && $this->latLong->equals( $target->latLong ) |
|
100 | 32 | && $this->precision === $target->precision |
|
101 | 32 | && $this->globe === $target->globe; |
|
102 | } |
||
103 | |||
104 | 16 | public function getCopy(): self { |
|
105 | 16 | return new self( |
|
106 | 16 | $this->latLong, |
|
107 | 16 | $this->precision, |
|
108 | 16 | $this->globe |
|
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @see Hashable::getHash |
||
114 | * |
||
115 | * @since 2.0 |
||
116 | */ |
||
117 | 1 | public function getHash(): string { |
|
118 | 1 | return md5( |
|
119 | 1 | $this->latLong->getLatitude() . '|' |
|
120 | 1 | . $this->latLong->getLongitude() . '|' |
|
121 | 1 | . $this->precision . '|' |
|
122 | 1 | . $this->globe |
|
123 | ); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @see Serializable::serialize |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 16 | public function serialize(): string { |
|
132 | 16 | return json_encode( array_values( $this->getArrayValue() ) ); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @see Serializable::unserialize |
||
137 | * |
||
138 | * @param string $value |
||
139 | * |
||
140 | * @throws InvalidArgumentException |
||
141 | */ |
||
142 | 17 | public function unserialize( $value ) { |
|
143 | 17 | list( $latitude, $longitude, $altitude, $precision, $globe ) = json_decode( $value ); |
|
|
|||
144 | 17 | $this->__construct( new LatLongValue( $latitude, $longitude ), $precision, $globe ); |
|
145 | 17 | } |
|
146 | |||
147 | /** |
||
148 | * @see DataValue::getType |
||
149 | */ |
||
150 | public static function getType(): string { |
||
151 | return 'globecoordinate'; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @see DataValue::getSortKey |
||
156 | */ |
||
157 | 16 | public function getSortKey(): float { |
|
158 | 16 | return $this->getLatitude(); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * @see DataValue::getValue |
||
163 | */ |
||
164 | 16 | public function getValue(): self { |
|
165 | 16 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @see DataValue::getArrayValue |
||
170 | */ |
||
171 | 32 | public function getArrayValue(): array { |
|
172 | return [ |
||
173 | 32 | 'latitude' => $this->latLong->getLatitude(), |
|
174 | 32 | 'longitude' => $this->latLong->getLongitude(), |
|
175 | |||
176 | // The altitude field is no longer used in this class. |
||
177 | // It is kept here for compatibility reasons. |
||
178 | 'altitude' => null, |
||
179 | |||
180 | 32 | 'precision' => $this->precision, |
|
181 | 32 | 'globe' => $this->globe, |
|
182 | ]; |
||
183 | } |
||
184 | |||
185 | public function toArray(): array { |
||
191 | |||
192 | 5 | public function withPrecision( ?float $precision ): self { |
|
193 | 5 | return new self( |
|
194 | 5 | $this->latLong, |
|
195 | $precision, |
||
196 | 5 | $this->globe |
|
197 | ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Constructs a new instance from the provided array. Round-trips with @see getArrayValue. |
||
202 | * |
||
203 | * @throws InvalidArgumentException |
||
204 | */ |
||
205 | 25 | public static function newFromArray( $data ): self { |
|
206 | 25 | if ( !is_array( $data ) ) { |
|
227 | |||
228 | } |
||
229 |
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.