<?php
namespace nur\mapper\fsv;

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

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

  const NAME = "assoc2fsv";
  const DEF = [self::NAME,
    "help" => "convertir en FSV",
    "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 Assoc2FsvMapper */
  protected static $command;

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

  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);
  }
}