<?php
namespace nulib\schema;

use nulib\schema\types\IType;
use nulib\schema\types\Registry;
use nulib\schema\types\tarray;
use nulib\schema\types\tbool;
use nulib\schema\types\tcontent;
use nulib\schema\types\tfloat;
use nulib\schema\types\tfunc;
use nulib\schema\types\tint;
use nulib\schema\types\tkey;
use nulib\schema\types\tmixed;
use nulib\schema\types\tpkey;
use nulib\schema\types\traw;
use nulib\schema\types\trawstring;
use nulib\schema\types\tstring;
use nulib\schema\types\ttext;
use nulib\ValueException;

/**
 * Class types: classe outil pour gérer le registre de types
 */
class types {
  /** @var Registry */
  private static $registry;

  static function registry(): Registry {
    if (self::$registry === null) {
      self::$registry = new Registry();
    }
    return self::$registry;
  }

  static function get(bool $nullable, $name, ?array $args=null, ?array $definition=null): IType {
    if ($name instanceof IType) return $name;
    if ($name !== null && !is_string($name)) {
      throw ValueException::invalid_type($name, "string");
    }
    return self::registry()->get($nullable, $name, $args, $definition);
  }

  static function rawstring(bool $nullable=true): trawstring { return self::get($nullable, "rawstring"); }
  static function string(bool $nullable=true): tstring { return self::get($nullable, "string"); }
  static function text(bool $nullable=true): ttext { return self::get($nullable, "text"); }
  static function bool(bool $nullable=true): tbool { return self::get($nullable, "bool"); }
  static function int(bool $nullable=true): tint { return self::get($nullable, "int"); }
  static function float(bool $nullable=true): tfloat { return self::get($nullable, "float"); }
  static function array(bool $nullable=true): tarray { return self::get($nullable, "array"); }
  static function callable(bool $nullable=true): tfunc { return self::get($nullable, "callable"); }
  static function raw(bool $nullable=true): traw { return self::get($nullable, "raw"); }
  static function mixed(bool $nullable=true): tmixed { return self::get($nullable, "mixed"); }
  static function key(bool $nullable=true): tkey { return self::get($nullable, "key"); }
  static function pkey(bool $nullable=true): tpkey { return self::get($nullable, "pkey"); }
  static function content(bool $nullable=true): tcontent { return self::get($nullable, "content"); }
}
