MediaWiki  master
FloatDef.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
29 class FloatDef extends NumericDef {
30 
31  protected $valueType = 'double';
32 
33  public function validate( $name, $value, array $settings, array $options ) {
34  // Use a regex so as to avoid any potential oddness PHP's default conversion might allow.
35  if ( !preg_match( '/^[+-]?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?$/D', $value ) ) {
36  $this->failure( 'badfloat', $name, $value, $settings, $options );
37  }
38 
39  $ret = (float)$value;
40  if ( !is_finite( $ret ) ) {
41  $this->failure( 'badfloat-notfinite', $name, $value, $settings, $options );
42  }
43 
44  return $this->checkRange( $ret, $name, $value, $settings, $options );
45  }
46 
47  public function stringifyValue( $name, $value, array $settings, array $options ) {
48  // Ensure sufficient precision for round-tripping. PHP_FLOAT_DIG was added in PHP 7.2.
49  $digits = defined( 'PHP_FLOAT_DIG' ) ? PHP_FLOAT_DIG : 15;
50  return sprintf( "%.{$digits}g", $value );
51  }
52 
53  public function getHelpInfo( $name, array $settings, array $options ) {
54  $info = parent::getHelpInfo( $name, $settings, $options );
55 
56  $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-float' )
57  ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 );
58 
59  return $info;
60  }
61 
62 }
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef\FloatDef\$valueType
$valueType
Definition: FloatDef.php:31
Wikimedia\ParamValidator\TypeDef\FloatDef
Type definition for a floating-point type.
Definition: FloatDef.php:29
Wikimedia\ParamValidator\TypeDef\NumericDef
Type definition base class for numeric types.
Definition: NumericDef.php:26
Wikimedia\ParamValidator\TypeDef\FloatDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: FloatDef.php:47
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\TypeDef\FloatDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: FloatDef.php:33
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
Wikimedia\ParamValidator\TypeDef\FloatDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition: FloatDef.php:53
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:76
Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange
checkRange( $value, $name, $origValue, array $settings, array $options)
Check the range of a value.
Definition: NumericDef.php:64
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42