<?php
namespace nur\data\types;

use nur\b\date\Time;

class STimeType extends RegexpType {
  const TITLE = "heure du jour";

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

  const PATTERN = '/^(\d+)\s*(?:[hH:.,]\s*(?:(\d+)\s*(?:[:.,]\s*(\d+)\s*)?)?)?/';
  const PATTERN_NORMALIZED = Time::PATTERN;

  /** @return array un tableau de la forme [$H, $M, $S] */
  protected function extractParsedValue(array $ms) {
    $h = intval($ms[1]);
    $m = isset($ms[2])? intval($ms[2]): 0;
    $s = isset($ms[3])? intval($ms[3]): 0;
    return [$h, $m, $s];
  }

  function verifixConvert(&$value): bool {
    if ($value === null) return true;
    $value = (new Time($value))->format();
    return true;
  }
}
