Completed
Push — newparam ( 72433c...c5fad3 )
by Jeroen De
01:21
created

GeoJsonContent::getMapHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A GeoJsonContent::isGeoJson() 0 6 4
1
<?php
2
3
namespace Maps\MediaWiki\Content;
4
5
use FormatJson;
6
use Maps\Presentation\GeoJsonMapPageUi;
7
use Maps\Presentation\OutputFacade;
8
use ParserOptions;
9
use ParserOutput;
10
use Status;
11
use Title;
12
13
class GeoJsonContent extends \JsonContent {
14
15
	public const CONTENT_MODEL_ID = 'GeoJSON';
16
17
	public static function newEmptyContentString(): string {
18
		$text = '{"type": "FeatureCollection", "features": []}';
19
		return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK );
20
	}
21
22 2
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
23 2
		parent::__construct(
24 2
			$text,
25
			$modelId
26
		);
27 2
	}
28
29 2
	public function getData(): Status {
30 2
		$status = parent::getData();
31
32 2
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
33 1
			return Status::newFatal( 'Invalid GeoJson' );
34
		}
35
36 1
		return $status;
37
	}
38
39 2
	private function isGeoJson( $json ): bool {
40 2
		return property_exists( $json, 'type' )
41 2
			&& $json->type === 'FeatureCollection'
42 2
			&& property_exists( $json, 'features' )
43 2
			&& is_array( $json->features );
44
	}
45
46
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
47
		$generateHtml, ParserOutput &$output ) {
48
49
		if ( $generateHtml && $this->isValid() ) {
50
			( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
51
		} else {
52
			$output->setText( '' );
53
		}
54
	}
55
56
}
57