MediaWiki REL1_41
BooleanDef.php
Go to the documentation of this file.
1<?php
2
4
10
23class BooleanDef extends TypeDef {
24
25 public static $TRUEVALS = [ 'true', 't', 'yes', 'y', 'on', '1' ];
26 public static $FALSEVALS = [ 'false', 'f', 'no', 'n', 'off', '0' ];
27
28 public function validate( $name, $value, array $settings, array $options ) {
29 $value = strtolower( $value );
30 if ( in_array( $value, self::$TRUEVALS, true ) ) {
31 return true;
32 }
33 if ( $value === '' || in_array( $value, self::$FALSEVALS, true ) ) {
34 return false;
35 }
36
37 $this->failure(
38 $this->failureMessage( 'badbool' )
39 ->textListParams( array_map( [ $this, 'quoteVal' ], self::$TRUEVALS ) )
40 ->numParams( count( self::$TRUEVALS ) )
41 ->textListParams( array_merge(
42 array_map( [ $this, 'quoteVal' ], self::$FALSEVALS ),
43 [ MessageValue::new( 'paramvalidator-emptystring' ) ]
44 ) )
45 ->numParams( count( self::$FALSEVALS ) + 1 ),
46 $name, $value, $settings, $options
47 );
48 }
49
50 private function quoteVal( $v ) {
51 return new ScalarParam( ParamType::TEXT, "\"$v\"" );
52 }
53
54 public function stringifyValue( $name, $value, array $settings, array $options ) {
55 return $value ? self::$TRUEVALS[0] : self::$FALSEVALS[0];
56 }
57
58 public function getHelpInfo( $name, array $settings, array $options ) {
59 $info = parent::getHelpInfo( $name, $settings, $options );
60
61 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-boolean' )
62 ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 );
63
64 return $info;
65 }
66
67}
Value object representing a message for i18n.
The constants used to specify parameter types.
Definition ParamType.php:11
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition ParamType.php:13
Value object representing a message parameter holding a single value.
Service for formatting and validating API parameters.
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
const PARAM_TYPE
(string|array) Type of the parameter.
Type definition for boolean types.
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
validate( $name, $value, array $settings, array $options)
Validate the value.
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Base definition for ParamValidator types.
Definition TypeDef.php:19
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition TypeDef.php:84
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:49