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