<?php
namespace nur\data\types;

class FloatType extends AbstractSimpleType {
  const TITLE = "nombre à virgule";

  private static $instance;
  static final function to_zfloat($value) {
    $type = self::$instance;
    if ($type === null) self::$instance = $type = new self();
    return $type->with($value);
  }
  static final function to_float($value): float {
    return floatval(self::to_zfloat($value));
  }

  function getClass(): string {
    return "float";
  }

  function isInstance($value, bool $strict=false): bool {
    return $value === null || is_float($value) || (!$strict && is_int($value));
  }

  function parse(string &$input) {
    if (preg_match('/^[0-9]+(?:\.[0-9]*)?/', $input, $ms)) {
      $value = $ms[0];
      $length = strlen($value);
      $input = substr($input, $length);
      return $value;
    }
    return false;
  }

  protected function verifixCheckClass(&$value, $orig, array &$result=null): bool {
    # accepter float et int
    if (is_float($value)) $value = self::result_valid($result, $value, $orig);
    elseif (is_int($value)) $value = self::result_valid($result, floatval($value), $orig);
    else return false;
    return true;
  }

  function verifixConvert(&$value): bool {
    if ($value === null && $this->ppAllowNull) return true;
    $value = floatval($value);
    return true;
  }
}
