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 
56  private function fixLocaleWeirdness( $value ) {
57  $localeData = localeconv();
58  if ( $localeData['decimal_point'] !== '.' ) {
59  $value = strtr( $value, [
60  $localeData['decimal_point'] => '.',
61  // PHP's number formatting currently uses only the first byte from 'decimal_point'.
62  // See upstream bug https://bugs.php.net/bug.php?id=78113
63  $localeData['decimal_point'][0] => '.',
64  ] );
65  }
66  return $value;
67  }
68 
69  public function stringifyValue( $name, $value, array $settings, array $options ) {
70  // Ensure sufficient precision for round-tripping. PHP_FLOAT_DIG was added in PHP 7.2.
71  $digits = defined( 'PHP_FLOAT_DIG' ) ? PHP_FLOAT_DIG : 15;
72  return $this->fixLocaleWeirdness( sprintf( "%.{$digits}g", $value ) );
73  }
74 
75  public function getHelpInfo( $name, array $settings, array $options ) {
76  $info = parent::getHelpInfo( $name, $settings, $options );
77 
78  $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-float' )
79  ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 );
80 
81  return $info;
82  }
83 
84 }
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49
Wikimedia\ParamValidator\TypeDef\FloatDef\fixLocaleWeirdness
fixLocaleWeirdness( $value)
Attempt to fix locale weirdness.
Definition: FloatDef.php:56
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:69
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:75
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