<?php
namespace nur\mapper\csv;

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

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

  const NAME = "incsv";
  const DEF = [self::NAME, "inc", 
    "help" => "configurer le fichier en entrée au format CSV",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME." [file]",
      ["-e", "--input-encoding", "args" => "value",
        "action" => [self::class, "set_input_encoding"],
      ],
      ["-s", "--skip-lines", "args" => "value", "type" => "int",
        "action" => [self::class, "set_skip_lines"],
      ],
      ["-f", "--csv-flavour", "args" => "value",
        "action" => [self::class, "set_csv_flavour"],
      ],
      ["-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 CsvReader */
  protected static $command;

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

  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()->setEncodingFilter($input_encoding);
  }

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

  static function set_csv_flavour(string $csv_flavour) {
    self::get()->setCsvFlavour($csv_flavour);
  }
}
