Completed
Push — LocationParsers ( afa02e )
by Jeroen De
04:46
created

RectangleParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 64
ccs 20
cts 25
cp 0.8
rs 10
c 0
b 0
f 0

2 Methods

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