MediaWiki  master
BooleanDef.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
23 class 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 }
Wikimedia\ParamValidator\TypeDef\failureMessage
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition: TypeDef.php:78
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:43
Wikimedia\ParamValidator\TypeDef\BooleanDef\quoteVal
quoteVal( $v)
Definition: BooleanDef.php:50
Wikimedia\ParamValidator\TypeDef\BooleanDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: BooleanDef.php:58
Wikimedia\ParamValidator\TypeDef\BooleanDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: BooleanDef.php:54
Wikimedia\Message\ScalarParam
Value object representing a message parameter holding a single value.
Definition: ScalarParam.php:10
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:18
Wikimedia\ParamValidator\TypeDef\BooleanDef
Type definition for boolean types.
Definition: BooleanDef.php:23
Wikimedia\ParamValidator\TypeDef\BooleanDef\$FALSEVALS
static $FALSEVALS
Definition: BooleanDef.php:26
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\TypeDef\BooleanDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: BooleanDef.php:28
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\ParamType\TEXT
const TEXT
A simple text string or another MessageValue, not otherwise formatted.
Definition: ParamType.php:13
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:38
Wikimedia\ParamValidator\TypeDef\BooleanDef\$TRUEVALS
static $TRUEVALS
Definition: BooleanDef.php:25
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:76
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
Wikimedia\Message\ParamType
The constants used to specify parameter types.
Definition: ParamType.php:11