Completed
Push — newparam ( 72433c...c5fad3 )
by Jeroen De
01:21
created

Location::newFromLatLon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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