Index: trunk/extensions/Maps/Includes/Maps_DistanceParser.php |
— | — | @@ -16,10 +16,6 @@ |
17 | 17 | /** |
18 | 18 | * Static class for distance validation and parsing. Internal representatations are in meters. |
19 | 19 | * |
20 | | - * TODO: |
21 | | - * This class has been quicly put together for 0.6 - with non generic code from Semantic Maps. |
22 | | - * It should be improved and made more generic, but is fine like this for now. |
23 | | - * |
24 | 20 | * @ingroup Maps |
25 | 21 | * |
26 | 22 | * @author Jeroen De Dauw |
— | — | @@ -36,8 +32,6 @@ |
37 | 33 | * @return float The distance in meters. |
38 | 34 | */ |
39 | 35 | public static function parseDistance( $distance ) { |
40 | | - global $egMapsDistanceUnits; |
41 | | - |
42 | 36 | if ( !self::isDistance( $distance ) ) { |
43 | 37 | return false; |
44 | 38 | } |
— | — | @@ -46,13 +40,8 @@ |
47 | 41 | preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance, $matches ); |
48 | 42 | |
49 | 43 | $value = (float)( $matches[1] . $matches[2] ); |
50 | | - $unit = $matches[5]; |
| 44 | + $value *= self::getUnitRatio( $matches[5] ); |
51 | 45 | |
52 | | - // Check for the precence of a supported unit, and if found, factor it in. |
53 | | - if ( $unit != '' && array_key_exists( $unit, $egMapsDistanceUnits ) ) { |
54 | | - $value *= $egMapsDistanceUnits[$unit]; |
55 | | - } |
56 | | - |
57 | 46 | return $value; |
58 | 47 | } |
59 | 48 | |
— | — | @@ -70,14 +59,37 @@ |
71 | 60 | return "$meters $unit"; |
72 | 61 | } |
73 | 62 | |
74 | | - public static function parseAndFormat( $distance, $unit ) { |
75 | | - return self::formatDistance( self::parseDistance( $distance ), $unit ); |
| 63 | + /** |
| 64 | + * Shortcut for converting from one unit to another. |
| 65 | + * |
| 66 | + * @param string $distance |
| 67 | + * @param string $unit |
| 68 | + * @param integer $decimals |
| 69 | + * |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + public static function parseAndFormat( $distance, $unit = null, $decimals = 2 ) { |
| 73 | + return self::formatDistance( self::parseDistance( $distance ), $unit, $decimals ); |
76 | 74 | } |
77 | 75 | |
| 76 | + /** |
| 77 | + * Returns if the provided string is a valid distance. |
| 78 | + * |
| 79 | + * @param string $distance |
| 80 | + * |
| 81 | + * @return boolean |
| 82 | + */ |
78 | 83 | public static function isDistance( $distance ) { |
79 | 84 | return preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance ); |
80 | 85 | } |
81 | 86 | |
| 87 | + /** |
| 88 | + * Returns the unit to meter ratio in a safe way, by first resolving the unit. |
| 89 | + * |
| 90 | + * @param string $unit |
| 91 | + * |
| 92 | + * @return float |
| 93 | + */ |
82 | 94 | public static function getUnitRatio( $unit = null ) { |
83 | 95 | global $egMapsDistanceUnits; |
84 | 96 | return $egMapsDistanceUnits[self::getValidUnit( $unit )]; |
— | — | @@ -87,6 +99,8 @@ |
88 | 100 | * Returns a valid unit. If the provided one is invalid, the default will be used. |
89 | 101 | * |
90 | 102 | * @param string $unit |
| 103 | + * |
| 104 | + * @return string |
91 | 105 | */ |
92 | 106 | public static function getValidUnit( $unit = null ) { |
93 | 107 | global $egMapsDistanceUnit, $egMapsDistanceUnits; |