Completed
Push — master ( 6f3a5b...502ebf )
by Jeroen De
124:28 queued 62:55
created

GlobeCoordinateValue::getSortKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace DataValues\Geo\Values;
6
7
use DataValues\DataValueObject;
8
use DataValues\IllegalValueException;
9
10
/**
11
 * Class representing a geographical coordinate value.
12
 *
13
 * @since 0.1
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 * @author Thiemo Kreuz
18
 */
19
class GlobeCoordinateValue extends DataValueObject {
20
21
	/**
22
	 * @var LatLongValue
23
	 */
24
	private $latLong;
25
26
	/**
27
	 * The precision of the coordinate in degrees, e.g. 0.01.
28
	 *
29
	 * @var float|null
30
	 */
31
	private $precision;
32
33
	/**
34
	 * IRI of the globe on which the location resides.
35
	 *
36
	 * @var string
37
	 */
38
	private $globe;
39
40
	/**
41
	 * Wikidata concept URI for the Earth. Used as default value when no other globe was specified.
42
	 */
43
	public const GLOBE_EARTH = 'http://www.wikidata.org/entity/Q2';
44
45
	/**
46
	 * @param LatLongValue $latLong
47
	 * @param float|int|null $precision in degrees, e.g. 0.01.
48
	 * @param string|null $globe IRI, defaults to 'http://www.wikidata.org/entity/Q2'.
49
	 *
50
	 * @throws IllegalValueException
51
	 */
52 75
	public function __construct( LatLongValue $latLong, float $precision = null, string $globe = null ) {
53 75
		$this->assertIsPrecision( $precision );
54
55 73
		if ( $globe === null ) {
56 16
			$globe = self::GLOBE_EARTH;
57 57
		} elseif ( $globe === '' ) {
58 1
			throw new IllegalValueException( '$globe must be a non-empty string or null' );
59
		}
60
61 72
		$this->latLong = $latLong;
62 72
		$this->precision = $precision;
63 72
		$this->globe = $globe;
64 72
	}
65
66 75
	private function assertIsPrecision( ?float $precision ) {
67 75
		if ( is_float( $precision ) && ( $precision < -360 || $precision > 360 ) ) {
68 2
			throw new IllegalValueException( '$precision needs to be between -360 and 360' );
69
		}
70 73
	}
71
72
	/**
73
	 * @see Serializable::serialize
74
	 *
75
	 * @return string
76
	 */
77 32
	public function serialize(): string {
78 32
		return json_encode( array_values( $this->getArrayValue() ) );
79
	}
80
81
	/**
82
	 * @see Serializable::unserialize
83
	 *
84
	 * @param string $value
85
	 *
86
	 * @throws IllegalValueException
87
	 */
88 33
	public function unserialize( $value ) {
89 33
		list( $latitude, $longitude, $altitude, $precision, $globe ) = json_decode( $value );
0 ignored issues
show
Unused Code introduced by
The assignment to $altitude is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
90 33
		$this->__construct( new LatLongValue( $latitude, $longitude ), $precision, $globe );
91 33
	}
92
93
	/**
94
	 * @see DataValue::getType
95
	 *
96
	 * @return string
97
	 */
98
	public static function getType(): string {
99
		return 'globecoordinate';
100
	}
101
102
	/**
103
	 * @see DataValue::getSortKey
104
	 *
105
	 * @return float
106
	 */
107 16
	public function getSortKey(): float {
108 16
		return $this->getLatitude();
109
	}
110
111 19
	public function getLatitude(): float {
112 19
		return $this->latLong->getLatitude();
113
	}
114
115 3
	public function getLongitude(): float {
116 3
		return $this->latLong->getLongitude();
117
	}
118
119
	/**
120
	 * @see DataValue::getValue
121
	 *
122
	 * @return self
123
	 */
124 16
	public function getValue(): self {
125 16
		return $this;
126
	}
127
128 1
	public function getLatLong(): LatLongValue {
129 1
		return $this->latLong;
130
	}
131
132
	/**
133
	 * Returns the precision of the coordinate in degrees, e.g. 0.01.
134
	 *
135
	 * @return float|int|null
136
	 */
137 14
	public function getPrecision(): ?float {
138 14
		return $this->precision;
139
	}
140
141
	/**
142
	 * Returns the IRI of the globe on which the location resides.
143
	 *
144
	 * @return string
145
	 */
146 8
	public function getGlobe(): string {
147 8
		return $this->globe;
148
	}
149
150
	/**
151
	 * @see Hashable::getHash
152
	 *
153
	 * @since 2.0
154
	 *
155
	 * @return string
156
	 */
157 1
	public function getHash(): string {
158 1
		return md5( $this->latLong->getLatitude() . '|'
159 1
			. $this->latLong->getLongitude() . '|'
160 1
			. $this->precision . '|'
161 1
			. $this->globe );
162
	}
163
164
	/**
165
	 * @see DataValue::getArrayValue
166
	 *
167
	 * @return array
168
	 */
169 48
	public function getArrayValue(): array {
170
		return [
171 48
			'latitude' => $this->latLong->getLatitude(),
172 48
			'longitude' => $this->latLong->getLongitude(),
173
174
			// The altitude field is no longer used in this class.
175
			// It is kept here for compatibility reasons.
176
			'altitude' => null,
177
178 48
			'precision' => $this->precision,
179 48
			'globe' => $this->globe,
180
		];
181
	}
182
183
	/**
184
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
185
	 * This is expected to round-trip with @see getArrayValue.
186
	 *
187
	 * @deprecated since 2.0.1. Static DataValue::newFromArray constructors like this are
188
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
189
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
190
	 *
191
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
192
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
193
	 *  this. This is not even guaranteed to be an array!
194
	 *
195
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
196
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
197
	 * @return self
198
	 */
199 22
	public static function newFromArray( $data ): self {
200 22
		self::requireArrayFields( $data, [ 'latitude', 'longitude' ] );
201
202 17
		return new static(
203 17
			new LatLongValue(
204 17
				$data['latitude'],
205 17
				$data['longitude']
206
			),
207 17
			( isset( $data['precision'] ) ) ? $data['precision'] : null,
208 17
			( isset( $data['globe'] ) ) ? $data['globe'] : null
209
		);
210
	}
211
212
}
213