<?php
namespace nur\sery\schema\_scalar;

use nulib\cl;
use nur\sery\schema\ref\ref_analyze;
use nur\sery\schema\Result;
use nur\sery\schema\ValueException;

/**
 * Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur
 *
 * @property bool $present la valeur existe-t-elle?
 * @property bool $available si la valeur existe, est-elle disponible?
 * @property bool $null si la valeur existe, est-elle nulle?
 * @property bool $valid si la valeur existe, est-elle valide?
 * @property bool $normalized si la valeur est valide, est-elle normalisée?
 * @property string|null $orig valeur originale avant analyse avec parse()
 * @property string|null $message message si la valeur n'est pas valide
 */
class ScalarResult extends Result {
  const KEYS = ["present", "available", "null", "valid", "normalized", "orig", "message"];

  function isScalar(?ScalarResult &$scalar=null): bool { $scalar = $this; return true; }

  function getKeys(): array {
    return [null];
  }

  function getResult($key=null): Result {
    if ($key === null) return $this;
    else throw ValueException::invalid_key($key);
  }

  /** @var array  */
  protected $result;
  
  function reset(): void {
    $this->result = array_merge(
      array_fill_keys(static::KEYS, null), [
      "present" => false,
      "available" => false,
      "null" => false,
      "valid" => false,
      "normalized" => false,
    ]);
  }

  function __get(string $name) {
    return $this->result[$name];
  }

  function __set(string $name, $value): void {
    $this->result[$name] = $value;
  }

  protected static function replace_key(string &$message, ?string $key): void {
    if ($key) $message = str_replace("{key}", $key, $message);
    else $message = str_replace("{key}: ", "", $message);
  }

  function setMissing(ScalarSchema $schema): int {
    $this->present = false;
    $this->available = false;
    if (!$schema->required) {
      $this->null = false;
      $this->valid = true;
      $this->normalized = true;
      return ref_analyze::NORMALIZED;
    } else {
      $message = cl::get($schema->messages, "missing");
      self::replace_key($message, $schema->name);
      $this->message = $message;
      return ref_analyze::MISSING;
    }
  }

  function setUnavailable(ScalarSchema $schema): int {
    $this->present = true;
    $this->available = false;
    if (!$schema->required) {
      $this->null = false;
      $this->valid = true;
      $this->normalized = true;
      return ref_analyze::NORMALIZED;
    } else {
      $message = cl::get($schema->messages, "unavailable");
      self::replace_key($message, $schema->name);
      $this->message = $message;
      return ref_analyze::UNAVAILABLE;
    }
  }

  function setNull(ScalarSchema $schema): int {
    $this->present = true;
    $this->available = true;
    $this->null = true;
    if ($schema->nullable) {
      $this->valid = true;
      $this->normalized = true;
      return ref_analyze::NORMALIZED;
    } else {
      $message = cl::get($schema->messages, "null");
      self::replace_key($message, $schema->name);
      $this->message = $message;
      return ref_analyze::NULL;
    }
  }
}
