Completed
Pull Request — master (#603)
by Jeroen De
01:20
created

SubObjectBuilder::featureCollectionToSubObjects()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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