3 * Helper class to keep track of options when mixing links and form elements.
5 * @author Niklas Laxström
6 * @copyright Copyright © 2008, Niklas Laxström
9 class FormOptions
implements ArrayAccess
{
10 const AUTO
= -1; // ! Automatically detects simple data types
14 const INTNULL
= 3; // ! Useful for namespace selector
16 protected $options = array();
20 public function add( $name, $default, $type = self
::AUTO
) {
22 $option['default'] = $default;
23 $option['value'] = null;
24 $option['consumed'] = false;
26 if ( $type !== self
::AUTO
) {
27 $option['type'] = $type;
29 $option['type'] = self
::guessType( $default );
32 $this->options
[$name] = $option;
35 public function delete( $name ) {
36 $this->validateName( $name, true );
37 unset( $this->options
[$name] );
40 public static function guessType( $data ) {
41 if ( is_bool( $data ) ) {
43 } elseif ( is_int( $data ) ) {
45 } elseif ( is_string( $data ) ) {
48 throw new MWException( 'Unsupported datatype' );
54 public function validateName( $name, $strict = false ) {
55 if ( !isset( $this->options
[$name] ) ) {
57 throw new MWException( "Invalid option $name" );
65 public function setValue( $name, $value, $force = false ) {
66 $this->validateName( $name, true );
68 if ( !$force && $value === $this->options
[$name]['default'] ) {
69 // null default values as unchanged
70 $this->options
[$name]['value'] = null;
72 $this->options
[$name]['value'] = $value;
76 public function getValue( $name ) {
77 $this->validateName( $name, true );
79 return $this->getValueReal( $this->options
[$name] );
82 protected function getValueReal( $option ) {
83 if ( $option['value'] !== null ) {
84 return $option['value'];
86 return $option['default'];
90 public function reset( $name ) {
91 $this->validateName( $name, true );
92 $this->options
[$name]['value'] = null;
95 public function consumeValue( $name ) {
96 $this->validateName( $name, true );
97 $this->options
[$name]['consumed'] = true;
99 return $this->getValueReal( $this->options
[$name] );
102 public function consumeValues( /*Array*/ $names ) {
105 foreach ( $names as $name ) {
106 $this->validateName( $name, true );
107 $this->options
[$name]['consumed'] = true;
108 $out[] = $this->getValueReal( $this->options
[$name] );
116 public function validateIntBounds( $name, $min, $max ) {
117 $this->validateName( $name, true );
119 if ( $this->options
[$name]['type'] !== self
::INT ) {
120 throw new MWException( "Option $name is not of type int" );
123 $value = $this->getValueReal( $this->options
[$name] );
124 $value = max( $min, min( $max, $value ) );
126 $this->setValue( $name, $value );
129 # Getting the data out for use
131 public function getUnconsumedValues( $all = false ) {
134 foreach ( $this->options
as $name => $data ) {
135 if ( !$data['consumed'] ) {
136 if ( $all ||
$data['value'] !== null ) {
137 $values[$name] = $this->getValueReal( $data );
145 public function getChangedValues() {
148 foreach ( $this->options
as $name => $data ) {
149 if ( $data['value'] !== null ) {
150 $values[$name] = $data['value'];
157 public function getAllValues() {
160 foreach ( $this->options
as $name => $data ) {
161 $values[$name] = $this->getValueReal( $data );
169 public function fetchValuesFromRequest( WebRequest
$r, $values = false ) {
171 $values = array_keys( $this->options
);
174 foreach ( $values as $name ) {
175 $default = $this->options
[$name]['default'];
176 $type = $this->options
[$name]['type'];
180 $value = $r->getBool( $name, $default ); break;
182 $value = $r->getInt( $name, $default ); break;
184 $value = $r->getText( $name, $default ); break;
186 $value = $r->getIntOrNull( $name ); break;
188 throw new MWException( 'Unsupported datatype' );
191 if ( $value !== null ) {
192 $this->options
[$name]['value'] = $value === $default ?
null : $value;
197 /* ArrayAccess methods */
198 public function offsetExists( $name ) {
199 return isset( $this->options
[$name] );
202 public function offsetGet( $name ) {
203 return $this->getValue( $name );
206 public function offsetSet( $name, $value ) {
207 $this->setValue( $name, $value );
210 public function offsetUnset( $name ) {
211 $this->delete( $name );