Completed
Push — address-as-title ( 9b6eeb...601934 )
by Peter
11:07
created

CircleParser::stringParse()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 4.6666
cc 8
eloc 19
nc 128
nop 1
1
<?php
2
3
namespace Maps;
4
5
use DataValues\Geo\Parsers\GeoCoordinateParser;
6
use DataValues\Geo\Values\LatLongValue;
7
use Maps\Elements\Circle;
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 CircleParser 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 Circle
33
	 */
34
	public function stringParse( $value ) {
35
		$metaData = explode( $this->metaDataSeparator , $value );
36
		$circleData = explode( ':' , array_shift( $metaData ) );
37
38
		$circle = new Circle( $this->stringToLatLongValue( $circleData[0] ), (float)$circleData[1] );
39
40
		if ( $metaData !== [] ) {
41
			$circle->setTitle( array_shift( $metaData ) );
42
		}
43
44
		if ( $metaData !== [] ) {
45
			$circle->setText( array_shift( $metaData ) );
46
		}
47
48
		if ( $metaData !== [] ) {
49
			$circle->setStrokeColor( array_shift( $metaData ) );
50
		}
51
52
		if ( $metaData !== [] ) {
53
			$circle->setStrokeOpacity( array_shift( $metaData ) );
54
		}
55
56
		if ( $metaData !== [] ) {
57
			$circle->setStrokeWeight( array_shift( $metaData ) );
58
		}
59
60
		if ( $metaData !== [] ) {
61
			$circle->setFillColor( array_shift( $metaData ) );
62
		}
63
64
		if ( $metaData !== [] ) {
65
			$circle->setFillOpacity( array_shift( $metaData ) );
66
		}
67
68
		return $circle;
69
	}
70
71
	private function stringToLatLongValue( $location ) {
72
		if ( $this->supportGeocoding && Geocoders::canGeocode() ) {
73
			$location = Geocoders::attemptToGeocode( $location );
74
75
			if ( $location === false ) {
76
				throw new ParseException( 'Failed to parse or geocode' );
77
			}
78
79
			assert( $location instanceof LatLongValue );
80
			return $location;
81
		}
82
83
		$parser = new GeoCoordinateParser( new \ValueParsers\ParserOptions() );
84
		return $parser->parse( $location );
85
	}
86
87
}
88