Completed
Push — cleanz ( d9e779 )
by Jeroen De
06:06
created

Line   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 47.06%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 52
ccs 8
cts 17
cp 0.4706
rs 10
c 0
b 0
f 0

3 Methods

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