X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FFormOptions.php;h=cd6e207231358a06f45c60e20a423d0a88ef4cb6;hb=ae03d1a0a38b7aa60a02843ef1e55a680c0166f2;hp=eff175649cee4d3978155321163fcd08649f7dda;hpb=0df93e8f766f7f51acfbffc9fe666713680ec8a7;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/FormOptions.php b/includes/FormOptions.php index eff175649c..cd6e207231 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 @@ class FormOptions implements ArrayAccess { 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 @@ class FormOptions implements ArrayAccess { 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 { @@ -144,10 +150,10 @@ class FormOptions implements ArrayAccess { /** * Use to set the value of an option. * - * @param string $name option name - * @param mixed $value value for the option - * @param bool $force Whether to set the value when it is equivalent to - * the default value for this option (default false). + * @param string $name Option name + * @param mixed $value Value for the option + * @param bool $force Whether to set the value when it is equivalent to the default value for this + * option (default false). */ public function setValue( $name, $value, $force = false ) { $this->validateName( $name, true ); @@ -161,10 +167,9 @@ class FormOptions implements ArrayAccess { } /** - * Get the value for the given option name. - * Internally use getValueReal() + * Get the value for the given option name. Uses getValueReal() internally. * - * @param string $name option name + * @param string $name Option name * @return mixed */ public function getValue( $name ) { @@ -174,9 +179,10 @@ class FormOptions implements ArrayAccess { } /** - * @todo Document - * @param array $option array structure describing the option - * @return mixed Value or the default value if it is null + * Return current option value, based on a structure taken from $options. + * + * @param array $option Array structure describing the option + * @return mixed Value, or the default value if it is null */ protected function getValueReal( $option ) { if ( $option['value'] !== null ) { @@ -234,19 +240,29 @@ class FormOptions implements ArrayAccess { } /** - * Validate and set an option integer value - * The value will be altered to fit in the range. + * @see validateBounds() + */ + public function validateIntBounds( $name, $min, $max ) { + $this->validateBounds( $name, $min, $max ); + } + + /** + * 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 $min minimum value - * @param int $max maximum value + * @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 validateIntBounds( $name, $min, $max ) { + public function validateBounds( $name, $min, $max ) { $this->validateName( $name, true ); + $type = $this->options[$name]['type']; - if ( $this->options[$name]['type'] !== self::INT ) { - throw new MWException( "Option $name is not of type int" ); + 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 @@ class FormOptions implements ArrayAccess { 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;