3 * Helper class to keep track of options when mixing links and form elements.
4 * @todo This badly need some examples and tests :-)
6 * Copyright © 2008, Niklas Laxstiröm
8 * Copyright © 2011, Antoine Musso
10 * @author Niklas Laxström
11 * @author Antoine Musso
14 class FormOptions
implements ArrayAccess
{
15 /** @name Type constants
16 * Used internally to map an option value to a WebRequest accessor
19 /** Mark value for automatic detection (for simple data types only) */
21 /** String type, maps guessType() to WebRequest::getText() */
23 /** Integer type, maps guessType() to WebRequest::getInt() */
25 /** Boolean type, maps guessType() to WebRequest::getBool() */
27 /** Integer type or null, maps to WebRequest::getIntOrNull()
28 * This is useful for the namespace selector.
36 protected $options = array();
40 public function add( $name, $default, $type = self
::AUTO
) {
42 $option['default'] = $default;
43 $option['value'] = null;
44 $option['consumed'] = false;
46 if ( $type !== self
::AUTO
) {
47 $option['type'] = $type;
49 $option['type'] = self
::guessType( $default );
52 $this->options
[$name] = $option;
55 public function delete( $name ) {
56 $this->validateName( $name, true );
57 unset( $this->options
[$name] );
61 * Used to find out which type the data is.
62 * All types are defined in the 'Type constants' section of this class
63 * Please note we do not support detection of INTNULL MediaWiki type
64 * which will be assumed as INT if the data is an integer.
66 * @param $data Mixed: value to guess type for
67 * @exception MWException Unsupported datatype
68 * @return int Type constant
70 public static function guessType( $data ) {
71 if ( is_bool( $data ) ) {
73 } elseif ( is_int( $data ) ) {
75 } elseif ( is_string( $data ) ) {
78 throw new MWException( 'Unsupported datatype' );
85 * Verify the given option name exist.
87 * @param $name String: option name
88 * @param $strict Boolean: throw an exception when the option does not exist (default false)
89 * @return Boolean: true if option exist, false otherwise
91 public function validateName( $name, $strict = false ) {
92 if ( !isset( $this->options
[$name] ) ) {
94 throw new MWException( "Invalid option $name" );
103 * Use to set the value of an option.
105 * @param $name String: option name
106 * @param $value Mixed: value for the option
107 * @param $force Boolean: whether to set the value when it is equivalent to the default value for this option (default false).
110 public function setValue( $name, $value, $force = false ) {
111 $this->validateName( $name, true );
113 if ( !$force && $value === $this->options
[$name]['default'] ) {
114 // null default values as unchanged
115 $this->options
[$name]['value'] = null;
117 $this->options
[$name]['value'] = $value;
122 * Get the value for the given option name.
123 * Internally use getValueReal()
125 * @param $name String: option name
128 public function getValue( $name ) {
129 $this->validateName( $name, true );
131 return $this->getValueReal( $this->options
[$name] );
136 * @param $option Array: array structure describing the option
137 * @return Mixed. Value or the default value if it is null
139 protected function getValueReal( $option ) {
140 if ( $option['value'] !== null ) {
141 return $option['value'];
143 return $option['default'];
148 * Delete the option value.
149 * This will make future calls to getValue() return the default value.
150 * @param $name String: option name
153 public function reset( $name ) {
154 $this->validateName( $name, true );
155 $this->options
[$name]['value'] = null;
160 * @param $name String: option name
163 public function consumeValue( $name ) {
164 $this->validateName( $name, true );
165 $this->options
[$name]['consumed'] = true;
167 return $this->getValueReal( $this->options
[$name] );
172 * @param $names Array: array of option names
175 public function consumeValues( /*Array*/ $names ) {
178 foreach ( $names as $name ) {
179 $this->validateName( $name, true );
180 $this->options
[$name]['consumed'] = true;
181 $out[] = $this->getValueReal( $this->options
[$name] );
188 * Validate and set an option integer value
189 * The value will be altered to fit in the range.
191 * @param $name String: option name
192 * @param $min Int: minimum value
193 * @param $max Int: maximum value
194 * @exception MWException Option is not of type int
197 public function validateIntBounds( $name, $min, $max ) {
198 $this->validateName( $name, true );
200 if ( $this->options
[$name]['type'] !== self
::INT ) {
201 throw new MWException( "Option $name is not of type int" );
204 $value = $this->getValueReal( $this->options
[$name] );
205 $value = max( $min, min( $max, $value ) );
207 $this->setValue( $name, $value );
211 * Getting the data out for use
212 * @param $all Boolean: whether to include unchanged options (default: false)
215 public function getUnconsumedValues( $all = false ) {
218 foreach ( $this->options
as $name => $data ) {
219 if ( !$data['consumed'] ) {
220 if ( $all ||
$data['value'] !== null ) {
221 $values[$name] = $this->getValueReal( $data );
230 * Return options modified as an array ( name => value )
233 public function getChangedValues() {
236 foreach ( $this->options
as $name => $data ) {
237 if ( $data['value'] !== null ) {
238 $values[$name] = $data['value'];
246 * Format options to an array ( name => value)
249 public function getAllValues() {
252 foreach ( $this->options
as $name => $data ) {
253 $values[$name] = $this->getValueReal( $data );
261 public function fetchValuesFromRequest( WebRequest
$r, $values = false ) {
263 $values = array_keys( $this->options
);
266 foreach ( $values as $name ) {
267 $default = $this->options
[$name]['default'];
268 $type = $this->options
[$name]['type'];
272 $value = $r->getBool( $name, $default ); break;
274 $value = $r->getInt( $name, $default ); break;
276 $value = $r->getText( $name, $default ); break;
278 $value = $r->getIntOrNull( $name ); break;
280 throw new MWException( 'Unsupported datatype' );
283 if ( $value !== null ) {
284 $this->options
[$name]['value'] = $value === $default ?
null : $value;
289 /** @name ArrayAccess functions
290 * Those function implements PHP ArrayAccess interface
291 * @see http://php.net/manual/en/class.arrayaccess.php
295 * Whether option exist
298 public function offsetExists( $name ) {
299 return isset( $this->options
[$name] );
302 * Retrieve an option value
305 public function offsetGet( $name ) {
306 return $this->getValue( $name );
308 /** Set an option to given value */
309 public function offsetSet( $name, $value ) {
310 $this->setValue( $name, $value );
312 /** Delete the option */
313 public function offsetUnset( $name ) {
314 $this->delete( $name );