Completed
Push — mv ( 2424da...13ee4c )
by Jeroen De
06:33 queued 01:32
created

LocationParser::getExpandedLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 7
cp 0.4286
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 4.679
1
<?php
2
3
namespace Maps;
4
5
use DataValues\Geo\Parsers\LatLongParser;
6
use Jeroen\SimpleGeocoder\Geocoder;
7
use Maps\Elements\Location;
8
use MapsMapper;
9
use Title;
10
use ValueParsers\ParseException;
11
use ValueParsers\StringValueParser;
12
use ValueParsers\ValueParser;
13
14
/**
15
 * ValueParser that parses the string representation of a location.
16
 *
17
 * @since 3.0
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
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 {
38 29
		$instance = new self( false );
39 29
		$instance->geocoder = $geocoder;
40 29
		$instance->useAddressAsTitle = $useAddressAsTitle;
41 29
		return $instance;
42
	}
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 ) ) );
0 ignored issues
show
Deprecated Code introduced by
The method MapsMapper::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
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 ) ) ) ;
0 ignored issues
show
Deprecated Code introduced by
The method MapsMapper::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
96
		}
97
98 78
		return $location;
99
	}
100
101 11
	private function setTitleOrLink( Location $location, $titleOrLink ) {
102 11
		if ( $this->isLink( $titleOrLink ) ) {
103 2
			$this->setLink( $location, $titleOrLink );
104
		} else {
105 9
			$location->setTitle( $titleOrLink );
106
		}
107 11
	}
108
109 11
	private function isLink( $value ) {
110 11
		return strpos( $value, 'link:' ) === 0;
111
	}
112
113 2
	private function setLink( Location $location, $link ) {
114 2
		$link = substr( $link, 5 );
115 2
		$location->setLink( $this->getExpandedLink( $link ) );
116 2
	}
117
118 2
	private function getExpandedLink( $link ) {
119 2
		if ( filter_var( $link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED ) ) {
120 2
			return $link;
121
		}
122
123
		$title = Title::newFromText( $link );
124
125
		if ( $title === null ) {
126
			return '';
127
		}
128
129
		return $title->getFullURL();
130
	}
131
132 2
	private function isAddress( string $coordsOrAddress ): bool {
133 2
		$coordinateParser = new LatLongParser();
134
135
		try {
136 2
			$coordinateParser->parse( $coordsOrAddress );
137
		}
138 1
		catch ( ParseException $parseException ) {
139 1
			return true;
140
		}
141
142 1
		return false;
143
	}
144
145
}
146