<?php
namespace nur\mapper\line;

use nur\b\params\Tparametrable;
use nur\mapper\base\encoding_utils;
use nur\mapper\base\Mapper;
use nur\mapper\base\Tencoding;

/**
 * Class IconvMapper
 *
 * --autogen-properties-and-methods--
 * @method string|null setInputEncoding(?string $value)
 * @method string|null setOutputEncoding(?string $value)
 */
class IconvMapper extends Mapper {
  use Tparametrable, Tencoding;

  protected function INPUT_ENCODING(): ?string {
    return static::INPUT_ENCODING;
  } const INPUT_ENCODING = null;

  protected function OUTPUT_ENCODING(): ?string {
    return static::OUTPUT_ENCODING;
  } const OUTPUT_ENCODING = "utf-8";

  function __construct(?string $inputEncoding=null, ?iterable $source=null) {
    parent::__construct($source);
    $this->pp_setInputEncoding($inputEncoding);
    $this->pp_setOutputEncoding(null);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = encoding_utils::PARAMETRABLE_PARAMS_SCHEMA;

  protected $ppInputEncoding;

  function pp_setInputEncoding(?string $inputEncoding): self {
    if ($inputEncoding === null) $inputEncoding = $this->INPUT_ENCODING();
    $this->ppInputEncoding = $inputEncoding;
    return $this;
  }

  protected $ppOutputEncoding;

  function pp_setOutputEncoding(?string $outputEncoding): self {
    if ($outputEncoding === null) $outputEncoding = $this->OUTPUT_ENCODING();
    $this->ppOutputEncoding = $outputEncoding;
    return $this;
  }

  function _cook(&$values, string $fromEncoding, string $toEncoding): void {
    if (is_array($values)) {
      foreach ($values as &$value) {
        $this->_cook($value, $fromEncoding, $toEncoding);
      }; unset($value);
    } else {
      $values = iconv($fromEncoding, $toEncoding, strval($values));
    }
  }

  function mapper($item) {
    $inputEncoding = $this->ppInputEncoding;
    $outputEncoding = $this->ppOutputEncoding;
    if ($inputEncoding !== null && $outputEncoding !== null) {
      $this->_cook($item, $inputEncoding, $outputEncoding);
    }
    return $item;
  }

  #############################################################################
  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*/[
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
  ];
  #--autogen-dynamic--
}
