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

SubObjectBuilder::getSubObjectsFromGeoJson()   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 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\GeoJsonPages\Semantic;
6
7
use GeoJson\Feature\FeatureCollection;
8
use GeoJson\GeoJson;
9
use GeoJson\Geometry\Point;
10
use Title;
11
12
class SubObjectBuilder {
13
14
	private $subjectPage;
15
	private $pointCount = 0;
16
17
	public function __construct( Title $subjectPage ) {
18
		$this->subjectPage = $subjectPage;
19
	}
20
21
	/**
22
	 * @return SubObject[]
23
	 */
24
	public function getSubObjectsFromGeoJson( string $jsonString ) {
25
		$json = json_decode( $jsonString );
26
		$geoJson = GeoJson::jsonUnserialize( $json );
27
28
		return iterator_to_array( $this->featureCollectionToSubObjects( $geoJson ) );
29
	}
30
31
	private function featureCollectionToSubObjects( FeatureCollection $featureCollection ) {
32
		foreach ( $featureCollection->getFeatures() as $feature ) {
33
			$geometry = $feature->getGeometry();
34
35
			if ( $geometry instanceof Point ) {
36
				yield $this->pointToSubobject( $geometry, $feature->getProperties() ?? [] );
37
			}
38
		}
39
	}
40
41
	private function pointToSubobject( Point $point, array $properties ): SubObject {
42
		$subObject = new SubObject( 'Point_' . ++$this->pointCount );
43
44
		$subObject->addPropertyValuePair(
45
			'HasCoordinates',
46
			new \SMWDIGeoCoord( $point->getCoordinates()[1], $point->getCoordinates()[0] )
47
		);
48
49
		if ( array_key_exists( 'description', $properties ) ) {
50
			$subObject->addPropertyValuePair(
51
				'HasDescription',
52
				new \SMWDIBlob( $properties['description'] )
53
			);
54
		}
55
56
		if ( array_key_exists( 'title', $properties ) ) {
57
			$subObject->addPropertyValuePair(
58
				'HasTitle',
59
				new \SMWDIBlob( $properties['title'] )
60
			);
61
		}
62
63
		return $subObject;
64
	}
65
66
67
}
68