1 | <?php |
||
21 | class LocationParser implements ValueParser { |
||
22 | |||
23 | private $geocoder; |
||
24 | private $useAddressAsTitle; |
||
25 | |||
26 | /** |
||
27 | * @deprecated Use newInstance instead |
||
28 | */ |
||
29 | public function __construct( $enableLegacyCrud = true ) { |
||
30 | if ( $enableLegacyCrud ) { |
||
31 | $this->geocoder = MapsFactory::newDefault()->newGeocoder(); |
||
32 | $this->useAddressAsTitle = false; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | 10 | public static function newInstance( Geocoder $geocoder, bool $useAddressAsTitle = false ): self { |
|
37 | 10 | $instance = new self( false ); |
|
38 | 10 | $instance->geocoder = $geocoder; |
|
39 | 10 | $instance->useAddressAsTitle = $useAddressAsTitle; |
|
40 | 10 | return $instance; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @see StringValueParser::stringParse |
||
45 | * |
||
46 | * @since 3.0 |
||
47 | * |
||
48 | * @param string $value |
||
49 | * |
||
50 | * @return Location |
||
51 | * @throws ParseException |
||
52 | */ |
||
53 | 10 | public function parse( $value ) { |
|
54 | 10 | $separator = '~'; |
|
55 | |||
56 | 10 | $metaData = explode( $separator, $value ); |
|
57 | |||
58 | 10 | $coordinatesOrAddress = array_shift( $metaData ); |
|
59 | 10 | $coordinates = $this->geocoder->geocode( $coordinatesOrAddress ); |
|
60 | |||
61 | 10 | if ( $coordinates === null ) { |
|
62 | throw new ParseException( 'Location is not a parsable coordinate and not a geocodable address' ); |
||
63 | } |
||
64 | |||
65 | 10 | $location = new Location( $coordinates ); |
|
66 | |||
67 | 10 | if ( $metaData !== [] ) { |
|
68 | 8 | $this->setTitleOrLink( $location, array_shift( $metaData ) ); |
|
69 | } else { |
||
70 | 2 | if ( $this->useAddressAsTitle && $this->isAddress( $coordinatesOrAddress ) ) { |
|
71 | 1 | $location->setTitle( $coordinatesOrAddress ); |
|
72 | } |
||
73 | } |
||
74 | |||
75 | 10 | if ( $metaData !== [] ) { |
|
76 | $location->setText( array_shift( $metaData ) ); |
||
77 | } |
||
78 | |||
79 | 10 | if ( $metaData !== [] ) { |
|
80 | $location->setIcon( array_shift( $metaData ) ); |
||
81 | } |
||
82 | |||
83 | 10 | if ( $metaData !== [] ) { |
|
84 | $location->setGroup( array_shift( $metaData ) ); |
||
85 | } |
||
86 | |||
87 | 10 | if ( $metaData !== [] ) { |
|
88 | $location->setInlineLabel( array_shift( $metaData ) ); |
||
89 | } |
||
90 | |||
91 | 10 | return $location; |
|
92 | } |
||
93 | |||
94 | 8 | private function setTitleOrLink( Location $location, $titleOrLink ) { |
|
95 | 8 | if ( $this->isLink( $titleOrLink ) ) { |
|
96 | 2 | $this->setLink( $location, $titleOrLink ); |
|
97 | } else { |
||
98 | 6 | $location->setTitle( $titleOrLink ); |
|
99 | } |
||
100 | 8 | } |
|
101 | |||
102 | 8 | private function isLink( $value ) { |
|
105 | |||
106 | 2 | private function setLink( Location $location, $link ) { |
|
107 | 2 | $link = substr( $link, 5 ); |
|
108 | 2 | $location->setLink( $this->getExpandedLink( $link ) ); |
|
109 | 2 | } |
|
110 | |||
111 | 2 | private function getExpandedLink( $link ) { |
|
124 | |||
125 | /** |
||
126 | * @param string $coordsOrAddress |
||
127 | * |
||
128 | * @return boolean |
||
129 | */ |
||
130 | 2 | private function isAddress( $coordsOrAddress ) { |
|
142 | |||
143 | } |
||
144 |