Completed
Push — nophpunit ( 9a5068 )
by Jeroen De
05:11
created

Location::getJSONObject()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

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