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

Location::getJSONObject()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
nc 256
nop 6
dl 0
loc 29
rs 3
c 1
b 0
f 0
ccs 0
cts 12
cp 0
crap 90
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
	 * Creates and returns a new instance of a Location from an address.
78
	 *
79
	 * @since 1.0
80
	 *
81
	 * @param string $address
82
	 * @deprecated
83
	 *
84
	 * @return Location
85
	 * @throws MWException
86
	 */
87
	public static function newFromAddress( $address ) {
88
		$address = Geocoders::attemptToGeocode( $address );
89
90
		if ( $address === false ) {
91
			throw new MWException( 'Could not geocode address' );
92
		}
93
94
		return new static( $address );
95
	}
96
97
	/**
98
	 * Constructor.
99
	 *
100
	 * @param LatLongValue $coordinates
101
	 *
102
	 * @since 3.0
103
	 */
104
	public function __construct( LatLongValue $coordinates ) {
105
		parent::__construct();
106
		$this->coordinates = $coordinates;
107
	}
108
109
	/**
110
	 * Sets the location to a set of coordinates. You can provide a string
111
	 * of raw coordinates, an array with lat and lon values and false.
112
	 *
113
	 * @since 3.0
114
	 *
115
	 * @param LatLongValue $coordinates
116
	 */
117
	public function setCoordinates( LatLongValue $coordinates ) {
118
		$this->coordinates = $coordinates;
119
	}
120
121
	/**
122
	 * Sets the location to an address.
123
	 *
124
	 * @since 0.7.1
125
	 *
126
	 * @param string $address
127
	 * @param boolean $asActualLocation When set to false, the location is not changed, only the address string is.
128
	 *
129
	 * @return boolean Success indicator
130
	 */
131
	public function setAddress( $address, $asActualLocation = true ) {
132
		if ( $asActualLocation ) {
133
			$coordinates = \Maps\Geocoders::geocode( $address );
134
135
			if ( $coordinates === false ) {
136
				return false;
137
			}
138
139
			$this->setCoordinates( $coordinates );
140
		}
141
142
		$this->address = $address;
143
144
		return true;
145
	}
146
147
	/**
148
	 * Returns the locations coordinates.
149
	 *
150
	 * @since 3.0
151
	 *
152
	 * @return LatLongValue
153
	 */
154
	public function getCoordinates() {
155
		return $this->coordinates;
156
	}
157
158
	/**
159
	 * Returns the address corresponding to this location.
160
	 * If there is none, and empty sting is returned.
161
	 *
162
	 * @since 0.7.1
163
	 *
164
	 * @return string
165
	 */
166
	public function getAddress() {
167
		if ( is_null( $this->address ) ) {
168
			$this->address = '';
169
		}
170
171
		return $this->address;
172
	}
173
174
175
	/**
176
	 * Returns if there is any icon.
177
	 *
178
	 * @since 1.0
179
	 *
180
	 * @return boolean
181
	 */
182
	public function hasIcon() {
183
		return $this->icon !== '';
184
	}
185
186
	/**
187
	 * Sets the icon
188
	 *
189
	 * @since 0.7.2
190
	 *
191
	 * @param string $icon
192
	 */
193
	public function setIcon( $icon ) {
194
		$this->icon = trim( $icon );
195
	}
196
197
	/**
198
	 * Sets the group
199
	 *
200
	 * @since 2.0
201
	 *
202
	 * @param string $group
203
	 */
204
	public function setGroup( $group ) {
205
		$this->group = trim( $group );
206
	}
207
208
	/**
209
	 * Returns the icon.
210
	 *
211
	 * @since 0.7.2
212
	 *
213
	 * @return string
214
	 */
215
	public function getIcon() {
216
		return $this->icon;
217
	}
218
219
	/**
220
	 * Returns the group.
221
	 *
222
	 * @since 2.0
223
	 *
224
	 * @return string
225
	 */
226
	public function getGroup() {
227
		return $this->group;
228
	}
229
230
	/**
231
	 * Returns whether Location is assigned to a group.
232
	 *
233
	 * @since 2.0
234
	 *
235
	 * @return string
236
	 */
237
	public function hasGroup() {
238
		return $this->group !== '';
239
	}
240
241
	/**
242
	 * @return string
243
	 * @since 2.0
244
	 */
245
	public function getInlineLabel(){
246
		return $this->inlineLabel;
247
	}
248
249
	/**
250
	 * @param $label
251
	 * @since 2.0
252
	 */
253
	public function setInlineLabel($label){
254
		$this->inlineLabel = $label;
255
	}
256
257
	/**
258
	 * @return bool
259
	 * @since 2.0
260
	 */
261
	public function hasInlineLabel(){
262
		return $this->inlineLabel !== '';
263
	}
264
265
	/**
266
	 * @return string
267
	 * @since 2.0
268
	 */
269
	public function getVisitedIcon() {
270
		return $this->visitedIcon;
271
	}
272
273
	/**
274
	 * @param $visitedIcon
275
	 * @since 2.0
276
	 */
277
	public function setVisitedIcon( $visitedIcon ) {
278
		$this->visitedIcon = trim($visitedIcon);
279
	}
280
281
	/**
282
	 * @return bool
283
	 * @since 2.0
284
	 */
285
	public function hasVisitedIcon(){
286
		return $this->visitedIcon !== '';
287
	}
288
289
	/**
290
	 * Returns an object that can directly be converted to JS using json_encode or similar.
291
	 *
292
	 * FIXME: complexity
293
	 *
294
	 * @since 1.0
295
	 *
296
	 * @param string $defText
297
	 * @param string $defTitle
298
	 * @param string $defIconUrl
299
	 * @param string $defGroup
300
	 * @param string $defInlineLabel
301
	 * @param string $defVisitedIcon
302
	 *
303
	 * @return array
304
	 */
305
	public function getJSONObject( $defText = '', $defTitle = '', $defIconUrl = '', $defGroup = '', $defInlineLabel = '', $defVisitedIcon = '' ) {
306
		$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...
307
308
		$array = [
309
			'lat' => $this->coordinates->getLatitude(),
310
			'lon' => $this->coordinates->getLongitude(),
311
			// not used for now:
312
			//'alt' => 0,
313
			'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...
314
		];
315
		$val = $this->getAddress();
316
		if( !empty( $val ) ) {
317
			$array['address'] = $val;
318
		}
319
		$val = $this->hasGroup() ? $this->getGroup() : $defGroup;
320
		if( !empty( $val ) ) {
321
			$array['group'] = $val;
322
		}
323
		$val = $this->hasInlineLabel() ? $this->getInlineLabel() : $defInlineLabel;
324
		if( !empty( $val ) ) {
325
			$array['inlineLabel'] = $val;
326
		}
327
		$val = $this->hasVisitedIcon() ? $this->getVisitedIcon() : $defVisitedIcon;
328
		if( !empty( $val ) ) {
329
			$array['visitedicon'] = $val;
330
		}
331
332
		return array_merge( $parentArray , $array );
333
	}
334
335
}
336