<?php
namespace nur\mapper\json;

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

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

  const NAME = "outjson";
  const DEF = [self::NAME, "outj",
    "help" => "configurer le fichier en sortie au format JSON",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME." [file]",
      ["-p", "--pretty-print",
        "action" => [self::class, "set_pretty_print", true],
      ],
      ["-j", "--force-object",
        "action" => [self::class, "set_force_object", true],
      ],
      ["-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"],
  ];

  /** @var JsonWriter */
  private static $command;

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

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

  static function set_pretty_print(bool $pretty_print) {
    self::get()->setPrettyPrint($pretty_print);
  }
  static function set_force_object(bool $force_object) {
    self::get()->setForceObject($force_object);
  }
}