Passed
Push — master ( 47535d...51a5f9 )
by Daniel
38s
created

GlobeCoordinateValue::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace DataValues\Geo\Values;
4
5
use DataValues\DataValueObject;
6
use DataValues\IllegalValueException;
7
8
/**
9
 * Class representing a geographical coordinate value.
10
 *
11
 * @since 0.1
12
 *
13
 * @license GPL-2.0+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class GlobeCoordinateValue extends DataValueObject {
17
18
	/**
19
	 * @var LatLongValue
20
	 */
21
	private $latLong;
22
23
	/**
24
	 * The precision of the coordinate in degrees, e.g. 0.01.
25
	 *
26
	 * @var float|int|null
27
	 */
28
	private $precision;
29
30
	/**
31
	 * IRI of the globe on which the location resides.
32
	 *
33
	 * @var string
34
	 */
35
	private $globe;
36
37
	/**
38
	 * Wikidata concept URI for the Earth. Used as default value when no other globe was specified.
39
	 */
40
	const GLOBE_EARTH = 'http://www.wikidata.org/entity/Q2';
41
42
	/**
43
	 * @param LatLongValue $latLong
44
	 * @param float|int|null $precision in degrees, e.g. 0.01.
45
	 * @param string|null $globe IRI, defaults to 'http://www.wikidata.org/entity/Q2'.
46
	 *
47
	 * @throws IllegalValueException
48
	 */
49
	public function __construct( LatLongValue $latLong, $precision, $globe = null ) {
50
		if ( $globe === null ) {
51
			$globe = self::GLOBE_EARTH;
52
		}
53
54
		$this->assertIsPrecision( $precision );
55
		$this->assertIsGlobe( $globe );
56
57
		$this->latLong = $latLong;
58
		$this->precision = $precision;
59
		$this->globe = $globe;
60
	}
61
62
	/**
63
	 * @param float|int|null $precision
64
	 */
65
	protected function assertIsPrecision( $precision ) {
66
		if ( !is_null( $precision ) && !is_float( $precision ) && !is_int( $precision ) ) {
67
			throw new IllegalValueException( '$precision must be a number or null' );
68
		}
69
	}
70
71
	/**
72
	 * @param string $globe
73
	 */
74
	protected function assertIsGlobe( $globe ) {
75
		if ( !is_string( $globe ) ) {
76
			throw new IllegalValueException( '$globe must be a string or null' );
77
		}
78
	}
79
80
	/**
81
	 * @see Serializable::serialize
82
	 *
83
	 * @return string
84
	 */
85
	public function serialize() {
86
		return json_encode( array_values( $this->getArrayValue() ) );
87
	}
88
89
	/**
90
	 * @see Serializable::unserialize
91
	 *
92
	 * @param string $value
93
	 *
94
	 * @throws IllegalValueException
95
	 */
96
	public function unserialize( $value ) {
97
		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...
98
		$this->__construct( new LatLongValue( $latitude, $longitude ), $precision, $globe );
99
	}
100
101
	/**
102
	 * @see DataValue::getType
103
	 *
104
	 * @return string
105
	 */
106
	public static function getType() {
107
		return 'globecoordinate';
108
	}
109
110
	/**
111
	 * @see DataValue::getSortKey
112
	 *
113
	 * @return float
114
	 */
115
	public function getSortKey() {
116
		return $this->getLatitude();
117
	}
118
119
	/**
120
	 * @since 0.1
121
	 *
122
	 * @return float
123
	 */
124
	public function getLatitude() {
125
		return $this->latLong->getLatitude();
126
	}
127
128
	/**
129
	 * @since 0.1
130
	 *
131
	 * @return float
132
	 */
133
	public function getLongitude() {
134
		return $this->latLong->getLongitude();
135
	}
136
137
	/**
138
	 * @see DataValue::getValue
139
	 *
140
	 * @return self
141
	 */
142
	public function getValue() {
143
		return $this;
144
	}
145
146
	/**
147
	 * @since 0.1
148
	 *
149
	 * @return LatLongValue
150
	 */
151
	public function getLatLong() {
152
		return $this->latLong;
153
	}
154
155
	/**
156
	 * Returns the precision of the coordinate in degrees, e.g. 0.01.
157
	 *
158
	 * @since 0.1
159
	 *
160
	 * @return float|int|null
161
	 */
162
	public function getPrecision() {
163
		return $this->precision;
164
	}
165
166
	/**
167
	 * Returns the IRI of the globe on which the location resides.
168
	 *
169
	 * @since 0.1
170
	 *
171
	 * @return string
172
	 */
173
	public function getGlobe() {
174
		return $this->globe;
175
	}
176
177
	/**
178
	 * @see Hashable::getHash
179
	 *
180
	 * @return string
181
	 */
182
	public function getHash() {
183
		return md5( $this->latLong->getLatitude() . '|'
184
			. $this->latLong->getLongitude() . '|'
185
			. $this->precision . '|'
186
			. $this->globe );
187
	}
188
189
	/**
190
	 * @see DataValue::getArrayValue
191
	 *
192
	 * @return array
193
	 */
194
	public function getArrayValue() {
195
		return array(
196
			'latitude' => $this->latLong->getLatitude(),
197
			'longitude' => $this->latLong->getLongitude(),
198
199
			// The altitude field is no longer used in this class.
200
			// It is kept here for compatibility reasons.
201
			'altitude' => null,
202
203
			'precision' => $this->precision,
204
			'globe' => $this->globe,
205
		);
206
	}
207
208
	/**
209
	 * Constructs a new instance of the DataValue from the provided data.
210
	 * This can round-trip with @see getArrayValue
211
	 *
212
	 * @since 0.1
213
	 *
214
	 * @param array $data
215
	 *
216
	 * @return self
217
	 * @throws IllegalValueException
218
	 */
219
	public static function newFromArray( array $data ) {
220
		self::requireArrayFields( $data, array( 'latitude', 'longitude' ) );
221
222
		return new static(
223
			new LatLongValue(
224
				$data['latitude'],
225
				$data['longitude']
226
			),
227
			( isset( $data['precision'] ) ) ? $data['precision'] : null,
228
			( isset( $data['globe'] ) ) ? $data['globe'] : null
229
		);
230
	}
231
232
}
233