<?php
namespace nulib\schema\types;

use nulib\schema\input\Input;
use nur\prop;
use nur\str;

abstract class _tsimple implements IType {
  const NAME = null;

  const ALIASES = [];

  static function get_params_from_definition(?array $definition): ?array {
    return null;
  }

  /**
   * $nullable et $params doivent toujours être les derniers arguments du
   * constructeur
   */
  function __construct(bool $nullable, ?array $params=null) {
    $this->nullable = $nullable;
    $this->params = $params;
  }

  protected bool $nullable;

  protected ?array $params;

  function getName(): string {
    return static::NAME;
  }

  function getAliases(): array {
    return static::ALIASES;
  }

  function getPhpType(bool $allowNullable=true): ?string {
    $phpType = $this->getClass();
    if ($phpType === "mixed") return null;
    if ($this->nullable && $allowNullable) $phpType = "?$phpType";
    return $phpType;
  }

  function getMissingValue(?bool &$valid=null) {
    $valid = true;
    return false;
  }

  function getNbStates(?array &$states=null): int {
    return 0;
  }

  function isAvailable(Input $input, $valueKey): bool {
    return $input->isAvailable($valueKey) && $input->get($valueKey) !== false;
  }

  function isNull($value): bool {
    return $value === null || $value === "";
  }

  function extract(string $value): string {
    return $value;
  }

  #############################################################################

  function getGetterName(string $name): string {
    return prop::get_getter_name($name);
  }

  function getSetterName(string $name): string {
    return prop::get_setter_name($name);
  }

  function getDeleterName(string $name): string {
    return prop::get_deletter_name($name);
  }

  function getClassConstName(string $name): string {
    return strtoupper($name);
  }

  function getObjectPropertyName(string $name): string {
    return str::us2camel($name);
  }

  function getArrayKeyName(string $name): string {
    return $name;
  }
}
