<?php
namespace nulib\schema\types;

use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\Result;
use nulib\schema\Schema;
use nulib\ValueException;

class tgeneric extends _tsimple {
  function __construct(string $class, bool $nullable, ?array $params=null) {
    $this->class = $class;
    parent::__construct($nullable, $params);
  }

  protected string $class;

  function getClass(): string {
    return $this->class;
  }

  function getNullValue() {
    return null;
  }

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

  function isValid($value, ?bool &$normalized=null): bool {
    $normalized = $value instanceof $this->class;
    return $normalized;
  }

  function parse(string $value) {
    throw ValueException::invalid_type($value, $this->class);
  }

  /**
   * @var ScalarSchema $schema
   */
  function normalize(&$value, Result $result, Schema $schema): bool {
    if (!$result->normalized) $result->setNormalized();
    return false;
  }

  function format($value, $format=null): string {
    return strval($value);
  }
}
