Completed
Push — master ( 81538e...c2bf57 )
by Jeroen De
10:06
created

Location::getJSONObject()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.5222
c 0
b 0
f 0
cc 9
nc 256
nop 6
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\LegacyModel;
6
7
use DataValues\Geo\Values\LatLongValue;
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
	 * @var LatLongValue
24
	 */
25
	private $coordinates;
26
27
	/**
28
	 * @var string
29
	 */
30
	private $address;
31
32
	/**
33
	 * @var string
34
	 */
35
	private $icon = '';
36
37
	/**
38
	 * @var string
39
	 */
40
	private $group = '';
41
42
	/**
43
	 * @var string
44
	 */
45
	private $inlineLabel = '';
46
47
	/**
48
	 * @var string
49
	 */
50
	private $visitedIcon = '';
51
52
	public function __construct( LatLongValue $coordinates, string $title = '', string $text = '' ) {
53
		$this->coordinates = $coordinates;
54
		$this->setTitle( $title );
55
		$this->setText( $text );
56
	}
57
58
	public static function newFromLatLon( float $lat, float $lon ): self {
59
		return new self( new LatLongValue( $lat, $lon ) );
60
	}
61
62
	public function getCoordinates(): LatLongValue {
63
		return $this->coordinates;
64
	}
65
66
	public function getJSONObject( string $defText = '', string $defTitle = '', string $defIconUrl = '',
67
		string $defGroup = '', string $defInlineLabel = '', string $defVisitedIcon = '' ): array {
68
69
		$parentArray = parent::getJSONObject( $defText, $defTitle );
0 ignored issues
show
Deprecated Code introduced by
The method Maps\LegacyModel\BaseElement::getJSONObject() has been deprecated.

This method has been deprecated.

Loading history...
70
71
		$array = [
72
			'lat' => $this->coordinates->getLatitude(),
73
			'lon' => $this->coordinates->getLongitude(),
74
			'icon' => $this->hasIcon() ? \Maps\MapsFunctions::getFileUrl( $this->getIcon() ) : $defIconUrl,
0 ignored issues
show
Deprecated Code introduced by
The method Maps\MapsFunctions::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
75
		];
76
		$val = $this->getAddress();
77
		if ( $val !== '' ) {
78
			$array['address'] = $val;
79
		}
80
		$val = $this->hasGroup() ? $this->getGroup() : $defGroup;
81
		if ( !empty( $val ) ) {
82
			$array['group'] = $val;
83
		}
84
		$val = $this->hasInlineLabel() ? $this->getInlineLabel() : $defInlineLabel;
85
		if ( !empty( $val ) ) {
86
			$array['inlineLabel'] = $val;
87
		}
88
		$val = $this->hasVisitedIcon() ? $this->getVisitedIcon() : $defVisitedIcon;
89
		if ( !empty( $val ) ) {
90
			$array['visitedicon'] = $val;
91
		}
92
93
		return array_merge( $parentArray, $array );
94
	}
95
96
	public function hasIcon(): bool {
97
		return $this->icon !== '';
98
	}
99
100
	public function getIcon(): string {
101
		return $this->icon;
102
	}
103
104
	public function setIcon( string $icon ) {
105
		$this->icon = $icon;
106
	}
107
108
	/**
109
	 * Returns the address corresponding to this location.
110
	 * If there is none, and empty sting is returned.
111
	 */
112
	public function getAddress(): string {
113
		if ( is_null( $this->address ) ) {
114
			$this->address = '';
115
		}
116
117
		return $this->address;
118
	}
119
120
	/**
121
	 * Returns whether Location is assigned to a group.
122
	 */
123
	public function hasGroup(): bool {
124
		return $this->group !== '';
125
	}
126
127
	public function getGroup(): string {
128
		return $this->group;
129
	}
130
131
	public function setGroup( string $group ) {
132
		$this->group = trim( $group );
133
	}
134
135
	public function hasInlineLabel(): bool {
136
		return $this->inlineLabel !== '';
137
	}
138
139
	public function getInlineLabel(): string {
140
		return $this->inlineLabel;
141
	}
142
143
	public function setInlineLabel( string $label ) {
144
		$this->inlineLabel = $label;
145
	}
146
147
	public function hasVisitedIcon(): bool {
148
		return $this->visitedIcon !== '';
149
	}
150
151
	public function getVisitedIcon(): string {
152
		return $this->visitedIcon;
153
	}
154
155
	public function setVisitedIcon( string $visitedIcon ) {
156
		$this->visitedIcon = $visitedIcon;
157
	}
158
159
}
160