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

RectangleParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
B parse() 0 39 8
A stringToLatLongValue() 0 9 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\WikitextParsers;
6
7
use DataValues\Geo\Values\LatLongValue;
8
use Jeroen\SimpleGeocoder\Geocoder;
9
use Maps\LegacyModel\Rectangle;
10
use Maps\MapsFactory;
11
use ValueParsers\ParseException;
12
use ValueParsers\StringValueParser;
13
use ValueParsers\ValueParser;
14
15
/**
16
 * @since 3.0
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Kim Eik
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class RectangleParser implements ValueParser {
23
24
	private $metaDataSeparator = '~';
25
26
	private $geocoder;
27
28
	public function __construct( $geocoder = null ) {
29
		$this->geocoder = $geocoder instanceof Geocoder ? $geocoder : MapsFactory::globalInstance()->getGeocoder();
30
	}
31
32
	/**
33
	 * @see StringValueParser::stringParse
34
	 *
35
	 * @since 3.0
36
	 *
37
	 * @param string $value
38
	 *
39
	 * @return Rectangle
40
	 */
41
	public function parse( $value ) {
42
		$metaData = explode( $this->metaDataSeparator, $value );
43
		$rectangleData = explode( ':', array_shift( $metaData ) );
44
45
		$rectangle = new Rectangle(
46
			$this->stringToLatLongValue( $rectangleData[0] ),
47
			$this->stringToLatLongValue( $rectangleData[1] )
48
		);
49
50
		if ( $metaData !== [] ) {
51
			$rectangle->setTitle( array_shift( $metaData ) );
52
		}
53
54
		if ( $metaData !== [] ) {
55
			$rectangle->setText( array_shift( $metaData ) );
56
		}
57
58
		if ( $metaData !== [] ) {
59
			$rectangle->setStrokeColor( array_shift( $metaData ) );
60
		}
61
62
		if ( $metaData !== [] ) {
63
			$rectangle->setStrokeOpacity( array_shift( $metaData ) );
64
		}
65
66
		if ( $metaData !== [] ) {
67
			$rectangle->setStrokeWeight( array_shift( $metaData ) );
68
		}
69
70
		if ( $metaData !== [] ) {
71
			$rectangle->setFillColor( array_shift( $metaData ) );
72
		}
73
74
		if ( $metaData !== [] ) {
75
			$rectangle->setFillOpacity( array_shift( $metaData ) );
76
		}
77
78
		return $rectangle;
79
	}
80
81
	private function stringToLatLongValue( string $location ): LatLongValue {
82
		$latLong = $this->geocoder->geocode( $location );
83
84
		if ( $latLong === null ) {
85
			throw new ParseException( 'Failed to parse or geocode' );
86
		}
87
88
		return $latLong;
89
	}
90
91
}
92