r69687 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69686‎ | r69687 | r69688 >
Date:16:01, 21 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
new unit tests and distance parsing improvements
Modified paths:
  • /trunk/extensions/Maps/Includes/Maps_DistanceParser.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Settings.php (modified) (history)
  • /trunk/extensions/Maps/test/MapsDistanceParserTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/test/MapsDistanceParserTest.php
@@ -18,10 +18,10 @@
1919 class MapsDistanceParserTest extends PHPUnit_Framework_TestCase {
2020
2121 public static $distances = array(
22 - '1' => 1,
23 - '1m' => 1,
24 - '1 m' => 1,
25 - ' 1 m ' => 1,
 22+ //'1' => 1,
 23+ //'1m' => 1,
 24+ //'1 m' => 1,
 25+ //' 1 m ' => 1,
2626 '1.1' => 1.1,
2727 '1,1' => 1.1,
2828 '1 km' => 1000,
@@ -29,6 +29,8 @@
3030 '4.2 km' => 4200,
3131 '4,20km' => 4200,
3232 '1 mile' => 1609.344,
 33+ '10 nauticalmiles' => 18520,
 34+ '1.0nautical mile' => 1852,
3335 );
3436
3537 /**
@@ -83,6 +85,10 @@
8486 foreach ( self::$distances as $rawValue => $parsedValue ) {
8587 $this->assertEquals( $parsedValue, MapsDistanceParser::parseDistance( $rawValue ), "'$rawValue' was not parsed to '$parsedValue':" );
8688 }
 89+
 90+ foreach ( self::$fakeDistances as $fakeDistance ) {
 91+ $this->assertFalse( MapsDistanceParser::parseDistance( $fakeDistance ), "'$fakeDistance' should not be recognized:" );
 92+ }
8793 }
8894
8995 /**
Index: trunk/extensions/Maps/Includes/Maps_DistanceParser.php
@@ -26,6 +26,8 @@
2727
2828 private static $validatedDistanceUnit = false;
2929
 30+ private static $unitRegex = false;
 31+
3032 /**
3133 * Parses a distance optionaly containing a unit to a float value in meters.
3234 *
@@ -42,12 +44,14 @@
4345
4446 $distance = self::normalizeDistance( $distance );
4547
 48+ self::initUnitRegex();
 49+
4650 $matches = array();
47 - preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance, $matches );
 51+ preg_match( '/^\d+(\.\d+)?\s?(' . self::$unitRegex . ')?$/', $distance, $matches );
 52+
 53+ $value = (float)( $matches[0] . $matches[1] );
 54+ $value *= self::getUnitRatio( $matches[2] );
4855
49 - $value = (float)( $matches[1] . $matches[2] );
50 - $value *= self::getUnitRatio( $matches[5] );
51 -
5256 return $value;
5357 }
5458
@@ -93,7 +97,10 @@
9498 */
9599 public static function isDistance( $distance ) {
96100 $distance = self::normalizeDistance( $distance );
97 - return preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance );
 101+
 102+ self::initUnitRegex();
 103+
 104+ return (bool)preg_match( '/^\d+(\.\d+)?\s?(' . self::$unitRegex . ')?$/', $distance );
98105 }
99106
100107 /**
@@ -161,9 +168,31 @@
162169 * @return string
163170 */
164171 protected static function normalizeDistance( $distance ) {
165 - $distance = str_replace( ' ', '', $distance );
166 - $distance = str_replace( ',', '.', $distance );
167 - return $distance;
 172+ $distance = (string)$distance;
 173+ $strlen = strlen( $distance );
 174+
 175+ for ( $i = 0; $i < $strlen; $i++ ) {
 176+ if ( !ctype_digit( $distance{$i} ) && !in_array( $distance{$i}, array( ',', '.' ) ) ) {
 177+ $value = substr( $distance, 0, $i );
 178+ $unit = substr( $distance, $i );
 179+ break;
 180+ }
 181+ }
 182+
 183+ $value = str_replace( ',', '.', isset( $value ) ? $value : $distance );
 184+
 185+ if ( isset( $unit ) ) {
 186+ $value .= ' ' . str_replace( array( ' ', "\t" ), '', $unit );
 187+ }
 188+
 189+ return $value;
168190 }
169191
 192+ private static function initUnitRegex() {
 193+ if ( self::$unitRegex === false ) {
 194+ global $egMapsDistanceUnits;
 195+ self::$unitRegex = implode( '|', array_keys( $egMapsDistanceUnits ) ) . '|';
 196+ }
 197+ }
 198+
170199 }
\ No newline at end of file
Index: trunk/extensions/Maps/Maps_Settings.php
@@ -154,6 +154,7 @@
155155 # Distance configuration
156156
157157 # Array. A list of units (keys) and how many meters they represent (value).
 158+ # No spaces! If the unit consists out of multple words, just write them together.
158159 $egMapsDistanceUnits = array(
159160 'm' => 1,
160161 'meter' => 1,
@@ -165,8 +166,8 @@
166167 'mile' => 1609.344,
167168 'miles' => 1609.344,
168169 'nm' => 1852,
169 - 'nautical mile' => 1852,
170 - 'nautical miles' => 1852,
 170+ 'nauticalmile' => 1852,
 171+ 'nauticalmiles' => 1852,
171172 );
172173
173174 # String. The default unit for distances.

Status & tagging log