Bartosz Dziewoński has uploaded a new change for review.
https://gerrit.wikimedia.org/r/94274
Change subject: FormOptions: Implement FLOAT type
......................................................................
FormOptions: Implement FLOAT type
Also added WebRequest#getFloat().
Change-Id: I854f09bd26287880a2806852274471904bc33092
---
M includes/FormOptions.php
M includes/WebRequest.php
M tests/phpunit/includes/FormOptionsTest.php
3 files changed, 52 insertions(+), 10 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/74/94274/1
diff --git a/includes/FormOptions.php b/includes/FormOptions.php
index 54822e3..cd6e207 100644
--- a/includes/FormOptions.php
+++ b/includes/FormOptions.php
@@ -4,6 +4,7 @@
*
* Copyright © 2008, Niklas Laxström
* Copyright © 2011, Antoine Musso
+ * Copyright © 2013, Bartosz Dziewoński
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -42,6 +43,9 @@
const STRING = 0;
/** Integer type, maps guessType() to WebRequest::getInt() */
const INT = 1;
+ /** Float type, maps guessType() to WebRequest::getFloat()
+ * @since 1.23 */
+ const FLOAT = 4;
/** Boolean type, maps guessType() to WebRequest::getBool() */
const BOOL = 2;
/** Integer type or null, maps to WebRequest::getIntOrNull()
@@ -112,6 +116,8 @@
return self::BOOL;
} elseif ( is_int( $data ) ) {
return self::INT;
+ } elseif ( is_float( $data ) ) {
+ return self::FLOAT;
} elseif ( is_string( $data ) ) {
return self::STRING;
} else {
@@ -234,19 +240,29 @@
}
/**
- * Validate and set an option integer value
- * The value will be altered to fit in the range.
- *
- * @param string $name option name
- * @param int $min minimum value
- * @param int $max maximum value
- * @throws MWException If option is not of type INT
+ * @see validateBounds()
*/
public function validateIntBounds( $name, $min, $max ) {
- $this->validateName( $name, true );
+ $this->validateBounds( $name, $min, $max );
+ }
- if ( $this->options[$name]['type'] !== self::INT ) {
- throw new MWException( "Option $name is not of type
int" );
+ /**
+ * Constrain a numeric value for a given option to a given range. The
value will be altered to fit
+ * in the range.
+ *
+ * @since 1.23
+ *
+ * @param string $name Option name
+ * @param int|float $min Minimum value
+ * @param int|float $max Maximum value
+ * @throws MWException If option is not of type INT
+ */
+ public function validateBounds( $name, $min, $max ) {
+ $this->validateName( $name, true );
+ $type = $this->options[$name]['type'];
+
+ if ( $type !== self::INT && $type !== self::FLOAT ) {
+ throw new MWException( "Option $name is not of type INT
or FLOAT" );
}
$value = $this->getValueReal( $this->options[$name] );
@@ -333,6 +349,9 @@
case self::INT:
$value = $r->getInt( $name, $default );
break;
+ case self::FLOAT:
+ $value = $r->getFloat( $name, $default
);
+ break;
case self::STRING:
$value = $r->getText( $name, $default );
break;
diff --git a/includes/WebRequest.php b/includes/WebRequest.php
index 462b312..4ad7344 100644
--- a/includes/WebRequest.php
+++ b/includes/WebRequest.php
@@ -481,6 +481,20 @@
}
/**
+ * Fetch a floating point value from the input or return $default if
not set.
+ * Guaranteed to return a float; non-numeric input will typically
+ * return 0.
+ *
+ * @since 1.23
+ * @param $name String
+ * @param $default Float
+ * @return Float
+ */
+ public function getFloat( $name, $default = 0 ) {
+ return floatval( $this->getVal( $name, $default ) );
+ }
+
+ /**
* Fetch a boolean value from the input or return $default if not set.
* Guaranteed to return true or false, with normal PHP semantics for
* boolean interpretation of strings.
diff --git a/tests/phpunit/includes/FormOptionsTest.php
b/tests/phpunit/includes/FormOptionsTest.php
index 08d6ba8..665fa39 100644
--- a/tests/phpunit/includes/FormOptionsTest.php
+++ b/tests/phpunit/includes/FormOptionsTest.php
@@ -34,6 +34,7 @@
$this->object->add( 'string1', 'string one' );
$this->object->add( 'string2', 'string two' );
$this->object->add( 'integer', 0 );
+ $this->object->add( 'float', 0.0 );
$this->object->add( 'intnull', 0, FormOptions::INTNULL );
}
@@ -44,6 +45,9 @@
}
private function assertGuessInt( $data ) {
$this->guess( FormOptions::INT, $data );
+ }
+ private function assertGuessFloat( $data ) {
+ $this->guess( FormOptions::FLOAT, $data );
}
private function assertGuessString( $data ) {
$this->guess( FormOptions::STRING, $data );
@@ -71,10 +75,15 @@
$this->assertGuessInt( 5 );
$this->assertGuessInt( 0x0F );
+ $this->assertGuessFloat( 0.0 );
+ $this->assertGuessFloat( 1.5 );
+ $this->assertGuessFloat( 1e3 );
+
$this->assertGuessString( 'true' );
$this->assertGuessString( 'false' );
$this->assertGuessString( '5' );
$this->assertGuessString( '0' );
+ $this->assertGuessString( '1.5' );
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/94274
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I854f09bd26287880a2806852274471904bc33092
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits