1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SMW\Exception\DataItemException; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @license GNU GPL v2+ |
7
|
|
|
* @author Jeroen De Dauw < [email protected] > |
8
|
|
|
*/ |
9
|
|
|
class SMWDIGeoCoord extends SMWDataItem { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var float |
13
|
|
|
*/ |
14
|
|
|
private $latitude; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var float |
18
|
|
|
*/ |
19
|
|
|
private $longitude; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var float|null |
23
|
|
|
*/ |
24
|
|
|
private $altitude = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Takes a latitude and longitude, and optionally an altitude. These can be provided in 2 forms: |
28
|
|
|
* * An associative array with lat, lon and alt keys |
29
|
|
|
* * Lat, lon and alt arguments |
30
|
|
|
* |
31
|
|
|
* The second way to provide the arguments, as well as the altitude argument, where introduced in SMW 1.7. |
32
|
|
|
*/ |
33
|
|
|
public function __construct() { |
34
|
|
|
$args = func_get_args(); |
35
|
|
|
|
36
|
|
|
$count = count( $args ); |
37
|
|
|
|
38
|
|
|
if ( $count === 1 && is_array( $args[0] ) ) { |
39
|
|
|
if ( array_key_exists( 'lat', $args[0] ) && array_key_exists( 'lon', $args[0] ) ) { |
40
|
|
|
$this->setLatitude( $args[0]['lat'] ); |
41
|
|
|
$this->setLongitude( $args[0]['lon'] ); |
42
|
|
|
|
43
|
|
|
if ( array_key_exists( 'alt', $args[0] ) ) { |
44
|
|
|
$this->altitude = (float)$args[0]['alt']; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
else { |
48
|
|
|
throw new DataItemException( 'Invalid coordinate data passed to the SMWDIGeoCoord constructor' ); |
49
|
4 |
|
} |
50
|
4 |
|
} |
51
|
|
|
elseif ( $count === 2 || $count === 3 ) { |
52
|
4 |
|
$this->setLatitude( $args[0] ); |
53
|
|
|
$this->setLongitude( $args[1] ); |
54
|
4 |
|
|
55
|
4 |
|
if ( $count === 3 ) { |
56
|
4 |
|
$this->altitude = (float)$args[2]; |
57
|
4 |
|
} |
58
|
|
|
} |
59
|
4 |
|
else { |
60
|
4 |
|
throw new DataItemException( 'Invalid coordinate data passed to the SMWDIGeoCoord constructor' ); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
private function setLatitude( $latitude ) { |
65
|
|
|
if ( is_int( $latitude ) ) { |
66
|
|
|
$latitude = (float)$latitude; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( !is_float( $latitude ) ) { |
70
|
|
|
throw new DataItemException( '$latitude should be a float' ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->latitude = $latitude; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function setLongitude( $longitude ) { |
77
|
|
|
if ( is_int( $longitude ) ) { |
78
|
4 |
|
$longitude = (float)$longitude; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( !is_float( $longitude ) ) { |
82
|
|
|
throw new DataItemException( '$longitude should be a float' ); |
83
|
|
|
} |
84
|
2 |
|
|
85
|
2 |
|
$this->longitude = $longitude; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* (non-PHPdoc) |
90
|
|
|
* @see SMWDataItem::getDIType() |
91
|
|
|
*/ |
92
|
|
|
public function getDIType() { |
93
|
|
|
return SMWDataItem::TYPE_GEO; |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
/** |
97
|
4 |
|
* Returns the coordinate set as an array with lat and long (and alt) keys |
98
|
|
|
* pointing to float values. |
99
|
4 |
|
* |
100
|
2 |
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getCoordinateSet() { |
103
|
4 |
|
$coords = array( 'lat' => $this->latitude, 'lon' => $this->longitude ); |
104
|
|
|
|
105
|
|
|
if ( !is_null( $this->altitude ) ) { |
106
|
|
|
$coords['alt'] = $this->altitude; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $coords; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* (non-PHPdoc) |
114
|
|
|
* @see SMWDataItem::getSortKey() |
115
|
|
|
*/ |
116
|
|
|
public function getSortKey() { |
117
|
|
|
return $this->latitude . ',' . $this->longitude . ( $this->altitude !== null ? ','. $this->altitude : '' ); |
118
|
4 |
|
} |
119
|
4 |
|
|
120
|
|
|
/** |
121
|
|
|
* (non-PHPdoc) |
122
|
|
|
* @see SMWDataItem::getSerialization() |
123
|
|
|
*/ |
124
|
|
|
public function getSerialization() { |
125
|
|
|
return implode( ',', $this->getCoordinateSet() ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create a data item from the provided serialization string and type |
130
|
|
|
* ID. |
131
|
|
|
* @note PHP can convert any string to some number, so we do not do |
132
|
|
|
* validation here (because this would require less efficient parsing). |
133
|
|
|
* |
134
|
2 |
|
* @param string $serialization |
135
|
2 |
|
* |
136
|
2 |
|
* @return self |
137
|
|
|
*/ |
138
|
2 |
|
public static function doUnserialize( $serialization ) { |
139
|
|
|
$parts = explode( ',', $serialization ); |
140
|
|
|
$count = count( $parts ); |
141
|
|
|
|
142
|
2 |
|
if ( $count !== 2 && $count !== 3 ) { |
143
|
|
|
throw new DataItemException( 'Unserialization of coordinates failed' ); |
144
|
2 |
|
} |
145
|
1 |
|
|
146
|
|
|
$coords = array( 'lat' => (float)$parts[0], 'lon' => (float)$parts[1] ); |
147
|
|
|
|
148
|
2 |
|
if ( $count === 3 ) { |
149
|
|
|
$coords['alt'] = (float)$parts[2]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return new self( $coords ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return float |
157
|
|
|
*/ |
158
|
|
|
public function getLatitude() { |
159
|
|
|
return $this->latitude; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return float |
164
|
|
|
*/ |
165
|
|
|
public function getLongitude() { |
166
|
|
|
return $this->longitude; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the altitude if set, null otherwise. |
171
|
|
|
* |
172
|
|
|
* @return float|null |
173
|
|
|
*/ |
174
|
|
|
public function getAltitude() { |
175
|
|
|
return $this->altitude; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function equals( SMWDataItem $di ) { |
179
|
|
|
if ( $di->getDIType() !== SMWDataItem::TYPE_GEO ) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $di->getSerialization() === $this->getSerialization(); |
184
|
4 |
|
} |
185
|
|
|
} |
186
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.