<?php
namespace nur\mapper\fsv;

use nur\A;
use nur\b\params\Tparametrable;
use nur\b\ValueException;
use nur\mapper\base\encoding_utils;
use nur\mapper\base\Mapper;

/**
 * Class Assoc2FsvMapper: mapper qui convertir un flux de tableaux associatifs
 * en flux de lignes au format FSV
 *
 * --autogen-properties-and-methods--
 * @method setFsvSchema($value)
 * @method string|null setInputEncoding(?string $value)
 * @method string|null setOutputEncoding(?string $value)
 */
class Assoc2FsvMapper extends Mapper {
  use Tparametrable;

  protected function FSV_SCHEMA(): ?array {
    return self::FSV_SCHEMA;
  } const FSV_SCHEMA = null;

  function __construct(?array $fsvSchema=null, ?iterable $source=null) {
    parent::__construct($source);
    if ($fsvSchema === null) $fsvSchema = $this->FSV_SCHEMA();
    $fsvSchema = new FsvSchema($fsvSchema);
    $this->setFsvSchema($fsvSchema);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "fsv_schema" => [null, null, "schéma des données en sortie"],
  ];

  static function _get_parametrable_params_schema(): array {
    return array_merge(self::PARAMETRABLE_PARAMS_SCHEMA
        , encoding_utils::PARAMETRABLE_PARAMS_SCHEMA);
  }

  /** @var FsvSchema */
  protected $fsvSchema;
  function pp_setFsvSchema($fsvSchema): void {
    if ($fsvSchema instanceof FsvSchema) $this->fsvSchema = $fsvSchema;
    elseif (is_array($fsvSchema)) $this->fsvSchema->setFsvSchema($fsvSchema);
    elseif ($fsvSchema !== null) throw ValueException::invalid_value($fsvSchema, "fsvSchema");
  }
  function pp_setInputEncoding(?string $inputEncoding): void {
    $this->fsvSchema->setDataEncoding($inputEncoding);
  }
  function pp_setOutputEncoding(?string $outputEncoding): void {
    $this->fsvSchema->setOutputEncoding($outputEncoding);
  }

  function getFsvSchema(): array {
    return $this->fsvSchema->getFsvSchema();
  }

  function getFsvColumns(): array {
    return $this->fsvSchema->getFsvColumns();
  }

  function _formatLine(array $row): string {
    $fsvSchema = $this->fsvSchema;
    if (!$fsvSchema->checkFsvSchema(false)) {
      $fsvSchema->setFsvSchema($this->getOvalue(fsv_defaults::OKEY_FSV_SCHEMA));
      $fsvSchema->checkFsvSchema();
    }
    return $this->fsvSchema->formatLine($row);
  }

  function mapper($item) {
    return $this->_formatLine(A::with($item));
  }

  #############################################################################
  const _AUTOGEN_CONSTS = [
    "" => [self::class, "_autogen_consts", true],
  ];
  const _AUTOGEN_LITERALS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '\\nur\\b\\params\\parametrable_utils::class',
    ],
    [self::class, 'self::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,
      self::class,
    ],
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_setters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      self::class,
    ],
  ];
  const _AUTO_GETTERS = /*autogen*/[
    'getFsvSchema' => 'fsv_schema',
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setFsvSchema' => 'fsv_schema',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
  ];
  #--autogen-dynamic--
}
