3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\TypeDef
;
6 use Wikimedia\ParamValidator\ValidationException
;
9 * Type definition for a floating-point type
11 * A valid representation consists of:
12 * - an optional sign (`+` or `-`)
13 * - a decimal number, using `.` as the decimal separator and no grouping
14 * - an optional E-notation suffix: the letter 'e' or 'E', an optional
15 * sign, and an integer
17 * Thus, for example, "12", "-.4", "6.022e23", or "+1.7e-10".
19 * The result from validate() is a PHP float.
21 * ValidationException codes:
22 * - 'badfloat': The value was invalid. No data.
23 * - 'notfinite': The value was in a valid format, but conversion resulted in
29 class FloatDef
extends TypeDef
{
31 public function validate( $name, $value, array $settings, array $options ) {
32 // Use a regex so as to avoid any potential oddness PHP's default conversion might allow.
33 if ( !preg_match( '/^[+-]?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?$/D', $value ) ) {
34 throw new ValidationException( $name, $value, $settings, 'badfloat', [] );
38 if ( !is_finite( $ret ) ) {
39 throw new ValidationException( $name, $value, $settings, 'notfinite', [] );
46 * Attempt to fix locale weirdness
48 * We don't have any usable number formatting function that's not locale-aware,
49 * and `setlocale()` isn't safe in multithreaded environments. Sigh.
51 * @param string $value Value to fix
54 private function fixLocaleWeirdness( $value ) {
55 $localeData = localeconv();
56 if ( $localeData['decimal_point'] !== '.' ) {
57 $value = strtr( $value, [
58 $localeData['decimal_point'] => '.',
59 // PHP's number formatting currently uses only the first byte from 'decimal_point'.
60 // See upstream bug https://bugs.php.net/bug.php?id=78113
61 $localeData['decimal_point'][0] => '.',
67 public function stringifyValue( $name, $value, array $settings, array $options ) {
68 // Ensure sufficient precision for round-tripping. PHP_FLOAT_DIG was added in PHP 7.2.
69 $digits = defined( 'PHP_FLOAT_DIG' ) ? PHP_FLOAT_DIG
: 15;
70 return $this->fixLocaleWeirdness( sprintf( "%.{$digits}g", $value ) );