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