Merge "CologneBlue rewrite: kill mWatchLinkNum, watchThisPage() is only called once...
[lhc/web/wiklou.git] / includes / FormOptions.php
1 <?php
2 /**
3 * Helper class to keep track of options when mixing links and form elements.
4 *
5 * Copyright © 2008, Niklas Laxstiröm
6 * Copyright © 2011, Antoine Musso
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @author Niklas Laxström
25 * @author Antoine Musso
26 */
27
28 /**
29 * Helper class to keep track of options when mixing links and form elements.
30 *
31 * @todo This badly need some examples and tests :-)
32 */
33 class FormOptions implements ArrayAccess {
34 /** @name Type constants
35 * Used internally to map an option value to a WebRequest accessor
36 */
37 /* @{ */
38 /** Mark value for automatic detection (for simple data types only) */
39 const AUTO = -1;
40 /** String type, maps guessType() to WebRequest::getText() */
41 const STRING = 0;
42 /** Integer type, maps guessType() to WebRequest::getInt() */
43 const INT = 1;
44 /** Boolean type, maps guessType() to WebRequest::getBool() */
45 const BOOL = 2;
46 /** Integer type or null, maps to WebRequest::getIntOrNull()
47 * This is useful for the namespace selector.
48 */
49 const INTNULL = 3;
50 /* @} */
51
52 /**
53 * @todo Document!
54 */
55 protected $options = array();
56
57 # Setting up
58
59 public function add( $name, $default, $type = self::AUTO ) {
60 $option = array();
61 $option['default'] = $default;
62 $option['value'] = null;
63 $option['consumed'] = false;
64
65 if ( $type !== self::AUTO ) {
66 $option['type'] = $type;
67 } else {
68 $option['type'] = self::guessType( $default );
69 }
70
71 $this->options[$name] = $option;
72 }
73
74 public function delete( $name ) {
75 $this->validateName( $name, true );
76 unset( $this->options[$name] );
77 }
78
79 /**
80 * Used to find out which type the data is.
81 * All types are defined in the 'Type constants' section of this class
82 * Please note we do not support detection of INTNULL MediaWiki type
83 * which will be assumed as INT if the data is an integer.
84 *
85 * @param $data Mixed: value to guess type for
86 * @exception MWException Unsupported datatype
87 * @return int Type constant
88 */
89 public static function guessType( $data ) {
90 if ( is_bool( $data ) ) {
91 return self::BOOL;
92 } elseif ( is_int( $data ) ) {
93 return self::INT;
94 } elseif ( is_string( $data ) ) {
95 return self::STRING;
96 } else {
97 throw new MWException( 'Unsupported datatype' );
98 }
99 }
100
101 # Handling values
102
103 /**
104 * Verify the given option name exist.
105 *
106 * @param $name String: option name
107 * @param $strict Boolean: throw an exception when the option does not exist (default false)
108 * @return Boolean: true if option exist, false otherwise
109 */
110 public function validateName( $name, $strict = false ) {
111 if ( !isset( $this->options[$name] ) ) {
112 if ( $strict ) {
113 throw new MWException( "Invalid option $name" );
114 } else {
115 return false;
116 }
117 }
118 return true;
119 }
120
121 /**
122 * Use to set the value of an option.
123 *
124 * @param $name String: option name
125 * @param $value Mixed: value for the option
126 * @param $force Boolean: whether to set the value when it is equivalent to the default value for this option (default false).
127 * @return null
128 */
129 public function setValue( $name, $value, $force = false ) {
130 $this->validateName( $name, true );
131
132 if ( !$force && $value === $this->options[$name]['default'] ) {
133 // null default values as unchanged
134 $this->options[$name]['value'] = null;
135 } else {
136 $this->options[$name]['value'] = $value;
137 }
138 }
139
140 /**
141 * Get the value for the given option name.
142 * Internally use getValueReal()
143 *
144 * @param $name String: option name
145 * @return Mixed
146 */
147 public function getValue( $name ) {
148 $this->validateName( $name, true );
149
150 return $this->getValueReal( $this->options[$name] );
151 }
152
153 /**
154 * @todo Document
155 * @param $option Array: array structure describing the option
156 * @return Mixed. Value or the default value if it is null
157 */
158 protected function getValueReal( $option ) {
159 if ( $option['value'] !== null ) {
160 return $option['value'];
161 } else {
162 return $option['default'];
163 }
164 }
165
166 /**
167 * Delete the option value.
168 * This will make future calls to getValue() return the default value.
169 * @param $name String: option name
170 * @return null
171 */
172 public function reset( $name ) {
173 $this->validateName( $name, true );
174 $this->options[$name]['value'] = null;
175 }
176
177 /**
178 * @todo Document
179 * @param $name String: option name
180 * @return null
181 */
182 public function consumeValue( $name ) {
183 $this->validateName( $name, true );
184 $this->options[$name]['consumed'] = true;
185
186 return $this->getValueReal( $this->options[$name] );
187 }
188
189 /**
190 * @todo Document
191 * @param $names Array: array of option names
192 * @return null
193 */
194 public function consumeValues( /*Array*/ $names ) {
195 $out = array();
196
197 foreach ( $names as $name ) {
198 $this->validateName( $name, true );
199 $this->options[$name]['consumed'] = true;
200 $out[] = $this->getValueReal( $this->options[$name] );
201 }
202
203 return $out;
204 }
205
206 /**
207 * Validate and set an option integer value
208 * The value will be altered to fit in the range.
209 *
210 * @param $name String: option name
211 * @param $min Int: minimum value
212 * @param $max Int: maximum value
213 * @exception MWException Option is not of type int
214 * @return null
215 */
216 public function validateIntBounds( $name, $min, $max ) {
217 $this->validateName( $name, true );
218
219 if ( $this->options[$name]['type'] !== self::INT ) {
220 throw new MWException( "Option $name is not of type int" );
221 }
222
223 $value = $this->getValueReal( $this->options[$name] );
224 $value = max( $min, min( $max, $value ) );
225
226 $this->setValue( $name, $value );
227 }
228
229 /**
230 * Getting the data out for use
231 * @param $all Boolean: whether to include unchanged options (default: false)
232 * @return Array
233 */
234 public function getUnconsumedValues( $all = false ) {
235 $values = array();
236
237 foreach ( $this->options as $name => $data ) {
238 if ( !$data['consumed'] ) {
239 if ( $all || $data['value'] !== null ) {
240 $values[$name] = $this->getValueReal( $data );
241 }
242 }
243 }
244
245 return $values;
246 }
247
248 /**
249 * Return options modified as an array ( name => value )
250 * @return Array
251 */
252 public function getChangedValues() {
253 $values = array();
254
255 foreach ( $this->options as $name => $data ) {
256 if ( $data['value'] !== null ) {
257 $values[$name] = $data['value'];
258 }
259 }
260
261 return $values;
262 }
263
264 /**
265 * Format options to an array ( name => value)
266 * @return Array
267 */
268 public function getAllValues() {
269 $values = array();
270
271 foreach ( $this->options as $name => $data ) {
272 $values[$name] = $this->getValueReal( $data );
273 }
274
275 return $values;
276 }
277
278 # Reading values
279
280 public function fetchValuesFromRequest( WebRequest $r, $values = false ) {
281 if ( !$values ) {
282 $values = array_keys( $this->options );
283 }
284
285 foreach ( $values as $name ) {
286 $default = $this->options[$name]['default'];
287 $type = $this->options[$name]['type'];
288
289 switch( $type ) {
290 case self::BOOL:
291 $value = $r->getBool( $name, $default ); break;
292 case self::INT:
293 $value = $r->getInt( $name, $default ); break;
294 case self::STRING:
295 $value = $r->getText( $name, $default ); break;
296 case self::INTNULL:
297 $value = $r->getIntOrNull( $name ); break;
298 default:
299 throw new MWException( 'Unsupported datatype' );
300 }
301
302 if ( $value !== null ) {
303 $this->options[$name]['value'] = $value === $default ? null : $value;
304 }
305 }
306 }
307
308 /** @name ArrayAccess functions
309 * Those function implements PHP ArrayAccess interface
310 * @see http://php.net/manual/en/class.arrayaccess.php
311 */
312 /* @{ */
313 /**
314 * Whether option exist
315 * @return bool
316 */
317 public function offsetExists( $name ) {
318 return isset( $this->options[$name] );
319 }
320 /**
321 * Retrieve an option value
322 * @return Mixed
323 */
324 public function offsetGet( $name ) {
325 return $this->getValue( $name );
326 }
327 /** Set an option to given value */
328 public function offsetSet( $name, $value ) {
329 $this->setValue( $name, $value );
330 }
331 /** Delete the option */
332 public function offsetUnset( $name ) {
333 $this->delete( $name );
334 }
335 /* @} */
336 }