Passed
Push — testz ( 584ff4 )
by Jeroen De
293:56 queued 277:54
created

LatLongValue::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 37
	public function __construct( float $latitude, float $longitude ) {
34 37
		$this->assertIsLatitude( $latitude );
35 33
		$this->assertIsLongitude( $longitude );
36
37 29
		$this->latitude = $latitude;
38 29
		$this->longitude = $longitude;
39 29
	}
40
41 37
	private function assertIsLatitude( float $latitude ) {
42 37
		if ( $latitude < -360 || $latitude > 360 ) {
43 4
			throw new InvalidArgumentException( 'Latitude needs to be between -360 and 360' );
44
		}
45 33
	}
46
47 33
	private function assertIsLongitude( float $longitude ) {
48 33
		if ( $longitude < -360 || $longitude > 360 ) {
49 4
			throw new InvalidArgumentException( 'Longitude needs to be between -360 and 360' );
50
		}
51 29
	}
52
53
	/**
54
	 * @see Serializable::serialize
55
	 *
56
	 * @return string
57
	 */
58 17
	public function serialize(): string {
59
		$data = [
60 17
			$this->latitude,
61 17
			$this->longitude
62
		];
63
64 17
		return implode( '|', $data );
65
	}
66
67
	/**
68
	 * @see Serializable::unserialize
69
	 *
70
	 * @param string $value
71
	 *
72
	 * @throws InvalidArgumentException
73
	 */
74 7
	public function unserialize( $value ) {
75 7
		$data = explode( '|', $value, 2 );
76
77 7
		if ( count( $data ) < 2 ) {
78
			throw new InvalidArgumentException( 'Invalid serialization provided in ' . __METHOD__ );
79
		}
80
81 7
		$this->__construct( (float)$data[0], (float)$data[1] );
82 7
	}
83
84 2
	public static function getType(): string {
85 2
		return 'geocoordinate';
86
	}
87
88 7
	public function getSortKey(): float {
89 7
		return $this->latitude;
90
	}
91
92
	public function getValue(): self {
93
		return $this;
94
	}
95
96 8
	public function getLatitude(): float {
97 8
		return $this->latitude;
98
	}
99
100 1
	public function getLongitude(): float {
101 1
		return $this->longitude;
102
	}
103
104
	/**
105
	 * @return float[]
106
	 */
107 8
	public function getArrayValue(): array {
108
		return [
109 8
			'latitude' => $this->latitude,
110 8
			'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 7
	public static function newFromArray( $data ): self {
123 7
		if ( !is_array( $data ) ) {
124
			throw new IllegalValueException( 'array expected' );
125
		}
126
127 7
		if ( !array_key_exists( 'latitude', $data ) ) {
128
			throw new IllegalValueException( 'latitude field required' );
129
		}
130
131 7
		if ( !array_key_exists( 'longitude', $data ) ) {
132
			throw new IllegalValueException( 'longitude field required' );
133
		}
134
135 7
		return new static( $data['latitude'], $data['longitude'] );
136
	}
137
138 1
	public function toArray(): array {
139
		return [
140 1
			'value' => $this->getArrayValue(),
141 1
			'type' => $this->getType(),
142
		];
143
	}
144
145
	/**
146
	 * @see \Hashable::getHash
147
	 */
148 1
	public function getHash(): string {
149 1
		return md5( serialize( $this ) );
150
	}
151
152
	/**
153
	 * @see \Comparable::equals
154
	 *
155
	 * @param mixed $target
156
	 *
157
	 * @return bool
158
	 */
159 15
	public function equals( $target ): bool {
160 15
		if ( $this === $target ) {
161 7
			return true;
162
		}
163
164 8
		return $target instanceof self
165 8
			&& serialize( $this ) === serialize( $target );
166
	}
167
168 9
	public function getCopy(): self {
169 9
		return new self( $this->latitude, $this->longitude );
170
	}
171
172
}
173