Index: trunk/extensions/Maps/Maps_CoordinateParser.php |
— | — | @@ -28,6 +28,7 @@ |
29 | 29 | class MapsCoordinateParser { |
30 | 30 | |
31 | 31 | protected static $mSeperators = array( ',', ';' ); |
| 32 | + protected static $mSeperatorsRegex = false; |
32 | 33 | |
33 | 34 | protected static $mI18nDirections = false; // Cache for localised direction labels |
34 | 35 | protected static $mDirections; // Cache for English direction labels |
— | — | @@ -38,11 +39,10 @@ |
39 | 40 | * will be stored in an array with keys 'lat' and 'lon'. |
40 | 41 | * |
41 | 42 | * @param string $coordinates The coordinates to be parsed. |
42 | | - * @param string $separator Delimiter that seperates the latitude and longitude |
43 | 43 | * |
44 | 44 | * @return array or false |
45 | 45 | */ |
46 | | - public static function parseCoordinates( $coordinates, $separator = ',' ) { |
| 46 | + public static function parseCoordinates( $coordinates ) { |
47 | 47 | // Normalize the coordinates string. |
48 | 48 | $coordinates = self::normalizeCoordinates( $coordinates ); |
49 | 49 | |
— | — | @@ -58,10 +58,14 @@ |
59 | 59 | } |
60 | 60 | |
61 | 61 | // Split the coodrinates string into a lat and lon part. |
62 | | - $coordinates = explode( $separator, $coordinates ); |
| 62 | + foreach( self::$mSeperators as $seperator ){ |
| 63 | + $split = explode( $seperator, $coordinates ); |
| 64 | + if ( count( $split ) == 2 ) break; |
| 65 | + } |
| 66 | + |
63 | 67 | $coordinates = array( |
64 | | - 'lat' => trim ( $coordinates[0] ), |
65 | | - 'lon' => trim ( $coordinates[1] ), |
| 68 | + 'lat' => trim ( $split[0] ), |
| 69 | + 'lon' => trim ( $split[1] ), |
66 | 70 | ); |
67 | 71 | |
68 | 72 | // Ensure the coordinates are in non-directional notation. |
— | — | @@ -221,8 +225,9 @@ |
222 | 226 | * @return boolean |
223 | 227 | */ |
224 | 228 | public static function areFloatCoordinates( $coordinates ) { |
225 | | - return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?,(\s)?(-)?\d{1,3}(\.\d{1,20})?$/', $coordinates ) // Non-directional |
226 | | - || preg_match( '/^\d{1,3}(\.\d{1,20})?(\s)?(N|S),(\s)?\d{1,3}(\.\d{1,20})?(\s)?(E|W)$/', $coordinates ); // Directional |
| 229 | + $sep = self::getSeperatorsRegex(); |
| 230 | + return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep . '(\s)?(-)?\d{1,3}(\.\d{1,20})?$/', $coordinates ) // Non-directional |
| 231 | + || preg_match( '/^\d{1,3}(\.\d{1,20})?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}(\.\d{1,20})?(\s)?(E|W)$/', $coordinates ); // Directional |
227 | 232 | } |
228 | 233 | |
229 | 234 | /** |
— | — | @@ -233,8 +238,11 @@ |
234 | 239 | * @return boolean |
235 | 240 | */ |
236 | 241 | public static function areDMSCoordinates( $coordinates ) { |
237 | | - return preg_match( '/^(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?),(\s)?(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)$/', $coordinates ) // Non-directional |
238 | | - || preg_match( '/^(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(N|S),(\s)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(E|W)$/', $coordinates ); // Directional |
| 242 | + $sep = self::getSeperatorsRegex(); |
| 243 | + return preg_match( '/^(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)' |
| 244 | + . $sep . '(\s)?(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)$/', $coordinates ) // Non-directional |
| 245 | + || preg_match( '/^(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(N|S)' |
| 246 | + . $sep . '(\s)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(E|W)$/', $coordinates ); // Directional |
239 | 247 | } |
240 | 248 | |
241 | 249 | /** |
— | — | @@ -245,8 +253,9 @@ |
246 | 254 | * @return boolean |
247 | 255 | */ |
248 | 256 | public static function areDDCoordinates( $coordinates ) { |
249 | | - return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/', $coordinates ) // Non-directional |
250 | | - || preg_match( '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S),(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)?(E|W)?$/', $coordinates ); // Directional |
| 257 | + $sep = self::getSeperatorsRegex(); |
| 258 | + return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°' . $sep . '(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/', $coordinates ) // Non-directional |
| 259 | + || preg_match( '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S)' . $sep . '(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)?(E|W)?$/', $coordinates ); // Directional |
251 | 260 | } |
252 | 261 | |
253 | 262 | /** |
— | — | @@ -257,8 +266,9 @@ |
258 | 267 | * @return boolean |
259 | 268 | */ |
260 | 269 | public static function areDMCoordinates( $coordinates ) { |
261 | | - return preg_match( '/(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?,(\s)?(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?$/', $coordinates ) // Non-directional |
262 | | - || preg_match( '/\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(N|S),(\s)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(E|W)?$/', $coordinates ); // Directional |
| 270 | + $sep = self::getSeperatorsRegex(); |
| 271 | + return preg_match( '/(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?' . $sep . '(\s)?(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?$/', $coordinates ) // Non-directional |
| 272 | + || preg_match( '/\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(E|W)?$/', $coordinates ); // Directional |
263 | 273 | } |
264 | 274 | |
265 | 275 | /** |
— | — | @@ -425,4 +435,9 @@ |
426 | 436 | return $coordinate; |
427 | 437 | } |
428 | 438 | |
| 439 | + private static function getSeperatorsRegex() { |
| 440 | + if ( !self::$mSeperatorsRegex ) self::$mSeperatorsRegex = '(' . implode( '|', self::$mSeperators ) . ')'; |
| 441 | + return self::$mSeperatorsRegex; |
| 442 | + } |
| 443 | + |
429 | 444 | } |
\ No newline at end of file |