MediaWiki master
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
47 public function stringifyValue( $name, $value, array $settings, array $options ) {
48 // Ensure sufficient precision for round-tripping
49 $digits = PHP_FLOAT_DIG;
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}
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:53
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:47
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