<?php
namespace nur\mapper\json;

use nur\b\params\Tparametrable;
use nur\mapper\base\Consumer;
use nur\mapper\base\Ttmpwriter;

/**
 * Class JsonWriter
 *
 * --autogen-properties-and-methods--
 * @method bool isPrettyPrint()
 * @method bool isForceObject()
 * @method setOutput($value)
 * @method bool setPrettyPrint(bool $value)
 * @method bool setForceObject(bool $value)
 */
class JsonWriter extends Consumer {
  use Tparametrable, Ttmpwriter;

  function __construct($output=null) {
    parent::__construct();
    $this->pp_setOutput($output);
  }

  protected $ppPrettyPrint = false;

  protected $ppForceObject = false;

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "output" => [null, null, "fichier en sortie"],
    "pretty_print" => ["bool", true, "type d'affichage"],
    "force_object" => ["bool", true, "forcer la sortie en objet"],
  ];

  protected function setup(): void {
    parent::setup();
    $this->setupWriter(false);
  }

  function cook($item) {
    $options = JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE;
    if ($this->ppPrettyPrint) $options += JSON_PRETTY_PRINT;
    if ($this->ppForceObject) $options += JSON_FORCE_OBJECT;
    $this->getWriter()->wnl(json_encode($item, $options));
  }

  protected function teardown(): void {
    $this->teardownWriter(false);
  }

  #############################################################################
  const _AUTOGEN_CONSTS = [
    "" => [self::class, "_autogen_consts"],
  ];
  const _AUTOGEN_LITERALS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '\\nur\\b\\params\\parametrable_utils::class',
    ],
    [
      self::PARAMETRABLE_PARAMS_SCHEMA,
      'self::PARAMETRABLE_PARAMS_SCHEMA',
    ],
  ];
  const _AUTOGEN_METHODS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_getters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      null,
    ],
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_setters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      null,
    ],
  ];
  const _AUTO_GETTERS = /*autogen*/[
    'getOutput' => 'output',
    'isPrettyPrint' => 'pretty_print',
    'isForceObject' => 'force_object',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setOutput' => 'output',
    'setPrettyPrint' => 'pretty_print',
    'setForceObject' => 'force_object',
  ];
  #--autogen-dynamic--
}
