| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 33 |
$settings[ParamValidator::PARAM_ISMULTI] = false; |
| 34 |
|
| 35 |
|
| 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 |
|
| 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 |
|
| 78 |
$info[ParamValidator::PARAM_DEFAULT] = null; |
| 79 |
|
| 80 |
return $info; |
| 81 |
} |
| 82 |
|
| 83 |
} |