Completed
Pull Request — master (#54)
by Peter
07:18
created

MapsLocation::getInlineLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class describing a single location (geographical point).
5
 *
6
 * @since 0.7.1
7
 *
8
 * @file Maps_Location.php
9
 * @ingroup Maps
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 * @author Daniel Werner
14
 */
15
class MapsLocation extends MapsBaseElement {
16
17
	/**
18
	 * @since 0.7.1
19
	 *
20
	 * @var float
21
	 */
22
	protected $latitude;
23
24
	/**
25
	 * @since 0.7.1
26
	 *
27
	 * @var float
28
	 */
29
	protected $longitude;
30
31
	/**
32
	 * @since 0.7.2
33
	 *
34
	 * @var float
35
	 */
36
	protected $altitude = 0;
37
38
	/**
39
	 * @since 0.7.1
40
	 *
41
	 * @var string
42
	 */
43
	protected $address;
44
45
	/**
46
	 * @since 0.7.2
47
	 *
48
	 * @var string
49
	 */
50
	protected $icon = '';
51
52
	/**
53
	 * @since 2.0
54
	 *
55
	 * @var string
56
	 */
57
	protected $group = '';
58
59
	/**
60
	 * @since 0.7.1
61
	 *
62
	 * @var boolean
63
	 */
64
	protected $isValid = false;
65
66
67
	/**
68
	 * @since 0.7.1
69
	 *
70
	 * @var string Element of the Maps_COORDS_ enum
71
	 */
72
	protected $format;
73
74
	/**
75
	 * @since 0.7.1
76
	 *
77
	 * @var boolean
78
	 */
79
	protected $directional;
80
81
	/**
82
	 * @since 0.7.1
83
	 *
84
	 * @var string
85
	 */
86
	protected $separator;
87
88
	/**
89
	 * @var string
90
	 * @since 2.0
91
	 */
92
	protected $inlineLabel = '';
93
94
	/**
95
	 * @var string
96
	 * @since 2.0
97
	 */
98
	protected $visitedIcon = '';
99
100
	/**
101
	 * Creates and returns a new instance of a MapsLocation from a latitude and longitude.
102
	 *
103
	 * @since 1.0
104
	 *
105
	 * @param float $lat
106
	 * @param float $lon
107
	 * @param string $format
108
	 *
109
	 * @return MapsLocation
110
	 */
111
	public static function newFromLatLon( $lat, $lon, $format = Maps_COORDS_FLOAT ) {
112
		return new self( $lat . ',' . $lon, $format );
113
	}
114
115
	/**
116
	 * Creates and returns a new instance of a MapsLocation from an address.
117
	 *
118
	 * @since 1.0
119
	 *
120
	 * @param string $address
121
	 * @param string $format
122
	 *
123
	 * @return MapsLocation
124
	 */
125
	public static function newFromAddress( $address, $format = Maps_COORDS_FLOAT ) {
126
		return new self( $address, $format );
127
	}
128
129
	/**
130
	 * Constructor.
131
	 *
132
	 * @param mixed $coordsOrAddress string or array with lat and lon
133
	 * @param string $format
134
	 * @param boolean $directional
135
	 * @param string $separator
136
	 *
137
	 * @since 0.7.1
138
	 */
139
	public function __construct( $coordsOrAddress = null, $format = Maps_COORDS_FLOAT, $directional = false, $separator = ',' ) {
140
		$this->format = $format;
141
		$this->directional = $directional;
142
		$this->separator = $separator;
143
144
		if ( !is_null( $coordsOrAddress ) ) {
145
			if ( MapsCoordinateParser::areCoordinates( $coordsOrAddress ) ) {
146
				$this->setCoordinates( $coordsOrAddress );
147
			}
148
			else {
149
				$this->setAddress( $coordsOrAddress );
150
			}
151
		}
152
	}
153
154
	/**
155
	 * Sets the location to a set of coordinates. You can provide a string
156
	 * of raw coordinates, an array with lat and lon values and false.
157
	 *
158
	 * @since 0.7.1
159
	 *
160
	 * @param mixed $coordinates
161
	 *
162
	 * @return boolean Success indicator
163
	 */
164
	public function setCoordinates( $coordinates ) {
165
		$coordSet = MapsCoordinateParser::parseCoordinates( $coordinates );
166
		$this->isValid = $coordSet !== false;
167
168
		$this->latitude = $coordSet['lat'];
169
		$this->longitude = $coordSet['lon'];
170
171
		return $this->isValid;
172
	}
173
174
	/**
175
	 * Sets the location to an address.
176
	 *
177
	 * @since 0.7.1
178
	 *
179
	 * @param string $address
180
	 * @param boolean $asActualLocation When set to false, the location is not changed, only the address string is.
181
	 *
182
	 * @return boolean Success indicator
183
	 */
184
	public function setAddress( $address, $asActualLocation = true ) {
185
		if ( $asActualLocation ) {
186
			$this->setCoordinates( MapsGeocoders::geocode( $address ) );
187
		}
188
189
		$this->address = $address;
190
191
		return $this->isValid;
192
	}
193
194
	/**
195
	 * Returns if the location is valid.
196
	 *
197
	 * @since 0.7.1
198
	 *
199
	 * @return boolean
200
	 */
201
	public function isValid() {
202
		return $this->isValid;
203
	}
204
205
	/**
206
	 * Returns the locations latitude.
207
	 *
208
	 * @since 0.7.1
209
	 *
210
	 * @return float
211
	 */
212
	public function getLatitude() {
213
		if ( !$this->isValid() ) {
214
			throw new MWException( 'Attempt to get the latitude of an invalid location' );
215
		}
216
		return $this->latitude;
217
	}
218
219
	/**
220
	 * Returns the locations longitude.
221
	 *
222
	 * @since 0.7.1
223
	 *
224
	 * @return float
225
	 */
226
	public function getLongitude() {
227
		if ( !$this->isValid() ) {
228
			throw new MWException( 'Attempt to get the longitude of an invalid location' );
229
		}
230
		return $this->longitude;
231
	}
232
233
	/**
234
	 * Returns the locations altitude.
235
	 *
236
	 * @since 0.7.3
237
	 *
238
	 * @return float
239
	 */
240
	public function getAltitude() {
241
		if ( !$this->isValid() ) {
242
			throw new MWException( 'Attempt to get the altitude of an invalid location' );
243
		}
244
		return $this->altitude;
245
	}
246
247
	/**
248
	 * Returns the locations coordinates formatted in the specified notation.
249
	 *
250
	 * @since 0.7.1
251
	 *
252
	 * @param string $format Element of the Maps_COORDS_ enum
253
	 * @param boolean $directional
254
	 * @param string $separator
255
	 *
256
	 * @return string
257
	 */
258
	public function getCoordinates( $format = null, $directional = null, $separator = null ) {
259
		if ( !$this->isValid() ) {
260
			throw new MWException( 'Attempt to get the coordinates for an invalid location' );
261
		}
262
		return MapsCoordinateParser::formatCoordinates(
263
			array( 'lat' => $this->latitude, 'lon' => $this->longitude ),
264
			is_null( $format ) ? $this->format : $format,
265
			is_null( $directional ) ? $this->directional : $directional,
266
			is_null( $separator ) ? $this->separator : $separator
267
		);
268
	}
269
270
	/**
271
	 * Returns the address corresponding to this location.
272
	 * If there is none, and empty sting is returned.
273
	 *
274
	 * @since 0.7.1
275
	 *
276
	 * @param boolean $geocodeIfEmpty
277
	 *
278
	 * @return string
279
	 */
280
	public function getAddress( $geocodeIfEmpty = true ) {
281
		if ( !$this->isValid() ) {
282
			throw new MWException( 'Attempt to get the address of an invalid location' );
283
		}
284
285
		if ( is_null( $this->address ) ) {
286
			if ( $geocodeIfEmpty ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
287
				// TODO: attempt to reverse-geocode
288
			}
289
290
			$this->address = '';
291
		}
292
293
		return $this->address;
294
	}
295
296
297
	/**
298
	 * Returns if there is any icon.
299
	 *
300
	 * @since 1.0
301
	 *
302
	 * @return boolean
303
	 */
304
	public function hasIcon() {
305
		return $this->icon !== '';
306
	}
307
308
	/**
309
	 * Sets the icon
310
	 *
311
	 * @since 0.7.2
312
	 *
313
	 * @param string $icon
314
	 */
315
	public function setIcon( $icon ) {
316
		$this->icon = trim( $icon );
317
	}
318
319
	/**
320
	 * Sets the group
321
	 *
322
	 * @since 2.0
323
	 *
324
	 * @param string $group
325
	 */
326
	public function setGroup( $group ) {
327
		$this->group = trim( $group );
328
	}
329
330
	/**
331
	 * Returns the icon.
332
	 *
333
	 * @since 0.7.2
334
	 *
335
	 * @return string
336
	 */
337
	public function getIcon() {
338
		return $this->icon;
339
	}
340
341
	/**
342
	 * Returns the group.
343
	 *
344
	 * @since 2.0
345
	 *
346
	 * @return string
347
	 */
348
	public function getGroup() {
349
		return $this->group;
350
	}
351
352
	/**
353
	 * Returns whether Location is asigned to a group.
354
	 *
355
	 * @since 2.0
356
	 *
357
	 * @return string
358
	 */
359
	public function hasGroup() {
360
		return $this->group !== '';
361
	}
362
363
	/**
364
	 * @return string
365
	 * @since 2.0
366
	 */
367
	public function getInlineLabel(){
368
		return $this->inlineLabel;
369
	}
370
371
	/**
372
	 * @param $label
373
	 * @since 2.0
374
	 */
375
	public function setInlineLabel($label){
376
		$this->inlineLabel = $label;
377
	}
378
379
	/**
380
	 * @return bool
381
	 * @since 2.0
382
	 */
383
	public function hasInlineLabel(){
384
		return $this->inlineLabel !== '';
385
	}
386
387
	/**
388
	 * @return string
389
	 * @since 2.0
390
	 */
391
	public function getVisitedIcon() {
392
		return $this->visitedIcon;
393
	}
394
395
	/**
396
	 * @param $visitedIcon
397
	 * @since 2.0
398
	 */
399
	public function setVisitedIcon( $visitedIcon ) {
400
		$this->visitedIcon = trim($visitedIcon);
401
	}
402
403
	/**
404
	 * @return bool
405
	 * @since 2.0
406
	 */
407
	public function hasVisitedIcon(){
408
		return $this->visitedIcon !== '';
409
	}
410
411
	/**
412
	 * Returns an object that can directly be converted to JS using json_encode or similar.
413
	 *
414
	 * @since 1.0
415
	 *
416
	 * @param string $defText
417
	 * @param string $defTitle
418
	 * @param string $defIconUrl
419
	 * @param string $defGroup
420
	 * @param string $defInlineLabel
421
	 *
422
	 * @return object
423
	 */
424
	public function getJSONObject( $defText = '', $defTitle = '', $defIconUrl = '', $defGroup = '', $defInlineLabel = '', $defVisitedIcon = '' ) {
425
		$parentArray = parent::getJSONObject( $defText , $defTitle );
426
		$array = array(
427
			'lat' => $this->getLatitude(),
428
			'lon' => $this->getLongitude(),
429
			'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...
430
		);
431
                $val=$this->getAltitude();
432
                if(!empty($val)) {
433
			$array['alt']=$val;
434
                }
435
                $val=$this->getAddress( false );
436
                if(!empty($val)) {
437
  			$array['address']=$val;
438
                }
439
                $val= $this->hasGroup() ?  $this->getGroup() : $defGroup;
440
                if(!empty($val)) {
441
  			$array['group']=$val;
442
                }
443
                $val=$this->hasInlineLabel() ? $this->getInlineLabel() : $defInlineLabel;
444
                if(!empty($val)) {
445
  			$array['inlineLabel']=$val;
446
                }
447
                $val=$this->hasVisitedIcon() ? $this->getVisitedIcon() : $defVisitedIcon;
448
                if(!empty($val)) {
449
  			$array['visitedicon']=$val;
450
                }
451
		return array_merge( $parentArray , $array );
452
	}
453
454
}
455