<?php
namespace nur\mapper\item;

use nur\mapper\base\mappers_command;
use nur\mapper\base\Tparams_command;

/**
 * Class SchemaMapper_command: classe de support pour construire une instance de
 * mapper pour une application recevant des commandes utilisateurs
 */
class SchemaMapper_command {
  use Tparams_command;

  const NAME = "schema";
  const DEF = [self::NAME,
    "help" => "appliquer un schéma",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME,
      ["-r", "--run-code", "args" => "value",
        "action" => [self::class, "set_schema_from_code"],
        "help" => "exécuter le code et utiliser sa valeur de retour comme schéma",
      ],
      ["-f", "--run-file", "args" => "file",
        "action" => [self::class, "set_schema_from_file"],
        "help" => "exécuter le fichier et utiliser sa valeur de retour comme schéma",
      ],
      ["-o", "--param", "args" => "value",
        "action" => [self::class, "add_params"],
        "help" => "spécifier une option générique",
      ],
    ],
  ];

  /** @var SchemaMapper */
  protected static $command;

  static function create_command() {
    self::$command = mappers_command::add(new SchemaMapper());
  }

  static function set_schema_from_code(string $code) {
    $schema = eval($code);
    self::get()->setFsvSchema($schema);
  }

  static function set_schema_from_file(string $file) {
    $schema = require($file);
    self::get()->setFsvSchema($schema);
  }
}
