ParamValidator: Flag as unstable for 1.34
[lhc/web/wiklou.git] / includes / libs / ParamValidator / TypeDef / FloatDef.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\ParamValidator\TypeDef;
6 use Wikimedia\ParamValidator\ValidationException;
7
8 /**
9 * Type definition for a floating-point type
10 *
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
16 *
17 * Thus, for example, "12", "-.4", "6.022e23", or "+1.7e-10".
18 *
19 * The result from validate() is a PHP float.
20 *
21 * ValidationException codes:
22 * - 'badfloat': The value was invalid. No data.
23 * - 'notfinite': The value was in a valid format, but conversion resulted in
24 * infinity or NAN.
25 *
26 * @since 1.34
27 * @unstable
28 */
29 class FloatDef extends TypeDef {
30
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', [] );
35 }
36
37 $ret = (float)$value;
38 if ( !is_finite( $ret ) ) {
39 throw new ValidationException( $name, $value, $settings, 'notfinite', [] );
40 }
41
42 return $ret;
43 }
44
45 /**
46 * Attempt to fix locale weirdness
47 *
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.
50 *
51 * @param string $value Value to fix
52 * @return string
53 */
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] => '.',
62 ] );
63 }
64 return $value;
65 }
66
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 ) );
71 }
72
73 }