Passed
Push — master ( b6220f...9d3cd0 )
by Jeroen De
52s queued 11s
created

GlobeCoordinateValue::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace DataValues\Geo\Values;
6
7
use DataValues\DataValue;
8
use DataValues\IllegalValueException;
9
use InvalidArgumentException;
10
11
/**
12
 * Value Object representing a latitude-longitude pair with a certain precision on a certain globe.
13
 *
14
 * @since 0.1
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Thiemo Kreuz
19
 */
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 ) {
49 80
		$this->assertIsPrecision( $precision );
50
51 78
		if ( $globe === null ) {
52 16
			$globe = self::GLOBE_EARTH;
53 62
		} elseif ( $globe === '' ) {
54 1
			throw new IllegalValueException( '$globe must be a non-empty string or null' );
55
		}
56
57 77
		$this->latLong = $latLong;
58 77
		$this->precision = $precision;
59 77
		$this->globe = $globe;
60 77
	}
61
62 80
	private function assertIsPrecision( ?float $precision ) {
63 80
		if ( is_float( $precision ) && ( $precision < -360 || $precision > 360 ) ) {
64 2
			throw new IllegalValueException( '$precision needs to be between -360 and 360' );
65
		}
66 78
	}
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 );
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...
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 {
186
		return [
187
			'value' => $this->getArrayValue(),
188
			'type' => $this->getType(),
189
		];
190
	}
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 ) ) {
207 3
			throw new IllegalValueException( 'array expected' );
208
		}
209
210 22
		if ( !array_key_exists( 'latitude', $data ) ) {
211 3
			throw new IllegalValueException( 'latitude field required' );
212
		}
213
214 19
		if ( !array_key_exists( 'longitude', $data ) ) {
215 2
			throw new IllegalValueException( 'longitude field required' );
216
		}
217
218 17
		return new static(
219 17
			new LatLongValue(
220 17
				$data['latitude'],
221 17
				$data['longitude']
222
			),
223 17
			( isset( $data['precision'] ) ) ? $data['precision'] : null,
224 17
			( isset( $data['globe'] ) ) ? $data['globe'] : null
225
		);
226
	}
227
228
}
229