Completed
Push — move ( b63aae...04f5b5 )
by Jeroen De
16:21 queued 06:23
created

LineParser::handleCommonParams()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
cc 5
nc 16
nop 2
1
<?php
2
3
namespace Maps\Presentation\WikitextParsers;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use Jeroen\SimpleGeocoder\Geocoder;
7
use Maps\Elements\Line;
8
use Maps\MapsFactory;
9
use ValueParsers\StringValueParser;
10
use ValueParsers\ValueParser;
11
12
/**
13
 * ValueParser that parses the string representation of a line.
14
 *
15
 * @since 3.0
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Kim Eik
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class LineParser implements ValueParser {
22
23
	private $metaDataSeparator = '~';
24
25
	private $geocoder = null;
26
27
	public function setGeocoder( Geocoder $geocoder ) {
28
		$this->geocoder = $geocoder;
29
	}
30
31
	private function getGeocoder(): Geocoder {
32
		if ( $this->geocoder == null ) {
33
			$this->geocoder = MapsFactory::newDefault()->newGeocoder();
34
		}
35
36
		return $this->geocoder;
37
	}
38
39
	/**
40
	 * @see StringValueParser::stringParse
41
	 *
42
	 * @since 3.0
43
	 *
44
	 * @param string $value
45
	 *
46
	 * @return Line
47
	 */
48
	public function parse( $value ) {
49
		$parts = explode( $this->metaDataSeparator, $value );
50
51
		$line = $this->constructShapeFromLatLongValues(
52
			$this->parseCoordinates(
53
				explode( ':', array_shift( $parts ) )
54
			)
55
		);
56
57
		$this->handleCommonParams( $parts, $line );
58
59
		return $line;
60
	}
61
62
	protected function constructShapeFromLatLongValues( array $locations ) {
63
		return new Line( $locations );
64
	}
65
66
	/**
67
	 * @since 3.0
68
	 *
69
	 * @param string[] $coordinateStrings
70
	 *
71
	 * @return LatLongValue[]
72
	 */
73
	protected function parseCoordinates( array $coordinateStrings ): array {
74
		$coordinates = [];
75
76
		foreach ( $coordinateStrings as $coordinateString ) {
77
			$coordinate = $this->getGeocoder()->geocode( $coordinateString );
78
79
			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...
80
				// TODO: good if the user knows something has been omitted
81
			} else {
82
				$coordinates[] = $coordinate;
83
			}
84
		}
85
86
		return $coordinates;
87
	}
88
89
	/**
90
	 * This method requires that parameters are positionally correct,
91
	 * 1. Link (one parameter) or bubble data (two parameters)
92
	 * 2. Stroke data (three parameters)
93
	 * 3. Fill data (two parameters)
94
	 * e.g ...title~text~strokeColor~strokeOpacity~strokeWeight~fillColor~fillOpacity
95
	 *
96
	 * @since 3.0
97
	 *
98
	 * @param array $params
99
	 * @param Line $line
100
	 */
101
	protected function handleCommonParams( array &$params, Line &$line ) {
102
		//Handle bubble and link parameters
103
104
		//create link data
105
		$linkOrTitle = array_shift( $params );
106
		if ( $link = $this->isLinkParameter( $linkOrTitle ) ) {
107
			$this->setLinkFromParameter( $line, $link );
108
		} else {
109
			//create bubble data
110
			$this->setBubbleDataFromParameter( $line, $params, $linkOrTitle );
111
		}
112
113
		//handle stroke parameters
114
		if ( $color = array_shift( $params ) ) {
115
			$line->setStrokeColor( $color );
116
		}
117
118
		if ( $opacity = array_shift( $params ) ) {
119
			$line->setStrokeOpacity( $opacity );
120
		}
121
122
		if ( $weight = array_shift( $params ) ) {
123
			$line->setStrokeWeight( $weight );
124
		}
125
	}
126
127
	/**
128
	 * Checks if a string is prefixed with link:
129
	 *
130
	 * @static
131
	 *
132
	 * @param $link
133
	 *
134
	 * @return bool|string
135
	 * @since 2.0
136
	 */
137
	private function isLinkParameter( $link ) {
138
		if ( strpos( $link, 'link:' ) === 0 ) {
139
			return substr( $link, 5 );
140
		}
141
142
		return false;
143
	}
144
145
	protected function setLinkFromParameter( Line &$line, $link ) {
146
		if ( filter_var( $link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED ) ) {
147
			$line->setLink( $link );
148
		} else {
149
			$title = \Title::newFromText( $link );
150
			$line->setLink( $title->getFullURL() );
151
		}
152
	}
153
154
	protected function setBubbleDataFromParameter( Line &$line, &$params, $title ) {
155
		if ( $title ) {
156
			$line->setTitle( $title );
157
		}
158
		if ( $text = array_shift( $params ) ) {
159
			$line->setText( $text );
160
		}
161
	}
162
163
}
164