MediaWiki master
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 if ( is_bool( $value ) ) {
30 return $value;
31 } elseif ( $options[ self::OPT_ENFORCE_JSON_TYPES ] ?? false ) {
32 $this->fatal(
33 $this->failureMessage( 'badbool-type' )
34 ->params( gettype( $value ) ),
35 $name, $value, $settings, $options
36 );
37 }
38
39 $value = strtolower( $value );
40 if ( in_array( $value, self::$TRUEVALS, true ) ) {
41 return true;
42 }
43 if ( $value === '' || in_array( $value, self::$FALSEVALS, true ) ) {
44 return false;
45 }
46
47 $this->fatal(
48 $this->failureMessage( 'badbool' )
49 ->textListParams( array_map( [ $this, 'quoteVal' ], self::$TRUEVALS ) )
50 ->numParams( count( self::$TRUEVALS ) )
51 ->textListParams( array_merge(
52 array_map( [ $this, 'quoteVal' ], self::$FALSEVALS ),
53 [ MessageValue::new( 'paramvalidator-emptystring' ) ]
54 ) )
55 ->numParams( count( self::$FALSEVALS ) + 1 ),
56 $name, $value, $settings, $options
57 );
58 }
59
60 private function quoteVal( $v ) {
61 return new ScalarParam( ParamType::TEXT, "\"$v\"" );
62 }
63
64 public function stringifyValue( $name, $value, array $settings, array $options ) {
65 return $value ? self::$TRUEVALS[0] : self::$FALSEVALS[0];
66 }
67
68 public function getHelpInfo( $name, array $settings, array $options ) {
69 $info = parent::getHelpInfo( $name, $settings, $options );
70
71 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-boolean' )
72 ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 );
73
74 return $info;
75 }
76
77}
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
fatal( $failure, $name, $value, array $settings, array $options)
Throw a ValidationException.
Definition TypeDef.php:99
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition TypeDef.php:156