MediaWiki  1.34.0
BooleanDef.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
22 class BooleanDef extends TypeDef {
23 
24  public static $TRUEVALS = [ 'true', 't', 'yes', 'y', 'on', '1' ];
25  public static $FALSEVALS = [ 'false', 'f', 'no', 'n', 'off', '0' ];
26 
27  public function validate( $name, $value, array $settings, array $options ) {
28  $value = strtolower( $value );
29  if ( in_array( $value, self::$TRUEVALS, true ) ) {
30  return true;
31  }
32  if ( $value === '' || in_array( $value, self::$FALSEVALS, true ) ) {
33  return false;
34  }
35 
36  throw new ValidationException( $name, $value, $settings, 'badbool', [
37  'truevals' => self::$TRUEVALS,
38  'falsevals' => array_merge( self::$FALSEVALS, [ 'the empty string' ] ),
39  ] );
40  }
41 
42  public function stringifyValue( $name, $value, array $settings, array $options ) {
43  return $value ? self::$TRUEVALS[0] : self::$FALSEVALS[0];
44  }
45 
46 }
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:15
Wikimedia\ParamValidator\TypeDef\BooleanDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: BooleanDef.php:42
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:15
Wikimedia\ParamValidator\TypeDef\BooleanDef
Type definition for boolean types.
Definition: BooleanDef.php:22
Wikimedia\ParamValidator\TypeDef\BooleanDef\$FALSEVALS
static $FALSEVALS
Definition: BooleanDef.php:25
Wikimedia\ParamValidator\TypeDef\BooleanDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: BooleanDef.php:27
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\ParamValidator\TypeDef\BooleanDef\$TRUEVALS
static $TRUEVALS
Definition: BooleanDef.php:24