<?php
namespace nur\mapper\fsv;

use nur\A;
use nur\mapper\base\Tparams_command;

/**
 * Class FsvReader_command: classe de support pour construire une instance de
 * {@link FsvReader} pour une application recevant des commandes utilisateurs
 */
class FsvReader_command {
  use Tparams_command;

  const NAME = "infsv";
  const DEF = [self::NAME, "inv",
    "help" => "configurer le fichier en entrée au format FSV",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME." [file]",
      ["-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",
      ],
      ["-e", "--input-encoding", "args" => "value",
        "action" => [self::class, "set_input_encoding"],
      ],
      ["-s", "--skip-lines", "args" => "value", "type" => "int",
        "action" => [self::class, "set_skip_lines"],
      ],
      ["-o", "--param", "args" => "value",
        "action" => [self::class, "add_params"],
        "help" => "spécifier une option générique",
      ],
      ["args" => [["file"]],
        "action" => [self::class, "set_file"],
      ],
    ],
  ];

  const F_OPTION = ["-f", "--input", "args" => "file",
    "action" => [self::class, "set_file"],
  ];
  const E_OPTION = ["-e", "--input-encoding", "args" => "value",
    "action" => [self::class, "set_input_encoding"],
  ];

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

  static function create_command() {
    self::$command = new FsvReader();
  }

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

  static function set_file($file) {
    $file = A::last(A::with($file));
    self::get()->setInput($file);
  }

  static function set_input_encoding(string $input_encoding) {
    self::get()->setInputEncoding($input_encoding);
  }

  static function set_skip_lines(int $skip_lines) {
    self::get()->setSkipLines($skip_lines);
  }
}