Completed
Push — geojsonsmw ( 4cff82 )
by Jeroen De
03:44
created

GeoJsonContent::fillParserOutput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 5
cts 7
cp 0.7143
cc 3
nc 2
nop 5
crap 3.2098
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 SMW\ApplicationFactory;
11
use SMW\DIProperty;
12
use Status;
13
use Title;
14
15
class GeoJsonContent extends \JsonContent {
16
17
	public const CONTENT_MODEL_ID = 'GeoJSON';
18
19
	public static function newEmptyContentString(): string {
20
		$text = '{"type": "FeatureCollection", "features": []}';
21
		return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK );
22
	}
23
24 5
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
25 5
		parent::__construct(
26 5
			$text,
27
			$modelId
28
		);
29 5
	}
30
31 5
	public function getData(): Status {
32 5
		$status = parent::getData();
33
34 5
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
35 1
			return Status::newFatal( 'Invalid GeoJson' );
36
		}
37
38 4
		return $status;
39
	}
40
41 5
	private function isGeoJson( $json ): bool {
42 5
		return property_exists( $json, 'type' )
43 5
			&& $json->type === 'FeatureCollection'
44 5
			&& property_exists( $json, 'features' )
45 5
			&& is_array( $json->features );
46
	}
47
48 3
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
49
		$generateHtml, ParserOutput &$output ) {
50
51 3
		if ( !$generateHtml || !$this->isValid() ) {
52
			$output->setText( '' );
53
			return;
54
		}
55
56 3
		$this->addMapHtmlToOutput( $output );
57
58 3
		$this->todoStoreSomeSmwStuff( $title, $output );
59 3
	}
60
61 3
	private function addMapHtmlToOutput( ParserOutput $output ) {
62 3
		( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
63 3
	}
64
65
	// TODO
66 3
	private function todoStoreSomeSmwStuff( Title $title, ParserOutput $output ) {
67 3
		$parserData = ApplicationFactory::getInstance()->newParserData( $title, $output );
68
69 3
		$parserData->getSemanticData()->addPropertyObjectValue(
70 3
			new DIProperty( 'HasNumber' ),
71 3
			new \SMWDINumber( 42 )
72
		);
73
74 3
		$parserData->copyToParserOutput();
75 3
	}
76
77
}
78