Line::getLineCoordinates()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
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 Maps\LegacyModel;
6
7
use DataValues\Geo\Values\LatLongValue;
8
use InvalidArgumentException;
9
10
/**
11
 * Class representing a collection of LatLongValue objects forming a line.
12
 *
13
 * @since 3.0
14
 *
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Kim Eik < [email protected] >
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class Line extends \Maps\LegacyModel\BaseStrokableElement {
21
22
	/**
23
	 * @since 3.0
24
	 *
25
	 * @var LatLongValue[]
26
	 */
27
	protected $coordinates;
28
29
	/**
30
	 * @since 3.0
31
	 *
32
	 * @param LatLongValue[] $coordinates
33
	 *
34
	 * @throws InvalidArgumentException
35
	 */
36 6
	public function __construct( array $coordinates = [] ) {
37 6
		foreach ( $coordinates as $coordinate ) {
38 5
			if ( !( $coordinate instanceof LatLongValue ) ) {
39 3
				throw new InvalidArgumentException( 'Can only construct Line with LatLongValue objects' );
40
			}
41
		}
42
43 3
		$this->coordinates = $coordinates;
44 3
	}
45
46
	/**
47
	 * @since 3.0
48
	 *
49
	 * @return LatLongValue[]
50
	 */
51 3
	public function getLineCoordinates() {
52 3
		return $this->coordinates;
53
	}
54
55
	public function getJSONObject( string $defText = '', string $defTitle = '' ): array {
56
		$parentArray = parent::getJSONObject( $defText, $defTitle );
57
		$posArray = [];
58
59
		foreach ( $this->coordinates as $mapLocation ) {
60
			$posArray[] = [
61
				'lat' => $mapLocation->getLatitude(),
62
				'lon' => $mapLocation->getLongitude()
63
			];
64
		}
65
66
		$posArray = [ 'pos' => $posArray ];
67
68
		return array_merge( $parentArray, $posArray );
69
	}
70
71
}
72