Completed
Pull Request — master (#53)
by no
15:10 queued 12:08
created

GlobeCoordinateValue   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 218
rs 10

15 Methods

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