Debian Codegraph @

includes/libs/ParamValidator/TypeDef/PresenceBooleanDef.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\Message\MessageValue;
6 use Wikimedia\ParamValidator\ParamValidator;
7 use Wikimedia\ParamValidator\TypeDef;
8
9 /**
10 * Type definition for checkbox-like boolean types
11 *
12 * This boolean is considered true if the parameter is present in the request,
13 * regardless of value. The only way for it to be false is for the parameter to
14 * be omitted entirely.
15 *
16 * The result from validate() is a PHP boolean.
17 *
18 * @since 1.34
19 * @unstable
20 */
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
73 $info[ParamValidator::PARAM_TYPE] = MessageValue::new(
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 }