Completed
Pull Request — master (#156)
by Jeroen De
04:20 queued 02:08
created

LatLongValue::equals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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
 * Object representing a geographic point.
13
 *
14
 * Latitude is specified in degrees within the range [-360, 360].
15
 * Longitude is specified in degrees within the range [-360, 360].
16
 *
17
 * @since 0.1
18
 *
19
 * @license GPL-2.0-or-later
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class LatLongValue implements DataValue {
23
24
	private $latitude;
25
	private $longitude;
26
27
	/**
28
	 * @param float|int $latitude Latitude in degrees within the range [-360, 360]
29
	 * @param float|int $longitude Longitude in degrees within the range [-360, 360]
30
	 *
31
	 * @throws InvalidArgumentException
32
	 */
33 63
	public function __construct( float $latitude, float $longitude ) {
34 63
		$this->assertIsLatitude( $latitude );
35 58
		$this->assertIsLongitude( $longitude );
36
37 54
		$this->latitude = $latitude;
38 54
		$this->longitude = $longitude;
39 54
	}
40
41 63
	private function assertIsLatitude( float $latitude ) {
42 63
		if ( $latitude < -360 || $latitude > 360 ) {
43 5
			throw new InvalidArgumentException( 'Latitude needs to be between -360 and 360' );
44
		}
45 58
	}
46
47 58
	private function assertIsLongitude( float $longitude ) {
48 58
		if ( $longitude < -360 || $longitude > 360 ) {
49 4
			throw new InvalidArgumentException( 'Longitude needs to be between -360 and 360' );
50
		}
51 54
	}
52
53
	/**
54
	 * @see Serializable::serialize
55
	 *
56
	 * @return string
57
	 */
58 42
	public function serialize(): string {
59
		$data = [
60 42
			$this->latitude,
61 42
			$this->longitude
62
		];
63
64 42
		return implode( '|', $data );
65
	}
66
67
	/**
68
	 * @see Serializable::unserialize
69
	 *
70
	 * @param string $value
71
	 *
72
	 * @throws InvalidArgumentException
73
	 */
74 10
	public function unserialize( $value ) {
75 10
		$data = explode( '|', $value, 2 );
76
77 10
		if ( count( $data ) < 2 ) {
78
			throw new InvalidArgumentException( 'Invalid serialization provided in ' . __METHOD__ );
79
		}
80
81 10
		$this->__construct( (float)$data[0], (float)$data[1] );
82 10
	}
83
84 20
	public static function getType(): string {
85 20
		return 'geocoordinate';
86
	}
87
88
	public function getSortKey(): float {
89
		return $this->latitude;
90
	}
91
92 10
	public function getValue(): self {
93 10
		return $this;
94
	}
95
96 10
	public function getLatitude(): float {
97 10
		return $this->latitude;
98
	}
99
100 10
	public function getLongitude(): float {
101 10
		return $this->longitude;
102
	}
103
104
	/**
105
	 * @return float[]
106
	 */
107 20
	public function getArrayValue(): array {
108
		return [
109 20
			'latitude' => $this->latitude,
110 20
			'longitude' => $this->longitude
111
		];
112
	}
113
114
	/**
115
	 * Constructs a new instance from the provided array. Round-trips with @see getArrayValue.
116
	 *
117
	 * @deprecated since 2.0.1. When using this static constructor for DataValues of unknown
118
	 * types, please use DataValueDeserializer from the data-values/serialization package instead.
119
	 *
120
	 * @throws InvalidArgumentException
121
	 */
122
	public static function newFromArray( $data ): self {
123
		if ( !is_array( $data ) ) {
124
			throw new IllegalValueException( 'array expected' );
125
		}
126
127
		if ( !array_key_exists( 'latitude', $data ) ) {
128
			throw new IllegalValueException( 'latitude field required' );
129
		}
130
131
		if ( !array_key_exists( 'longitude', $data ) ) {
132
			throw new IllegalValueException( 'longitude field required' );
133
		}
134
135
		return new static( $data['latitude'], $data['longitude'] );
136
	}
137
138 10
	public function toArray(): array {
139
		return [
140 10
			'value' => $this->getArrayValue(),
141 10
			'type' => $this->getType(),
142
		];
143
	}
144
145
	/**
146
	 * @see \Hashable::getHash
147
	 */
148 11
	public function getHash(): string {
149 11
		return md5( serialize( $this ) );
150
	}
151
152
	/**
153
	 * @see \Comparable::equals
154
	 *
155
	 * @param mixed $target
156
	 *
157
	 * @return bool
158
	 */
159 51
	public function equals( $target ): bool {
160 51
		if ( $this === $target ) {
161 20
			return true;
162
		}
163
164 41
		return $target instanceof self
165 41
			&& serialize( $this ) === serialize( $target );
166
	}
167
168 32
	public function getCopy(): self {
169 32
		return new self( $this->latitude, $this->longitude );
170
	}
171
172
}
173