<?php
namespace nulib\schema\types;

use Exception;
use nulib\php\func;
use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\Result;
use nulib\schema\Schema;
use nulib\ValueException;

class tfunc extends _tsimple {
  const NAME = "func";

  const ALIASES = ["function", "callable"];

  function getClass(): string {
    return func::class;
  }

  function getNullValue() {
    return null;
  }

  function isValid($value, ?bool &$normalized=null): bool {
    $normalized = $value instanceof func;
    return func::check($value);
  }

  function parse(string $value) {
    try {
      return func::ensure($value);
    } catch (Exception $e) {
      throw new ValueException(null, null, 0, $e);
    }
  }

  /**
   * @var ScalarSchema $schema
   */
  function normalize(&$value, Result $result, Schema $schema): bool {
    if ($result->normalized) {
    } elseif ($value instanceof func) {
      $result->setNormalized();
    } elseif (is_callable($value)) {
      $value = func::with($value);
      return true;
    }
    return false;
  }

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