Completed
Push — move ( ed2c91...889447 )
by Jeroen De
05:40 queued 03:50
created

KmlFormatter::clearElements()   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
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Maps\Presentation;
4
5
use Maps\Elements\Location;
6
use Xml;
7
8
/**
9
 * Class to format geographical data to KML.
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class KmlFormatter {
15
16
	/**
17
	 * @var array
18
	 */
19
	private $params;
20
21
	/**
22
	 * @var Location[]
23
	 */
24
	private $placemarks;
25
26
	public function __construct( array $params = [] ) {
27
		$this->params = $params;
28
		$this->clearElements();
29
	}
30
31
	/**
32
	 * Clears all set geographical objects.
33
	 */
34
	public function clearElements() {
35
		$this->clearPlacemarks();
36
	}
37
38
	/**
39
	 * Clears all set placemarks.
40
	 */
41
	public function clearPlacemarks() {
42
		$this->placemarks = [];
43
	}
44
45
	/**
46
	 * Builds and returns KML representing the set geographical objects.
47
	 */
48
	public function getKML(): string {
49
		$elements = $this->getKMLElements();
50
51
		// http://earth.google.com/kml/2.2
52
		return <<<EOT
53
<?xml version="1.0" encoding="UTF-8"?>
54
<kml xmlns="http://www.opengis.net/kml/2.2">
55
	<Document>
56
		$elements
57
	</Document>
58
</kml>
59
EOT;
60
	}
61
62
	/**
63
	 * Returns the KML for all set geographical objects.
64
	 */
65
	private function getKMLElements(): string {
66
		$elements = [];
67
68
		$elements = array_merge( $elements, $this->getPlaceMarks() );
69
70
		return implode( "\n", $elements );
71
	}
72
73
	/**
74
	 * Returns KML for all set placemarks in a list, where each element is
75
	 * a KML node representing a placemark.
76
	 */
77
	private function getPlaceMarks(): array {
78
		$placeMarks = [];
79
80
		foreach ( $this->placemarks as $location ) {
81
			$placeMarks[] = $this->getKMLForLocation( $location );
82
		}
83
84
		return $placeMarks;
85
	}
86
87
	private function getKMLForLocation( Location $location ): string {
88
		$name = '<name><![CDATA[ ' . $location->getTitle() . ']]></name>';
89
90
		$description = '<description><![CDATA[ ' . $location->getText() . ']]></description>';
91
92
		$coordinates = $location->getCoordinates();
93
94
		// lon,lat[,alt]
95
		$coordinates = Xml::element(
96
			'coordinates',
97
			[],
98
			$coordinates->getLongitude() . ',' . $coordinates->getLatitude() . ',0'
99
		);
100
101
		return <<<EOT
102
		<Placemark>
103
			$name
104
			$description
105
			<Point>
106
				$coordinates
107
			</Point>
108
		</Placemark>
109
		
110
EOT;
111
	}
112
113
	/**
114
	 * @param Location[] $placemarks
115
	 */
116
	public function addPlacemarks( array $placemarks ) {
117
		foreach ( $placemarks as $placemark ) {
118
			$this->placemarks[] = $placemark;
119
		}
120
	}
121
122
}
123