r67449 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67448‎ | r67449 | r67450 >
Date:10:12, 6 June 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.3.2 - improved distance code
Modified paths:
  • /trunk/extensions/Maps/Includes/Maps_DistanceParser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Includes/Maps_DistanceParser.php
@@ -16,10 +16,6 @@
1717 /**
1818 * Static class for distance validation and parsing. Internal representatations are in meters.
1919 *
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 - *
2420 * @ingroup Maps
2521 *
2622 * @author Jeroen De Dauw
@@ -36,8 +32,6 @@
3733 * @return float The distance in meters.
3834 */
3935 public static function parseDistance( $distance ) {
40 - global $egMapsDistanceUnits;
41 -
4236 if ( !self::isDistance( $distance ) ) {
4337 return false;
4438 }
@@ -46,13 +40,8 @@
4741 preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance, $matches );
4842
4943 $value = (float)( $matches[1] . $matches[2] );
50 - $unit = $matches[5];
 44+ $value *= self::getUnitRatio( $matches[5] );
5145
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 -
5746 return $value;
5847 }
5948
@@ -70,14 +59,37 @@
7160 return "$meters $unit";
7261 }
7362
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 );
7674 }
7775
 76+ /**
 77+ * Returns if the provided string is a valid distance.
 78+ *
 79+ * @param string $distance
 80+ *
 81+ * @return boolean
 82+ */
7883 public static function isDistance( $distance ) {
7984 return preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance );
8085 }
8186
 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+ */
8294 public static function getUnitRatio( $unit = null ) {
8395 global $egMapsDistanceUnits;
8496 return $egMapsDistanceUnits[self::getValidUnit( $unit )];
@@ -87,6 +99,8 @@
88100 * Returns a valid unit. If the provided one is invalid, the default will be used.
89101 *
90102 * @param string $unit
 103+ *
 104+ * @return string
91105 */
92106 public static function getValidUnit( $unit = null ) {
93107 global $egMapsDistanceUnit, $egMapsDistanceUnits;

Status & tagging log