Completed
Push — cs ( 148090 )
by Jeroen De
03:24
created

LineParser::constructShapeFromLatLongValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maps;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use Maps\Elements\Line;
7
use Maps\Geocoders\Geocoder;
8
use ValueParsers\StringValueParser;
9
use ValueParsers\ValueParser;
10
11
/**
12
 * ValueParser that parses the string representation of a line.
13
 *
14
 * @since 3.0
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Kim Eik
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class LineParser implements ValueParser {
21
22
	private $metaDataSeparator = '~';
23
24
	private $geocoder;
25
26 3
	public function __construct() {
27 3
		$this->geocoder = MapsFactory::newDefault()->newGeocoder();
28 3
	}
29
30 3
	public function setGeocoder( Geocoder $geocoder ) {
31 3
		$this->geocoder = $geocoder;
32 3
	}
33
34
	/**
35
	 * @see StringValueParser::stringParse
36
	 *
37
	 * @since 3.0
38
	 *
39
	 * @param string $value
40
	 *
41
	 * @return Line
42
	 */
43 3
	public function parse( $value ) {
44 3
		$parts = explode( $this->metaDataSeparator, $value );
45
46 3
		$line = $this->constructShapeFromLatLongValues(
47 3
			$this->parseCoordinates(
48 3
				explode( ':', array_shift( $parts ) )
49
			)
50
		);
51
52 3
		$this->handleCommonParams( $parts, $line );
53
54 3
		return $line;
55
	}
56
57 3
	protected function constructShapeFromLatLongValues( array $locations ) {
58 3
		return new Line( $locations );
59
	}
60
61
	/**
62
	 * @since 3.0
63
	 *
64
	 * @param string[] $coordinateStrings
65
	 *
66
	 * @return LatLongValue[]
67
	 */
68 3
	protected function parseCoordinates( array $coordinateStrings ) {
69 3
		$coordinates = [];
70
71 3
		foreach ( $coordinateStrings as $coordinateString ) {
72 3
			$coordinate = $this->geocoder->geocode( $coordinateString );
73
74 3
			if ( $coordinate === null ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
75
				// TODO: good if the user knows something has been omitted
76
			}
77
			else {
78 3
				$coordinates[] = $coordinate;
79
			}
80
		}
81
82 3
		return $coordinates;
83
	}
84
85
	/**
86
	 * This method requires that parameters are positionally correct,
87
	 * 1. Link (one parameter) or bubble data (two parameters)
88
	 * 2. Stroke data (three parameters)
89
	 * 3. Fill data (two parameters)
90
	 * e.g ...title~text~strokeColor~strokeOpacity~strokeWeight~fillColor~fillOpacity
91
	 *
92
	 * @since 3.0
93
	 *
94
	 * @param array $params
95
	 * @param Line $line
96
	 */
97 3
	protected function handleCommonParams( array &$params, Line &$line ) {
98
		//Handle bubble and link parameters
99
100
		//create link data
101 3
		$linkOrTitle = array_shift( $params );
102 3
		if ( $link = $this->isLinkParameter( $linkOrTitle ) ) {
103
			$this->setLinkFromParameter( $line, $link );
104
		}
105
		else {
106
			//create bubble data
107 3
			$this->setBubbleDataFromParameter( $line, $params, $linkOrTitle );
108
		}
109
110
		//handle stroke parameters
111 3
		if ( $color = array_shift( $params ) ) {
112
			$line->setStrokeColor( $color );
113
		}
114
115 3
		if ( $opacity = array_shift( $params ) ) {
116
			$line->setStrokeOpacity( $opacity );
117
		}
118
119 3
		if ( $weight = array_shift( $params ) ) {
120
			$line->setStrokeWeight( $weight );
121
		}
122 3
	}
123
124
	/**
125
	 * Checks if a string is prefixed with link:
126
	 *
127
	 * @static
128
	 *
129
	 * @param $link
130
	 *
131
	 * @return bool|string
132
	 * @since 2.0
133
	 */
134 3
	private function isLinkParameter( $link ) {
135 3
		if ( strpos( $link, 'link:' ) === 0 ) {
136
			return substr( $link, 5 );
137
		}
138
139 3
		return false;
140
	}
141
142
	protected function setLinkFromParameter( Line &$line, $link ) {
143
		if ( filter_var( $link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED ) ) {
144
			$line->setLink( $link );
145
		}
146
		else {
147
			$title = \Title::newFromText( $link );
148
			$line->setLink( $title->getFullURL() );
149
		}
150
	}
151
152 3
	protected function setBubbleDataFromParameter( Line &$line, &$params, $title ) {
153 3
		if ( $title ) {
154 1
			$line->setTitle( $title );
155
		}
156 3
		if ( $text = array_shift( $params ) ) {
157 1
			$line->setText( $text );
158
		}
159 3
	}
160
161
}
162