RectangleParser::stringToLatLongValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
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 6
	public function __construct( $geocoder = null ) {
29 6
		$this->geocoder = $geocoder instanceof Geocoder ? $geocoder : MapsFactory::globalInstance()->getGeocoder();
30 6
	}
31
32
	/**
33
	 * @see StringValueParser::stringParse
34
	 *
35
	 * @since 3.0
36
	 *
37
	 * @param string $value
38
	 *
39
	 * @return Rectangle
40
	 */
41 6
	public function parse( $value ) {
42 6
		$metaData = explode( $this->metaDataSeparator, $value );
43 6
		$rectangleData = explode( ':', array_shift( $metaData ) );
44
45 6
		$rectangle = new Rectangle(
46 6
			$this->stringToLatLongValue( $rectangleData[0] ),
47 6
			$this->stringToLatLongValue( $rectangleData[1] )
48
		);
49
50 6
		if ( $metaData !== [] ) {
51 5
			$rectangle->setTitle( array_shift( $metaData ) );
52
		}
53
54 6
		if ( $metaData !== [] ) {
55 4
			$rectangle->setText( array_shift( $metaData ) );
56
		}
57
58 6
		if ( $metaData !== [] ) {
59 3
			$rectangle->setStrokeColor( array_shift( $metaData ) );
60
		}
61
62 6
		if ( $metaData !== [] ) {
63 2
			$rectangle->setStrokeOpacity( array_shift( $metaData ) );
64
		}
65
66 6
		if ( $metaData !== [] ) {
67 2
			$rectangle->setStrokeWeight( array_shift( $metaData ) );
68
		}
69
70 6
		if ( $metaData !== [] ) {
71 2
			$rectangle->setFillColor( array_shift( $metaData ) );
72
		}
73
74 6
		if ( $metaData !== [] ) {
75 2
			$rectangle->setFillOpacity( array_shift( $metaData ) );
76
		}
77
78 6
		return $rectangle;
79
	}
80
81 6
	private function stringToLatLongValue( string $location ): LatLongValue {
82 6
		$latLong = $this->geocoder->geocode( $location );
83
84 6
		if ( $latLong === null ) {
85
			throw new ParseException( 'Failed to parse or geocode' );
86
		}
87
88 6
		return $latLong;
89
	}
90
91
}
92