1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\LatLongValue; |
6
|
|
|
use Jeroen\SimpleGeocoder\Geocoder; |
7
|
|
|
use Maps\Elements\Line; |
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
|
|
|
public function __construct() { |
27
|
|
|
$this->geocoder = MapsFactory::newDefault()->newGeocoder(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setGeocoder( Geocoder $geocoder ) { |
31
|
|
|
$this->geocoder = $geocoder; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @see StringValueParser::stringParse |
36
|
|
|
* |
37
|
|
|
* @since 3.0 |
38
|
|
|
* |
39
|
|
|
* @param string $value |
40
|
|
|
* |
41
|
|
|
* @return Line |
42
|
|
|
*/ |
43
|
|
|
public function parse( $value ) { |
44
|
|
|
$parts = explode( $this->metaDataSeparator, $value ); |
45
|
|
|
|
46
|
|
|
$line = $this->constructShapeFromLatLongValues( |
47
|
|
|
$this->parseCoordinates( |
48
|
|
|
explode( ':', array_shift( $parts ) ) |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->handleCommonParams( $parts, $line ); |
53
|
|
|
|
54
|
|
|
return $line; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function constructShapeFromLatLongValues( array $locations ) { |
58
|
|
|
return new Line( $locations ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @since 3.0 |
63
|
|
|
* |
64
|
|
|
* @param string[] $coordinateStrings |
65
|
|
|
* |
66
|
|
|
* @return LatLongValue[] |
67
|
|
|
*/ |
68
|
|
|
protected function parseCoordinates( array $coordinateStrings ) { |
69
|
|
|
$coordinates = []; |
70
|
|
|
|
71
|
|
|
foreach ( $coordinateStrings as $coordinateString ) { |
72
|
|
|
$coordinate = $this->geocoder->geocode( $coordinateString ); |
73
|
|
|
|
74
|
|
|
if ( $coordinate === null ) { |
|
|
|
|
75
|
|
|
// TODO: good if the user knows something has been omitted |
76
|
|
|
} else { |
77
|
|
|
$coordinates[] = $coordinate; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $coordinates; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This method requires that parameters are positionally correct, |
86
|
|
|
* 1. Link (one parameter) or bubble data (two parameters) |
87
|
|
|
* 2. Stroke data (three parameters) |
88
|
|
|
* 3. Fill data (two parameters) |
89
|
|
|
* e.g ...title~text~strokeColor~strokeOpacity~strokeWeight~fillColor~fillOpacity |
90
|
|
|
* |
91
|
|
|
* @since 3.0 |
92
|
|
|
* |
93
|
|
|
* @param array $params |
94
|
|
|
* @param Line $line |
95
|
|
|
*/ |
96
|
|
|
protected function handleCommonParams( array &$params, Line &$line ) { |
97
|
|
|
//Handle bubble and link parameters |
98
|
|
|
|
99
|
|
|
//create link data |
100
|
|
|
$linkOrTitle = array_shift( $params ); |
101
|
|
|
if ( $link = $this->isLinkParameter( $linkOrTitle ) ) { |
102
|
|
|
$this->setLinkFromParameter( $line, $link ); |
103
|
|
|
} else { |
104
|
|
|
//create bubble data |
105
|
|
|
$this->setBubbleDataFromParameter( $line, $params, $linkOrTitle ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//handle stroke parameters |
109
|
|
|
if ( $color = array_shift( $params ) ) { |
110
|
|
|
$line->setStrokeColor( $color ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ( $opacity = array_shift( $params ) ) { |
114
|
|
|
$line->setStrokeOpacity( $opacity ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( $weight = array_shift( $params ) ) { |
118
|
|
|
$line->setStrokeWeight( $weight ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Checks if a string is prefixed with link: |
124
|
|
|
* |
125
|
|
|
* @static |
126
|
|
|
* |
127
|
|
|
* @param $link |
128
|
|
|
* |
129
|
|
|
* @return bool|string |
130
|
|
|
* @since 2.0 |
131
|
|
|
*/ |
132
|
|
|
private function isLinkParameter( $link ) { |
133
|
|
|
if ( strpos( $link, 'link:' ) === 0 ) { |
134
|
|
|
return substr( $link, 5 ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function setLinkFromParameter( Line &$line, $link ) { |
141
|
|
|
if ( filter_var( $link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED ) ) { |
142
|
|
|
$line->setLink( $link ); |
143
|
|
|
} else { |
144
|
|
|
$title = \Title::newFromText( $link ); |
145
|
|
|
$line->setLink( $title->getFullURL() ); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function setBubbleDataFromParameter( Line &$line, &$params, $title ) { |
150
|
|
|
if ( $title ) { |
151
|
|
|
$line->setTitle( $title ); |
152
|
|
|
} |
153
|
|
|
if ( $text = array_shift( $params ) ) { |
154
|
|
|
$line->setText( $text ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
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 theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.