Completed
Push — master ( 564666...efaf30 )
by Jeroen De
03:01
created

GeoJsonContent::fillParserOutput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 5
crap 12
1
<?php
2
3
namespace Maps\MediaWiki\Content;
4
5
use FormatJson;
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
	public static function newEmptyContentString(): string {
17
		$text = '{"type": "FeatureCollection", "features": []}';
18
		return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK );
19
	}
20
21 2
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
22 2
		parent::__construct(
23 2
			$text,
24
			$modelId
25
		);
26 2
	}
27
28 2
	public function getData(): Status {
29 2
		$status = parent::getData();
30
31 2
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
32 1
			return Status::newFatal( 'Invalid GeoJson' );
33
		}
34
35 1
		return $status;
36
	}
37
38 2
	private function isGeoJson( $json ): bool {
39 2
		return property_exists( $json, 'type' )
40 2
			&& $json->type === 'FeatureCollection'
41 2
			&& property_exists( $json, 'features' )
42 2
			&& is_array( $json->features );
43
	}
44
45
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
46
		$generateHtml, ParserOutput &$output ) {
47
48
		if ( $generateHtml && $this->isValid() ) {
49
			( new GeoJsonPage( $this->beautifyJSON() ) )->addToParserOutput( $output );
50
		} else {
51
			$output->setText( '' );
52
		}
53
	}
54
55
}
56