<?php
namespace nur\mapper\csv;

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

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

  const NAME = "outcsv";
  const DEF = [self::NAME, "outc", 
    "help" => "configurer le fichier en sortie au format CSV",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME." [file]",
      ["-t", "--output-encoding", "args" => "value",
        "action" => [self::class, "set_output_encoding"],
      ],
      ["-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 O_OPTION = ["-o", "--output", "args" => "file",
    "action" => [self::class, "set_file"],
  ];
  const T_OPTION = ["-t", "--output-encoding", "args" => "value",
    "action" => [self::class, "set_output_encoding"],
  ];

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

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

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

  static function set_output_encoding(string $output_encoding) {
    self::get()->setEncodingFilter($output_encoding);
  }

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