1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\Presentation\WikitextParsers; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\LatLongValue; |
6
|
|
|
use Jeroen\SimpleGeocoder\Geocoder; |
7
|
|
|
use Maps\Elements\Circle; |
8
|
|
|
use Maps\MapsFactory; |
9
|
|
|
use ValueParsers\ParseException; |
10
|
|
|
use ValueParsers\StringValueParser; |
11
|
|
|
use ValueParsers\ValueParser; |
12
|
|
|
|
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 CircleParser implements ValueParser { |
21
|
|
|
|
22
|
|
|
private $metaDataSeparator = '~'; |
23
|
|
|
|
24
|
|
|
private $geocoder; |
25
|
|
|
|
26
|
|
|
public function __construct( $geocoder = null ) { |
27
|
|
|
$this->geocoder = $geocoder instanceof Geocoder ? $geocoder : MapsFactory::newDefault()->newGeocoder(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see StringValueParser::stringParse |
32
|
|
|
* |
33
|
|
|
* @since 3.0 |
34
|
|
|
* |
35
|
|
|
* @param string $value |
36
|
|
|
* |
37
|
|
|
* @return Circle |
38
|
|
|
*/ |
39
|
|
|
public function parse( $value ) { |
40
|
|
|
$metaData = explode( $this->metaDataSeparator, $value ); |
41
|
|
|
$circleData = explode( ':', array_shift( $metaData ) ); |
42
|
|
|
|
43
|
|
|
$circle = new Circle( $this->stringToLatLongValue( $circleData[0] ), (float)$circleData[1] ); |
44
|
|
|
|
45
|
|
|
if ( $metaData !== [] ) { |
46
|
|
|
$circle->setTitle( array_shift( $metaData ) ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ( $metaData !== [] ) { |
50
|
|
|
$circle->setText( array_shift( $metaData ) ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ( $metaData !== [] ) { |
54
|
|
|
$circle->setStrokeColor( array_shift( $metaData ) ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ( $metaData !== [] ) { |
58
|
|
|
$circle->setStrokeOpacity( array_shift( $metaData ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( $metaData !== [] ) { |
62
|
|
|
$circle->setStrokeWeight( array_shift( $metaData ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ( $metaData !== [] ) { |
66
|
|
|
$circle->setFillColor( array_shift( $metaData ) ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( $metaData !== [] ) { |
70
|
|
|
$circle->setFillOpacity( array_shift( $metaData ) ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $circle; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function stringToLatLongValue( string $location ): LatLongValue { |
77
|
|
|
$latLong = $this->geocoder->geocode( $location ); |
78
|
|
|
|
79
|
|
|
if ( $latLong === null ) { |
80
|
|
|
throw new ParseException( 'Failed to parse or geocode' ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $latLong; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|