Completed
Push — master ( be45f4...df5ccc )
by Jeroen De
03:42
created

GeoJsonContent::wrapHtmlInThumbDivs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Maps\MediaWiki\Content;
4
5
use Html;
6
use Maps\Presentation\GeoJsonPage;
7
use ParserOptions;
8
use ParserOutput;
9
use Status;
10
use Title;
11
12
class GeoJsonContent extends \JsonContent {
13
14
	public const CONTENT_MODEL_ID = 'GeoJSON';
15
16 2
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
17 2
		parent::__construct(
18 2
			$text,
19
			$modelId
20
		);
21 2
	}
22
23 2
	public function getData(): Status {
24 2
		$status = parent::getData();
25
26 2
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
27 1
			return Status::newFatal( 'Invalid GeoJson' );
28
		}
29
30 1
		return $status;
31
	}
32
33 2
	private function isGeoJson( $json ): bool {
34 2
		return property_exists( $json, 'type' )
35 2
			&& $json->type === 'FeatureCollection'
36 2
			&& property_exists( $json, 'features' )
37 2
			&& is_array( $json->features );
38
	}
39
40
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
41
		$generateHtml, ParserOutput &$output ) {
42
43
		if ( $generateHtml && $this->isValid() ) {
44
			$output->setText(
45
				( new GeoJsonPage() )->getMapHtml()
46
				.
47
				Html::element(
48
					'script',
49
					[],
50
					'var GeoJson =' . $this->beautifyJSON() . ';'
51
				)
52
			);
53
			$output->addModules( 'ext.maps.leaflet.editor' );
54
		} else {
55
			$output->setText( '' );
56
		}
57
	}
58
59
60
61
}
62