Completed
Push — styling ( 65b228 )
by Jeroen De
04:06
created

GeoJsonContent   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 0
loc 44
ccs 18
cts 22
cp 0.8182
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A newEmptyContentString() 0 4 1
A __construct() 0 6 1
A getData() 0 9 3
A isGeoJson() 0 6 4
A fillParserOutput() 0 9 3
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 3
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
23 3
		parent::__construct(
24 3
			$text,
25
			$modelId
26
		);
27 3
	}
28
29 3
	public function getData(): Status {
30 3
		$status = parent::getData();
31
32 3
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
33 1
			return Status::newFatal( 'Invalid GeoJson' );
34
		}
35
36 2
		return $status;
37
	}
38
39 3
	private function isGeoJson( $json ): bool {
40 3
		return property_exists( $json, 'type' )
41 3
			&& $json->type === 'FeatureCollection'
42 3
			&& property_exists( $json, 'features' )
43 3
			&& is_array( $json->features );
44
	}
45
46 1
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
47
		$generateHtml, ParserOutput &$output ) {
48
49 1
		if ( $generateHtml && $this->isValid() ) {
50 1
			( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
51
		} else {
52
			$output->setText( '' );
53
		}
54 1
	}
55
56
}
57