<?php
namespace nur\mapper\json;

use nur\b\io\IReader;
use nur\b\params\Tparametrable;
use nur\mapper\base\Producer;
use nur\reader;
use Symfony\Component\Yaml\Yaml;

/**
 * Class YamlReader
 *
 * --autogen-properties-and-methods--
 * @method setInput($value)
 */
class YamlReader extends Producer {
  use Tparametrable;

  function __construct($input=null) {
    parent::__construct();
    $this->pp_setInput($input);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "input" => [null, null, "fichier en entrée"],
  ];

  protected $ppInput;

  function pp_setInput($input): void {
    if ($input instanceof IReader) $this->reader = $input;
    else $this->ppInput = $input;
  }

  /** @var IReader */
  protected $reader;

  protected function setup(): void {
    if ($this->reader === null) {
      $this->reader = reader::with($this->ppInput);
    }
  }

  function producer() {
    $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE;
    yield Yaml::parse($this->reader->readContents(), $flags);
  }

  protected function teardown(): void {
    if ($this->reader !== null) {
      $this->reader->close();
      $this->reader = null;
    }
  }

  #############################################################################
  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*/[
    'getInput' => 'input',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setInput' => 'input',
  ];
  #--autogen-dynamic--
}
