MediaWiki REL1_37
FloatDef.php
Go to the documentation of this file.
1<?php
2
4
7
29class 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}
Value object representing a message for i18n.
Service for formatting and validating API parameters.
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
const PARAM_TYPE
(string|array) Type of the parameter.
Type definition for a floating-point type.
Definition FloatDef.php:29
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
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition FloatDef.php:33
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition FloatDef.php:69
fixLocaleWeirdness( $value)
Attempt to fix locale weirdness.
Definition FloatDef.php:56
Type definition base class for numeric types.
checkRange( $value, $name, $origValue, array $settings, array $options)
Check the range of a value.
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:49