MediaWiki  master
PresenceBooleanDef.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
21 class PresenceBooleanDef extends TypeDef {
22 
23  public function getValue( $name, array $settings, array $options ) {
24  return $this->callbacks->hasParam( $name, $options ) ? true : null;
25  }
26 
27  public function validate( $name, $value, array $settings, array $options ) {
28  return (bool)$value;
29  }
30 
31  public function normalizeSettings( array $settings ) {
32  // Cannot be multi-valued
33  $settings[ParamValidator::PARAM_ISMULTI] = false;
34 
35  // Default the default to false so ParamValidator::getValue() returns false (T244440)
36  $settings += [ ParamValidator::PARAM_DEFAULT => false ];
37 
38  return parent::normalizeSettings( $settings );
39  }
40 
41  public function checkSettings( string $name, $settings, array $options, array $ret ) : array {
42  $ret = parent::checkSettings( $name, $settings, $options, $ret );
43 
44  if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
45  !isset( $ret['issues'][ParamValidator::PARAM_ISMULTI] )
46  ) {
47  $ret['issues'][ParamValidator::PARAM_ISMULTI] =
48  'PARAM_ISMULTI cannot be used for presence-boolean-type parameters';
49  }
50 
51  if ( ( $settings[ParamValidator::PARAM_DEFAULT] ?? false ) !== false &&
52  !isset( $ret['issues'][ParamValidator::PARAM_DEFAULT] )
53  ) {
54  $ret['issues'][ParamValidator::PARAM_DEFAULT] =
55  'Default for presence-boolean-type parameters must be false or null';
56  }
57 
58  return $ret;
59  }
60 
61  public function getParamInfo( $name, array $settings, array $options ) {
62  $info = parent::getParamInfo( $name, $settings, $options );
63 
64  // No need to report the default of "false"
65  $info['default'] = null;
66 
67  return $info;
68  }
69 
70  public function getHelpInfo( $name, array $settings, array $options ) {
71  $info = parent::getHelpInfo( $name, $settings, $options );
72 
74  'paramvalidator-help-type-presenceboolean'
75  )->params( 1 );
76 
77  // No need to report the default of "false"
78  $info[ParamValidator::PARAM_DEFAULT] = null;
79 
80  return $info;
81  }
82 
83 }
true
return true
Definition: router.php:90
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.
Definition: PresenceBooleanDef.php:61
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:19
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.
Definition: PresenceBooleanDef.php:41
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: PresenceBooleanDef.php:27
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\normalizeSettings
normalizeSettings(array $settings)
Normalize a settings array Stable to override.
Definition: PresenceBooleanDef.php:31
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef
Type definition for checkbox-like boolean types.
Definition: PresenceBooleanDef.php:21
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\getValue
getValue( $name, array $settings, array $options)
Get the value from the request Stable to override.
Definition: PresenceBooleanDef.php:23
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: PresenceBooleanDef.php:70
Wikimedia\ParamValidator\ParamValidator\PARAM_DEFAULT
const PARAM_DEFAULT
(mixed) Default value of the parameter.
Definition: ParamValidator.php:68
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