Completed
Push — master ( 81538e...c2bf57 )
by Jeroen De
10:06
created

GeoJsonContent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\GeoJsonPages;
6
7
use FormatJson;
8
use Maps\MapsFactory;
9
use Maps\GeoJsonPages\GeoJsonMapPageUi;
10
use Maps\Presentation\OutputFacade;
11
use ParserOptions;
12
use ParserOutput;
13
use Status;
14
use Title;
15
16
class GeoJsonContent extends \JsonContent {
17
18
	public const CONTENT_MODEL_ID = 'GeoJSON';
19
20
	public static function newEmptyContentString(): string {
21
		$text = '{"type": "FeatureCollection", "features": []}';
22
		return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK );
23
	}
24
25
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
26
		parent::__construct(
27
			$text,
28
			$modelId
29
		);
30
	}
31
32
	public function getData(): Status {
33
		$status = parent::getData();
34
35
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
36
			return Status::newFatal( 'Invalid GeoJson' );
37
		}
38
39
		return $status;
40
	}
41
42
	private function isGeoJson( $json ): bool {
43
		return property_exists( $json, 'type' )
44
			&& $json->type === 'FeatureCollection'
45
			&& property_exists( $json, 'features' )
46
			&& is_array( $json->features );
47
	}
48
49
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
50
		$generateHtml, ParserOutput &$output ) {
51
52
		if ( !$generateHtml || !$this->isValid() ) {
53
			$output->setText( '' );
54
			return;
55
		}
56
57
		$this->addMapHtmlToOutput( $output );
58
59
		$this->storeSemanticValues( $title, $output );
60
	}
61
62
	private function addMapHtmlToOutput( ParserOutput $output ) {
63
		( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
64
	}
65
66
	private function storeSemanticValues( Title $title, ParserOutput $output ) {
67
		if ( MapsFactory::globalInstance()->smwIntegrationIsEnabled() ) {
68
			MapsFactory::globalInstance()->newSemanticGeoJsonStore( $output, $title )->storeGeoJson( $this->mText );
69
		}
70
	}
71
72
}
73