Location::getJSONObject()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.3752

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 15
cts 18
cp 0.8333
rs 6.5222
c 0
b 0
f 0
cc 9
nc 256
nop 6
crap 9.3752
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 77
	public function __construct( LatLongValue $coordinates, string $title = '', string $text = '' ) {
53 77
		$this->coordinates = $coordinates;
54 77
		$this->setTitle( $title );
55 77
		$this->setText( $text );
56 77
	}
57
58 3
	public static function newFromLatLon( float $lat, float $lon ): self {
59 3
		return new self( new LatLongValue( $lat, $lon ) );
60
	}
61
62 8
	public function getCoordinates(): LatLongValue {
63 8
		return $this->coordinates;
64
	}
65
66 18
	public function getJSONObject( string $defText = '', string $defTitle = '', string $defIconUrl = '',
67
		string $defGroup = '', string $defInlineLabel = '', string $defVisitedIcon = '' ): array {
68
69 18
		$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 18
			'lat' => $this->coordinates->getLatitude(),
73 18
			'lon' => $this->coordinates->getLongitude(),
74 18
			'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 18
		$val = $this->getAddress();
77 18
		if ( $val !== '' ) {
78
			$array['address'] = $val;
79
		}
80 18
		$val = $this->hasGroup() ? $this->getGroup() : $defGroup;
81 18
		if ( !empty( $val ) ) {
82
			$array['group'] = $val;
83
		}
84 18
		$val = $this->hasInlineLabel() ? $this->getInlineLabel() : $defInlineLabel;
85 18
		if ( !empty( $val ) ) {
86 1
			$array['inlineLabel'] = $val;
87
		}
88 18
		$val = $this->hasVisitedIcon() ? $this->getVisitedIcon() : $defVisitedIcon;
89 18
		if ( !empty( $val ) ) {
90
			$array['visitedicon'] = $val;
91
		}
92
93 18
		return array_merge( $parentArray, $array );
94
	}
95
96 18
	public function hasIcon(): bool {
97 18
		return $this->icon !== '';
98
	}
99
100
	public function getIcon(): string {
101
		return $this->icon;
102
	}
103
104 4
	public function setIcon( string $icon ) {
105 4
		$this->icon = $icon;
106 4
	}
107
108
	/**
109
	 * Returns the address corresponding to this location.
110
	 * If there is none, and empty sting is returned.
111
	 */
112 18
	public function getAddress(): string {
113 18
		if ( is_null( $this->address ) ) {
114 18
			$this->address = '';
115
		}
116
117 18
		return $this->address;
118
	}
119
120
	/**
121
	 * Returns whether Location is assigned to a group.
122
	 */
123 18
	public function hasGroup(): bool {
124 18
		return $this->group !== '';
125
	}
126
127
	public function getGroup(): string {
128
		return $this->group;
129
	}
130
131 1
	public function setGroup( string $group ) {
132 1
		$this->group = trim( $group );
133 1
	}
134
135 18
	public function hasInlineLabel(): bool {
136 18
		return $this->inlineLabel !== '';
137
	}
138
139 1
	public function getInlineLabel(): string {
140 1
		return $this->inlineLabel;
141
	}
142
143 1
	public function setInlineLabel( string $label ) {
144 1
		$this->inlineLabel = $label;
145 1
	}
146
147 18
	public function hasVisitedIcon(): bool {
148 18
		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