Completed
Pull Request — master (#53)
by no
06:33 queued 04:17
created

GlobeCoordinateValue::assertIsPrecision()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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