KmlFormatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0
ccs 24
cts 24
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A formatLocationsAsKml() 0 13 1
A locationToKmlPlacemark() 0 21 1
A getCoordinateString() 0 6 1
A escapeValue() 0 3 1
A getKmlForLocations() 0 11 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Presentation;
6
7
use Maps\LegacyModel\Location;
8
9
/**
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class KmlFormatter {
14
15
	/**
16
	 * Builds and returns KML representing the set geographical objects.
17
	 */
18 3
	public function formatLocationsAsKml( Location ...$locations ): string {
19 3
		$elements = $this->getKmlForLocations( $locations );
20
21
		// http://earth.google.com/kml/2.2
22
		return <<<EOT
23
<?xml version="1.0" encoding="UTF-8"?>
24
<kml xmlns="http://www.opengis.net/kml/2.2">
25
	<Document>
26 3
$elements
27
	</Document>
28
</kml>
29
EOT;
30
	}
31
32 3
	private function getKmlForLocations( array $locations ): string {
33 3
		return implode(
34 3
			"\n",
35 3
			array_map(
36 3
				function( Location $location ) {
37 2
					return $this->locationToKmlPlacemark( $location );
38 3
				},
39
				$locations
40
			)
41
		);
42
	}
43
44
45 2
	private function locationToKmlPlacemark( Location $location ): string {
46
		// TODO: escaping?
47 2
		$name = '<name><![CDATA[' . $location->getTitle() . ']]></name>';
48
49
		// TODO: escaping?
50 2
		$description = '<description><![CDATA[' . $location->getText() . ']]></description>';
51
52
		$coordinates = '<coordinates>'
53 2
			. $this->escapeValue( $this->getCoordinateString( $location ) )
54 2
			. '</coordinates>';
55
56
		return <<<EOT
57
		<Placemark>
58 2
			$name
59 2
			$description
60
			<Point>
61 2
				$coordinates
62
			</Point>
63
		</Placemark>
64
EOT;
65
	}
66
67 2
	private function getCoordinateString( Location $location ): string {
68
		// lon,lat[,alt]
69 2
		return $location->getCoordinates()->getLongitude()
70 2
			. ',' . $location->getCoordinates()->getLatitude()
71 2
			. ',0';
72
	}
73
74 2
	private function escapeValue( string $value ): string {
75 2
		return htmlspecialchars( $value, ENT_NOQUOTES );
76
	}
77
78
}
79