1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps; |
4
|
|
|
|
5
|
|
|
use Maps\Elements\Rectangle; |
6
|
|
|
use ValueParsers\ParseException; |
7
|
|
|
use ValueParsers\StringValueParser; |
8
|
|
|
use ValueParsers\ValueParser; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @since 3.0 |
12
|
|
|
* |
13
|
|
|
* @licence GNU GPL v2+ |
14
|
|
|
* @author Kim Eik |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class RectangleParser implements ValueParser { |
18
|
|
|
|
19
|
|
|
private $metaDataSeparator = '~'; |
20
|
|
|
|
21
|
|
|
private $geocoder; |
22
|
|
|
|
23
|
|
|
public function __construct() { |
24
|
|
|
$this->geocoder = MapsFactory::newDefault()->newGeocoder(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @see StringValueParser::stringParse |
29
|
|
|
* |
30
|
|
|
* @since 3.0 |
31
|
|
|
* |
32
|
|
|
* @param string $value |
33
|
|
|
* |
34
|
|
|
* @return Rectangle |
35
|
|
|
*/ |
36
|
|
|
public function parse( $value ) { |
37
|
|
|
$metaData = explode( $this->metaDataSeparator , $value ); |
38
|
|
|
$rectangleData = explode( ':' , array_shift( $metaData ) ); |
39
|
|
|
|
40
|
|
|
$rectangle = new Rectangle( |
41
|
|
|
$this->stringToLatLongValue( $rectangleData[0] ), |
|
|
|
|
42
|
|
|
$this->stringToLatLongValue( $rectangleData[1] ) |
|
|
|
|
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
if ( $metaData !== [] ) { |
46
|
|
|
$rectangle->setTitle( array_shift( $metaData ) ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ( $metaData !== [] ) { |
50
|
|
|
$rectangle->setText( array_shift( $metaData ) ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ( $metaData !== [] ) { |
54
|
|
|
$rectangle->setStrokeColor( array_shift( $metaData ) ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ( $metaData !== [] ) { |
58
|
|
|
$rectangle->setStrokeOpacity( array_shift( $metaData ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( $metaData !== [] ) { |
62
|
|
|
$rectangle->setStrokeWeight( array_shift( $metaData ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $rectangle; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function stringToLatLongValue( $location ) { |
69
|
|
|
$latLong = $this->geocoder->geocode( $location ); |
70
|
|
|
|
71
|
|
|
if ( $location === null ) { |
72
|
|
|
throw new ParseException( 'Failed to parse or geocode' ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $latLong; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: