Completed
Push — killgeocoders ( 08fbcb )
by Jeroen De
06:57
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 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Maps\Elements;
4
5
use DataValues\Geo\Values\LatLongValue;
6
7
/**
8
 * Class describing a single location (geographical point).
9
 *
10
 * TODO: rethink the design of this class after deciding on what actual role it has
11
 *
12
 * @since 3.0
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 * @author Daniel Werner
17
 */
18
class Location extends BaseElement {
19
20
	/**
21
	 * @var LatLongValue
22
	 */
23
	private $coordinates;
24
25
	/**
26
	 * @var string
27
	 */
28
	private $address;
29
30
	/**
31
	 * @var string
32
	 */
33
	private $icon = '';
34
35
	/**
36
	 * @var string
37
	 */
38
	private $group = '';
39
40
	/**
41
	 * @var string
42
	 */
43
	private $inlineLabel = '';
44
45
	/**
46
	 * @var string
47
	 */
48
	private $visitedIcon = '';
49
50
	/**
51
	 * Creates and returns a new instance of a Location from a latitude and longitude.
52
	 *
53
	 * @since 1.0
54
	 *
55
	 * @param float $lat
56
	 * @param float $lon
57
	 *
58
	 * @return Location
59
	 */
60
	public static function newFromLatLon( $lat, $lon ) {
61
		return new self( new LatLongValue( $lat, $lon ) );
62
	}
63
64
	public function __construct( LatLongValue $coordinates ) {
65
		parent::__construct();
66
		$this->coordinates = $coordinates;
67
	}
68
69
	/**
70
	 * Returns the locations coordinates.
71
	 *
72
	 * @since 3.0
73
	 *
74
	 * @return LatLongValue
75
	 */
76
	public function getCoordinates() {
77
		return $this->coordinates;
78
	}
79
80
	/**
81
	 * Returns the address corresponding to this location.
82
	 * If there is none, and empty sting is returned.
83
	 *
84
	 * @since 0.7.1
85
	 *
86
	 * @return string
87
	 */
88
	public function getAddress() {
89
		if ( is_null( $this->address ) ) {
90
			$this->address = '';
91
		}
92
93
		return $this->address;
94
	}
95
96
97
	/**
98
	 * Returns if there is any icon.
99
	 *
100
	 * @since 1.0
101
	 *
102
	 * @return boolean
103
	 */
104
	public function hasIcon() {
105
		return $this->icon !== '';
106
	}
107
108
	/**
109
	 * Sets the icon
110
	 *
111
	 * @since 0.7.2
112
	 *
113
	 * @param string $icon
114
	 */
115
	public function setIcon( $icon ) {
116
		$this->icon = trim( $icon );
117
	}
118
119
	/**
120
	 * Sets the group
121
	 *
122
	 * @since 2.0
123
	 *
124
	 * @param string $group
125
	 */
126
	public function setGroup( $group ) {
127
		$this->group = trim( $group );
128
	}
129
130
	/**
131
	 * Returns the icon.
132
	 *
133
	 * @since 0.7.2
134
	 *
135
	 * @return string
136
	 */
137
	public function getIcon() {
138
		return $this->icon;
139
	}
140
141
	/**
142
	 * Returns the group.
143
	 *
144
	 * @since 2.0
145
	 *
146
	 * @return string
147
	 */
148
	public function getGroup() {
149
		return $this->group;
150
	}
151
152
	/**
153
	 * Returns whether Location is assigned to a group.
154
	 *
155
	 * @since 2.0
156
	 *
157
	 * @return string
158
	 */
159
	public function hasGroup() {
160
		return $this->group !== '';
161
	}
162
163
	/**
164
	 * @return string
165
	 * @since 2.0
166
	 */
167
	public function getInlineLabel(){
168
		return $this->inlineLabel;
169
	}
170
171
	/**
172
	 * @param $label
173
	 * @since 2.0
174
	 */
175
	public function setInlineLabel($label){
176
		$this->inlineLabel = $label;
177
	}
178
179
	/**
180
	 * @return bool
181
	 * @since 2.0
182
	 */
183
	public function hasInlineLabel(){
184
		return $this->inlineLabel !== '';
185
	}
186
187
	/**
188
	 * @return string
189
	 * @since 2.0
190
	 */
191
	public function getVisitedIcon() {
192
		return $this->visitedIcon;
193
	}
194
195
	/**
196
	 * @param $visitedIcon
197
	 * @since 2.0
198
	 */
199
	public function setVisitedIcon( $visitedIcon ) {
200
		$this->visitedIcon = trim($visitedIcon);
201
	}
202
203
	/**
204
	 * @return bool
205
	 * @since 2.0
206
	 */
207
	public function hasVisitedIcon(){
208
		return $this->visitedIcon !== '';
209
	}
210
211
	/**
212
	 * Returns an object that can directly be converted to JS using json_encode or similar.
213
	 *
214
	 * FIXME: complexity
215
	 *
216
	 * @since 1.0
217
	 *
218
	 * @param string $defText
219
	 * @param string $defTitle
220
	 * @param string $defIconUrl
221
	 * @param string $defGroup
222
	 * @param string $defInlineLabel
223
	 * @param string $defVisitedIcon
224
	 *
225
	 * @return array
226
	 */
227
	public function getJSONObject( $defText = '', $defTitle = '', $defIconUrl = '', $defGroup = '', $defInlineLabel = '', $defVisitedIcon = '' ) {
228
		$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...
229
230
		$array = [
231
			'lat' => $this->coordinates->getLatitude(),
232
			'lon' => $this->coordinates->getLongitude(),
233
			'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...
234
		];
235
		$val = $this->getAddress();
236
		if( $val !== '' ) {
237
			$array['address'] = $val;
238
		}
239
		$val = $this->hasGroup() ? $this->getGroup() : $defGroup;
240
		if( !empty( $val ) ) {
241
			$array['group'] = $val;
242
		}
243
		$val = $this->hasInlineLabel() ? $this->getInlineLabel() : $defInlineLabel;
244
		if( !empty( $val ) ) {
245
			$array['inlineLabel'] = $val;
246
		}
247
		$val = $this->hasVisitedIcon() ? $this->getVisitedIcon() : $defVisitedIcon;
248
		if( !empty( $val ) ) {
249
			$array['visitedicon'] = $val;
250
		}
251
252
		return array_merge( $parentArray , $array );
253
	}
254
255
}
256