From 4530268205f51284b220b7397b793bcd47ee6349 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Sun, 8 Jun 2008 19:23:54 +0000 Subject: [PATCH] * Improvements to FormOptions --- includes/FormOptions.php | 45 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/includes/FormOptions.php b/includes/FormOptions.php index 8c7849e8e6..5888a0c431 100644 --- a/includes/FormOptions.php +++ b/includes/FormOptions.php @@ -6,7 +6,7 @@ * @copyright Copyright © 2008, Niklas Laxström */ -class FormOptions { +class FormOptions implements ArrayAccess { const AUTO = -1; //! Automatically detects simple data types const STRING = 0; const INT = 1; @@ -32,6 +32,11 @@ 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) ) { return self::BOOL; @@ -80,6 +85,11 @@ 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; @@ -134,10 +144,22 @@ class FormOptions { 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']; @@ -160,4 +182,21 @@ class FormOptions { } } + /* ArrayAccess methods */ + public function offsetExists( $name ) { + return isset($this->options[$name]); + } + + public function offsetGet( $name ) { + return $this->getValue( $name ); + } + + public function offsetSet( $name, $value ) { + return $this->setValue( $name, $value ); + } + + public function offsetUnset( $name ) { + return $this->delete( $name ); + } + } -- 2.20.1