Completed
Push — address-as-title ( feca68...38b79f )
by Peter
04:24
created

Location::newTitledFromLatLon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace Maps\Elements;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use Maps\Geocoders;
7
use MWException;
8
9
/**
10
 * Class describing a single location (geographical point).
11
 *
12
 * TODO: rethink the design of this class after deciding on what actual role it has
13
 *
14
 * @since 3.0
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Daniel Werner
19
 */
20
class Location extends BaseElement {
21
22
	/**
23
	 * @since 3.0
24
	 *
25
	 * @var LatLongValue
26
	 */
27
	protected $coordinates;
28
29
	/**
30
	 * @since 0.7.1
31
	 *
32
	 * @var string
33
	 */
34
	protected $address;
35
36
	/**
37
	 * @since 0.7.2
38
	 *
39
	 * @var string
40
	 */
41
	protected $icon = '';
42
43
	/**
44
	 * @since 2.0
45
	 *
46
	 * @var string
47
	 */
48
	protected $group = '';
49
50
	/**
51
	 * @var string
52
	 * @since 2.0
53
	 */
54
	protected $inlineLabel = '';
55
56
	/**
57
	 * @var string
58
	 * @since 2.0
59
	 */
60
	protected $visitedIcon = '';
61
62
	/**
63
	 * Creates and returns a new instance of a Location from a latitude and longitude.
64
	 *
65
	 * @since 1.0
66
	 *
67
	 * @param float $lat
68
	 * @param float $lon
69
	 *
70
	 * @return Location
71
	 */
72
	public static function newFromLatLon( $lat, $lon ) {
73
		return new self( new LatLongValue( $lat, $lon ) );
74
	}
75
76
77
	/**
78
	 * Creates and returns a new instance of a Location with title from a latitude and longitude.
79
	 *
80
	 * @since 3.7
81
	 *
82
	 * @param float $lat
83
	 * @param float $lon
84
	 *
85
	 * @return Location
86
	 */
87
	public static function newTitledFromLatLon( $lat, $lon ) {
88
		$location = new self( new LatLongValue( $lat, $lon ) );
89
		$location->setTitle( $lat . ',' . $lon );
90
		return $location;
91
	}
92
93
	/**
94
	 * Creates and returns a new instance of a Location from an address.
95
	 *
96
	 * @since 1.0
97
	 *
98
	 * @param string $address
99
	 * @deprecated
100
	 *
101
	 * @return Location
102
	 * @throws MWException
103
	 */
104
	public static function newFromAddress( $address ) {
105
		$address = Geocoders::attemptToGeocode( $address );
106
107
		if ( $address === false ) {
108
			throw new MWException( 'Could not geocode address' );
109
		}
110
111
		return new static( $address );
112
	}
113
114
	/**
115
	 * Constructor.
116
	 *
117
	 * @param LatLongValue $coordinates
118
	 *
119
	 * @since 3.0
120
	 */
121
	public function __construct( LatLongValue $coordinates ) {
122
		parent::__construct();
123
		$this->coordinates = $coordinates;
124
	}
125
126
	/**
127
	 * Sets the location to a set of coordinates. You can provide a string
128
	 * of raw coordinates, an array with lat and lon values and false.
129
	 *
130
	 * @since 3.0
131
	 *
132
	 * @param LatLongValue $coordinates
133
	 */
134
	public function setCoordinates( LatLongValue $coordinates ) {
135
		$this->coordinates = $coordinates;
136
	}
137
138
	/**
139
	 * Sets the location to an address.
140
	 *
141
	 * @since 0.7.1
142
	 *
143
	 * @param string $address
144
	 * @param boolean $asActualLocation When set to false, the location is not changed, only the address string is.
145
	 *
146
	 * @return boolean Success indicator
147
	 */
148
	public function setAddress( $address, $asActualLocation = true ) {
149
		if ( $asActualLocation ) {
150
			$coordinates = \Maps\Geocoders::geocode( $address );
151
152
			if ( $coordinates === false ) {
153
				return false;
154
			}
155
156
			$this->setCoordinates( $coordinates );
157
		}
158
159
		$this->address = $address;
160
161
		return true;
162
	}
163
164
	/**
165
	 * Returns the locations coordinates.
166
	 *
167
	 * @since 3.0
168
	 *
169
	 * @return LatLongValue
170
	 */
171
	public function getCoordinates() {
172
		return $this->coordinates;
173
	}
174
175
	/**
176
	 * Returns the address corresponding to this location.
177
	 * If there is none, and empty sting is returned.
178
	 *
179
	 * @since 0.7.1
180
	 *
181
	 * @return string
182
	 */
183
	public function getAddress() {
184
		if ( is_null( $this->address ) ) {
185
			$this->address = '';
186
		}
187
188
		return $this->address;
189
	}
190
191
192
	/**
193
	 * Returns if there is any icon.
194
	 *
195
	 * @since 1.0
196
	 *
197
	 * @return boolean
198
	 */
199
	public function hasIcon() {
200
		return $this->icon !== '';
201
	}
202
203
	/**
204
	 * Sets the icon
205
	 *
206
	 * @since 0.7.2
207
	 *
208
	 * @param string $icon
209
	 */
210
	public function setIcon( $icon ) {
211
		$this->icon = trim( $icon );
212
	}
213
214
	/**
215
	 * Sets the group
216
	 *
217
	 * @since 2.0
218
	 *
219
	 * @param string $group
220
	 */
221
	public function setGroup( $group ) {
222
		$this->group = trim( $group );
223
	}
224
225
	/**
226
	 * Returns the icon.
227
	 *
228
	 * @since 0.7.2
229
	 *
230
	 * @return string
231
	 */
232
	public function getIcon() {
233
		return $this->icon;
234
	}
235
236
	/**
237
	 * Returns the group.
238
	 *
239
	 * @since 2.0
240
	 *
241
	 * @return string
242
	 */
243
	public function getGroup() {
244
		return $this->group;
245
	}
246
247
	/**
248
	 * Returns whether Location is assigned to a group.
249
	 *
250
	 * @since 2.0
251
	 *
252
	 * @return string
253
	 */
254
	public function hasGroup() {
255
		return $this->group !== '';
256
	}
257
258
	/**
259
	 * @return string
260
	 * @since 2.0
261
	 */
262
	public function getInlineLabel(){
263
		return $this->inlineLabel;
264
	}
265
266
	/**
267
	 * @param $label
268
	 * @since 2.0
269
	 */
270
	public function setInlineLabel($label){
271
		$this->inlineLabel = $label;
272
	}
273
274
	/**
275
	 * @return bool
276
	 * @since 2.0
277
	 */
278
	public function hasInlineLabel(){
279
		return $this->inlineLabel !== '';
280
	}
281
282
	/**
283
	 * @return string
284
	 * @since 2.0
285
	 */
286
	public function getVisitedIcon() {
287
		return $this->visitedIcon;
288
	}
289
290
	/**
291
	 * @param $visitedIcon
292
	 * @since 2.0
293
	 */
294
	public function setVisitedIcon( $visitedIcon ) {
295
		$this->visitedIcon = trim($visitedIcon);
296
	}
297
298
	/**
299
	 * @return bool
300
	 * @since 2.0
301
	 */
302
	public function hasVisitedIcon(){
303
		return $this->visitedIcon !== '';
304
	}
305
306
	/**
307
	 * Returns an object that can directly be converted to JS using json_encode or similar.
308
	 *
309
	 * FIXME: complexity
310
	 *
311
	 * @since 1.0
312
	 *
313
	 * @param string $defText
314
	 * @param string $defTitle
315
	 * @param string $defIconUrl
316
	 * @param string $defGroup
317
	 * @param string $defInlineLabel
318
	 * @param string $defVisitedIcon
319
	 *
320
	 * @return array
321
	 */
322
	public function getJSONObject( $defText = '', $defTitle = '', $defIconUrl = '', $defGroup = '', $defInlineLabel = '', $defVisitedIcon = '' ) {
323
		$parentArray = parent::getJSONObject( $defText , $defTitle );
0 ignored issues
show
Deprecated Code introduced by
The method Maps\Elements\BaseElement::getJSONObject() has been deprecated.

This method has been deprecated.

Loading history...
324
325
		$array = [
326
			'lat' => $this->coordinates->getLatitude(),
327
			'lon' => $this->coordinates->getLongitude(),
328
			'alt' => 0,
329
			'address' => $this->getAddress(),
330
			'icon' => $this->hasIcon() ? \MapsMapper::getFileUrl( $this->getIcon() ) : $defIconUrl,
0 ignored issues
show
Deprecated Code introduced by
The method MapsMapper::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
331
			'group' => $this->hasGroup() ?  $this->getGroup() : $defGroup,
332
			'inlineLabel' => $this->hasInlineLabel() ? $this->getInlineLabel() : $defInlineLabel,
333
			'visitedicon' => $this->hasVisitedIcon() ? $this->getVisitedIcon() : $defVisitedIcon,
334
		];
335
336
		return array_merge( $parentArray , $array );
337
	}
338
339
}
340