<?php
namespace nur\sery\wip\schema\types;

use nur\sery\wip\schema\_scalar\ScalarResult;
use nur\sery\wip\schema\_scalar\ScalarSchema;
use nur\sery\wip\schema\Result;
use nur\sery\wip\schema\Schema;

class tstring extends _tsimple {
  static function ensure_string(&$string): void {
    if (!is_string($string)) $string = strval($string);
  }

  static function ensure_nstring(&$string): void {
    if ($string !== null) self::ensure_string($string);
  }

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

  function isValid($value, ?bool &$normalized=null): bool {
    $normalized = is_string($value);
    return is_scalar($value);
  }
  
  /**
   * @var ScalarResult $result
   * @var ScalarSchema $schema
   */
  function verifix(&$value, Result &$result, Schema $schema): bool {
    if (is_string($value)) {
      $result->setNormalized();
      return false;
    } elseif (is_scalar($value)) {
      $value = strval($value);
      $result->setValid();
      return true;
    } else {
      $result->setInvalid($value, $schema);
      return false;
    }
  }

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