Completed
Push — master ( a9fe00...be45f4 )
by Jeroen De
09:51
created

GeoJsonContent   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 75
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getData() 0 9 3
A isGeoJson() 0 6 4
A fillParserOutput() 0 18 3
A getMapHtml() 0 13 1
A wrapHtmlInThumbDivs() 0 15 1
1
<?php
2
3
namespace Maps\MediaWiki\Content;
4
5
use Html;
6
use ParserOptions;
7
use ParserOutput;
8
use Status;
9
use Title;
10
11
class GeoJsonContent extends \JsonContent {
12
13
	public const CONTENT_MODEL_ID = 'GeoJSON';
14
15
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
16
		parent::__construct( $text, $modelId );
17
	}
18
19
	public function getData(): Status {
20
		$status = parent::getData();
21
22
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
23
			return Status::newFatal( 'Invalid GeoJson' );
24
		}
25
26
		return $status;
27
	}
28
29
	private function isGeoJson( $json ): bool {
30
		return property_exists( $json, 'type' )
31
			&& $json->type === 'FeatureCollection'
32
			&& property_exists( $json, 'features' )
33
			&& is_array( $json->features );
34
	}
35
36
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
37
		$generateHtml, ParserOutput &$output ) {
38
39
		if ( $generateHtml && $this->isValid() ) {
40
			$output->setText(
41
				$this->getMapHtml()
42
				.
43
				Html::element(
44
					'script',
45
					[],
46
					'var GeoJson =' . $this->beautifyJSON() . ';'
47
				)
48
			);
49
			$output->addModules( 'ext.maps.leaflet.editor' );
50
		} else {
51
			$output->setText( '' );
52
		}
53
	}
54
55
	private function getMapHtml(): string {
56
		return $this->wrapHtmlInThumbDivs(
57
			Html::rawElement(
58
				'div',
59
				[
60
					'id' => 'GeoJsonMap',
61
					'style' => "width: 100%; height: 600px; background-color: #eeeeee; overflow: hidden;",
62
					'class' => 'maps-map maps-leaflet GeoJsonMap'
63
				],
64
				wfMessage( 'maps-loading-map' )->inContentLanguage()->escaped()
65
			)
66
		);
67
	}
68
69
	private function wrapHtmlInThumbDivs( string $html ): string {
70
		return Html::rawElement(
71
			'div',
72
			[
73
				'class' => 'thumb'
74
			],
75
			Html::rawElement(
76
				'div',
77
				[
78
					'class' => 'thumbinner'
79
				],
80
				$html
81
			)
82
		);
83
	}
84
85
}
86