<?php
namespace nulib\schema\types;

use nulib\php\content\c;
use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\Result;
use nulib\schema\Schema;

abstract class tcontent extends _tunion {
  const NAME = "content";

  function getClass(): string {
    return "string|array";
  }

  function getNullValue() {
    return $this->nullable? null: [];
  }

  function isValid($value, ?bool &$normalized=null): bool {
    $normalized = is_string($value) || is_array($value);
    return $normalized || is_scalar($value);
  }

  function parse(string $value) {
    return $value;
  }

  /**
   * @var ScalarSchema $schema
   */
  function normalize(&$value, Result $result, Schema $schema): bool {
    if ($result->normalized) {
    } elseif (is_string($value) || is_array($value)) {
      $result->setNormalized();
    } elseif (is_scalar($value)) {
      $value = strval($value);
      return true;
    }
    return false;
  }

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