1 | <?php |
||
22 | class LocationParser implements ValueParser { |
||
23 | |||
24 | private $geocoder; |
||
25 | private $useAddressAsTitle; |
||
26 | |||
27 | /** |
||
28 | * @deprecated Use newInstance instead |
||
29 | */ |
||
30 | 84 | public function __construct( $enableLegacyCrud = true ) { |
|
31 | 84 | if ( $enableLegacyCrud ) { |
|
32 | 55 | $this->geocoder = MapsFactory::newDefault()->newGeocoder(); |
|
33 | 55 | $this->useAddressAsTitle = false; |
|
34 | } |
||
35 | 84 | } |
|
36 | |||
37 | 29 | public static function newInstance( Geocoder $geocoder, bool $useAddressAsTitle = false ): self { |
|
43 | |||
44 | /** |
||
45 | * @see StringValueParser::stringParse |
||
46 | * |
||
47 | * @since 3.0 |
||
48 | * |
||
49 | * @param string $value |
||
50 | * |
||
51 | * @return Location |
||
52 | * @throws ParseException |
||
53 | */ |
||
54 | 79 | public function parse( $value ) { |
|
55 | 79 | $separator = '~'; |
|
56 | |||
57 | 79 | $metaData = explode( $separator, $value ); |
|
58 | |||
59 | 79 | $coordinatesOrAddress = array_shift( $metaData ); |
|
60 | 79 | $coordinates = $this->geocoder->geocode( $coordinatesOrAddress ); |
|
61 | |||
62 | 79 | if ( $coordinates === null ) { |
|
63 | 1 | throw new ParseException( 'Location is not a parsable coordinate and not a geocodable address' ); |
|
64 | } |
||
65 | |||
66 | 78 | $location = new Location( $coordinates ); |
|
67 | |||
68 | 78 | if ( $metaData !== [] ) { |
|
69 | 11 | $this->setTitleOrLink( $location, array_shift( $metaData ) ); |
|
70 | } else { |
||
71 | 67 | if ( $this->useAddressAsTitle && $this->isAddress( $coordinatesOrAddress ) ) { |
|
72 | 1 | $location->setTitle( $coordinatesOrAddress ); |
|
73 | } |
||
74 | } |
||
75 | |||
76 | 78 | if ( $metaData !== [] ) { |
|
77 | 2 | $location->setText( array_shift( $metaData ) ); |
|
78 | } |
||
79 | |||
80 | 78 | if ( $metaData !== [] ) { |
|
81 | // FIXME: global access |
||
82 | 1 | $location->setIcon( MapsMapper::getFileUrl( array_shift( $metaData ) ) ); |
|
|
|||
83 | } |
||
84 | |||
85 | 78 | if ( $metaData !== [] ) { |
|
86 | 1 | $location->setGroup( array_shift( $metaData ) ); |
|
87 | } |
||
88 | |||
89 | 78 | if ( $metaData !== [] ) { |
|
90 | 1 | $location->setInlineLabel( array_shift( $metaData ) ); |
|
91 | } |
||
92 | |||
93 | 78 | if ( $metaData !== [] ) { |
|
94 | // FIXME: global access |
||
95 | $location->setVisitedIcon( MapsMapper::getFileUrl( array_shift( $metaData ) ) ) ; |
||
96 | } |
||
97 | |||
98 | 78 | return $location; |
|
99 | } |
||
100 | |||
101 | 11 | private function setTitleOrLink( Location $location, $titleOrLink ) { |
|
108 | |||
109 | 11 | private function isLink( $value ) { |
|
112 | |||
113 | 2 | private function setLink( Location $location, $link ) { |
|
117 | |||
118 | 2 | private function getExpandedLink( $link ) { |
|
131 | |||
132 | 2 | private function isAddress( string $coordsOrAddress ): bool { |
|
144 | |||
145 | } |
||
146 |
This method has been deprecated.