Added alias of jQuery to $j - this is only to get site/user scripts that were using...
[lhc/web/wiklou.git] / includes / FormOptions.php
index 8c7849e..df5d41d 100644 (file)
@@ -6,12 +6,12 @@
  * @copyright Copyright © 2008, Niklas Laxström
  */
 
-class FormOptions {
-       const AUTO = -1; //! Automatically detects simple data types
+class FormOptions implements ArrayAccess {
+       const AUTO = -1; // ! Automatically detects simple data types
        const STRING = 0;
        const INT = 1;
        const BOOL = 2;
-       const INTNULL = 3; //! Useful for namespace selector
+       const INTNULL = 3; // ! Useful for namespace selector
 
        protected $options = array();
 
@@ -32,12 +32,17 @@ class FormOptions {
                $this->options[$name] = $option;
        }
 
+       public function delete( $name ) {
+               $this->validateName( $name, true );
+               unset( $this->options[$name] );
+       }
+
        public static function guessType( $data ) {
-               if ( is_bool($data) ) {
+               if ( is_bool( $data ) ) {
                        return self::BOOL;
-               } elseif( is_int($data) ) {
+               } elseif ( is_int( $data ) ) {
                        return self::INT;
-               } elseif( is_string($data) ) {
+               } elseif ( is_string( $data ) ) {
                        return self::STRING;
                } else {
                        throw new MWException( 'Unsupported datatype' );
@@ -47,7 +52,7 @@ class FormOptions {
        # Handling values
 
        public function validateName( $name, $strict = false ) {
-               if ( !isset($this->options[$name]) ) {
+               if ( !isset( $this->options[$name] ) ) {
                        if ( $strict ) {
                                throw new MWException( "Invalid option $name" );
                        } else {
@@ -59,6 +64,7 @@ class FormOptions {
 
        public function setValue( $name, $value, $force = false ) {
                $this->validateName( $name, true );
+
                if ( !$force && $value === $this->options[$name]['default'] ) {
                        // null default values as unchanged
                        $this->options[$name]['value'] = null;
@@ -69,6 +75,7 @@ class FormOptions {
 
        public function getValue( $name ) {
                $this->validateName( $name, true );
+
                return $this->getValueReal( $this->options[$name] );
        }
 
@@ -80,19 +87,27 @@ class FormOptions {
                }
        }
 
+       public function reset( $name ) {
+               $this->validateName( $name, true );
+               $this->options[$name]['value'] = null;
+       }
+
        public function consumeValue( $name ) {
                $this->validateName( $name, true );
                $this->options[$name]['consumed'] = true;
+
                return $this->getValueReal( $this->options[$name] );
        }
 
        public function consumeValues( /*Array*/ $names ) {
                $out = array();
+
                foreach ( $names as $name ) {
                        $this->validateName( $name, true );
                        $this->options[$name]['consumed'] = true;
                        $out[] = $this->getValueReal( $this->options[$name] );
                }
+
                return $out;
        }
 
@@ -101,8 +116,9 @@ class FormOptions {
        public function validateIntBounds( $name, $min, $max ) {
                $this->validateName( $name, true );
 
-               if ( $this->options[$name]['type'] !== self::INT )
+               if ( $this->options[$name]['type'] !== self::INT ) {
                        throw new MWException( "Option $name is not of type int" );
+               }
 
                $value = $this->getValueReal( $this->options[$name] );
                $value = max( $min, min( $max, $value ) );
@@ -114,6 +130,7 @@ class FormOptions {
 
        public function getUnconsumedValues( $all = false ) {
                $values = array();
+
                foreach ( $this->options as $name => $data ) {
                        if ( !$data['consumed'] ) {
                                if ( $all || $data['value'] !== null ) {
@@ -121,23 +138,40 @@ class FormOptions {
                                }
                        }
                }
+
                return $values;
        }
 
        public function getChangedValues() {
                $values = array();
+
                foreach ( $this->options as $name => $data ) {
                        if ( $data['value'] !== null ) {
                                $values[$name] = $data['value'];
                        }
                }
+
+               return $values;
+       }
+
+       public function getAllValues() {
+               $values = array();
+
+               foreach ( $this->options as $name => $data ) {
+                       $values[$name] = $this->getValueReal( $data );
+               }
+
                return $values;
        }
 
        # Reading values
 
-       public function fetchValuesFromRequest( WebRequest $r ) {
-               foreach ( array_keys($this->options) as $name ) {
+       public function fetchValuesFromRequest( WebRequest $r, $values = false ) {
+               if ( !$values ) {
+                       $values = array_keys( $this->options );
+               }
+
+               foreach ( $values as $name ) {
                        $default = $this->options[$name]['default'];
                        $type = $this->options[$name]['type'];
 
@@ -154,10 +188,26 @@ class FormOptions {
                                        throw new MWException( 'Unsupported datatype' );
                        }
 
-                       if ( $value !== $default && $value !== null ) {
-                               $this->options[$name]['value'] = $value;
+                       if ( $value !== null ) {
+                               $this->options[$name]['value'] = $value === $default ? null : $value;
                        }
                }
        }
 
+       /* ArrayAccess methods */
+       public function offsetExists( $name ) {
+               return isset( $this->options[$name] );
+       }
+
+       public function offsetGet( $name ) {
+               return $this->getValue( $name );
+       }
+
+       public function offsetSet( $name, $value ) {
+               $this->setValue( $name, $value );
+       }
+
+       public function offsetUnset( $name ) {
+               $this->delete( $name );
+       }
 }