Merge "Lazy load PasswordReset on SpecialPasswordReset"
[lhc/web/wiklou.git] / includes / FormOptions.php
index 54822e3..e558944 100644 (file)
@@ -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,12 +43,18 @@ 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()
         * This is useful for the namespace selector.
         */
        const INTNULL = 3;
+       /** Array type, maps guessType() to WebRequest::getArray()
+        * @since 1.29 */
+       const ARR = 5;
        /* @} */
 
        /**
@@ -60,7 +67,7 @@ class FormOptions implements ArrayAccess {
         *   consumeValue() or consumeValues()
         * - 'type' - one of the type constants (but never AUTO)
         */
-       protected $options = array();
+       protected $options = [];
 
        # Setting up
 
@@ -72,7 +79,7 @@ class FormOptions implements ArrayAccess {
         * @param int $type One of the type constants (optional, defaults to AUTO)
         */
        public function add( $name, $default, $type = self::AUTO ) {
-               $option = array();
+               $option = [];
                $option['default'] = $default;
                $option['value'] = null;
                $option['consumed'] = false;
@@ -112,8 +119,12 @@ 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;
+               } elseif ( is_array( $data ) ) {
+                       return self::ARR;
                } else {
                        throw new MWException( 'Unsupported datatype' );
                }
@@ -222,7 +233,7 @@ class FormOptions implements ArrayAccess {
         * @return array Array of option values, or the default values if they are null
         */
        public function consumeValues( $names ) {
-               $out = array();
+               $out = [];
 
                foreach ( $names as $name ) {
                        $this->validateName( $name, true );
@@ -234,19 +245,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.
         *
-        * @param string $name option name
-        * @param int $min minimum value
-        * @param int $max maximum value
+        * @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 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] );
@@ -262,7 +283,7 @@ class FormOptions implements ArrayAccess {
         * @return array
         */
        public function getUnconsumedValues( $all = false ) {
-               $values = array();
+               $values = [];
 
                foreach ( $this->options as $name => $data ) {
                        if ( !$data['consumed'] ) {
@@ -280,7 +301,7 @@ class FormOptions implements ArrayAccess {
         * @return array
         */
        public function getChangedValues() {
-               $values = array();
+               $values = [];
 
                foreach ( $this->options as $name => $data ) {
                        if ( $data['value'] !== null ) {
@@ -296,7 +317,7 @@ class FormOptions implements ArrayAccess {
         * @return array
         */
        public function getAllValues() {
-               $values = array();
+               $values = [];
 
                foreach ( $this->options as $name => $data ) {
                        $values[$name] = $this->getValueReal( $data );
@@ -333,12 +354,18 @@ 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;
                                case self::INTNULL:
                                        $value = $r->getIntOrNull( $name );
                                        break;
+                               case self::ARR:
+                                       $value = $r->getArray( $name );
+                                       break;
                                default:
                                        throw new MWException( 'Unsupported datatype' );
                        }
@@ -354,22 +381,37 @@ class FormOptions implements ArrayAccess {
         * @see http://php.net/manual/en/class.arrayaccess.php
         */
        /* @{ */
-       /** Whether the option exists. */
+       /**
+        * Whether the option exists.
+        * @param string $name
+        * @return bool
+        */
        public function offsetExists( $name ) {
                return isset( $this->options[$name] );
        }
 
-       /** Retrieve an option value. */
+       /**
+        * Retrieve an option value.
+        * @param string $name
+        * @return mixed
+        */
        public function offsetGet( $name ) {
                return $this->getValue( $name );
        }
 
-       /** Set an option to given value. */
+       /**
+        * Set an option to given value.
+        * @param string $name
+        * @param mixed $value
+        */
        public function offsetSet( $name, $value ) {
                $this->setValue( $name, $value );
        }
 
-       /** Delete the option. */
+       /**
+        * Delete the option.
+        * @param string $name
+        */
        public function offsetUnset( $name ) {
                $this->delete( $name );
        }