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
|
3 |
|
public function setGeocoder( Geocoder $geocoder ) { |
30
|
3 |
|
$this->geocoder = $geocoder; |
31
|
3 |
|
} |
32
|
|
|
|
33
|
3 |
|
private function getGeocoder(): Geocoder { |
34
|
3 |
|
if ( $this->geocoder == null ) { |
35
|
|
|
$this->geocoder = MapsFactory::globalInstance()->getGeocoder(); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
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
|
3 |
|
public function parse( $value ) { |
51
|
3 |
|
$parts = explode( $this->metaDataSeparator, $value ); |
52
|
|
|
|
53
|
3 |
|
$line = $this->constructShapeFromLatLongValues( |
54
|
3 |
|
$this->parseCoordinates( |
55
|
3 |
|
explode( ':', array_shift( $parts ) ) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
|
59
|
3 |
|
$this->handleCommonParams( $parts, $line ); |
60
|
|
|
|
61
|
3 |
|
return $line; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
protected function constructShapeFromLatLongValues( array $locations ) { |
65
|
3 |
|
return new Line( $locations ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @since 3.0 |
70
|
|
|
* |
71
|
|
|
* @param string[] $coordinateStrings |
72
|
|
|
* |
73
|
|
|
* @return LatLongValue[] |
74
|
|
|
*/ |
75
|
3 |
|
protected function parseCoordinates( array $coordinateStrings ): array { |
76
|
3 |
|
$coordinates = []; |
77
|
|
|
|
78
|
3 |
|
foreach ( $coordinateStrings as $coordinateString ) { |
79
|
3 |
|
$coordinate = $this->getGeocoder()->geocode( $coordinateString ); |
80
|
|
|
|
81
|
3 |
|
if ( $coordinate !== null ) { |
82
|
|
|
// TODO: good if the user knows something has been omitted |
83
|
3 |
|
$coordinates[] = $coordinate; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
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
|
3 |
|
protected function handleCommonParams( array &$params, Line &$line ) { |
103
|
|
|
//Handle bubble and link parameters |
104
|
|
|
|
105
|
|
|
//create link data |
106
|
3 |
|
$linkOrTitle = array_shift( $params ) ?? ''; |
107
|
3 |
|
if ( $link = $this->isLinkParameter( $linkOrTitle ) ) { |
108
|
|
|
$this->setLinkFromParameter( $line, $link ); |
109
|
|
|
} else { |
110
|
|
|
//create bubble data |
111
|
3 |
|
$this->setBubbleDataFromParameter( $line, $params, $linkOrTitle ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//handle stroke parameters |
115
|
3 |
|
if ( $color = array_shift( $params ) ) { |
116
|
|
|
$line->setStrokeColor( $color ); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
if ( $opacity = array_shift( $params ) ) { |
120
|
|
|
$line->setStrokeOpacity( $opacity ); |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
if ( $weight = array_shift( $params ) ) { |
124
|
|
|
$line->setStrokeWeight( $weight ); |
125
|
|
|
} |
126
|
3 |
|
} |
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
|
3 |
|
private function isLinkParameter( $link ) { |
139
|
3 |
|
if ( strpos( $link, 'link:' ) === 0 ) { |
140
|
|
|
return substr( $link, 5 ); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
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
|
3 |
|
protected function setBubbleDataFromParameter( Line &$line, &$params, $title ) { |
156
|
3 |
|
if ( $title ) { |
157
|
1 |
|
$line->setTitle( $title ); |
158
|
|
|
} |
159
|
3 |
|
if ( $text = array_shift( $params ) ) { |
160
|
1 |
|
$line->setText( $text ); |
161
|
|
|
} |
162
|
3 |
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|