<?php
namespace nur\mapper\item;

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

/**
 * Class AttributeFilterMapper: sélection de certains attributs d'un flux de
 * tableaux associatifs
 *
 * --autogen-properties-and-methods--
 * @method array|null setFields(?array $value)
 * @method array|null setAddFields(?array $value)
 * @method array|null setRemoveFields(?array $value)
 */
class AttributeFilterMapper extends Mapper {
  use Tparametrable;

  function __construct(?array $fields=null, ?iterable $source=null) {
    parent::__construct($source);
    $this->pp_setFields($fields);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "fields" => ["?array", null, "liste des champs à sélectionner"],
    "add_fields" => ["?array", null, "liste des champs à ajouter"],
    "remove_fields" => ["?array", null, "liste des champs à supprimer"],
  ];

  /** @var array */
  protected $ppFields;

  function pp_setFields(?array $fields): void {
    if ($fields !== null) $fields = A::flattened($fields);
    $this->ppFields = $fields;
  }

  /** @var array */
  protected $ppAddFields;

  function pp_setAddFields(?array $addFields): void {
    if ($addFields !== null) $addFields = A::flattened($addFields);
    $this->ppAddFields = $addFields;
  }

  /** @var array */
  protected $ppRemoveFields;

  function pp_setRemoveFields(?array $removeFields): void {
    if ($removeFields !== null) $removeFields = A::flattened($removeFields);
    $this->ppRemoveFields = $removeFields;
  }

  function mapper($item) {
    $item = A::with($item);
    $fields = $this->ppFields;
    $addFields = $this->ppAddFields;
    $removeFields = $this->ppRemoveFields;
    if ($fields === null && ($addFields !== null || $removeFields !== null)) {
      $fields = array_keys($item);
    }
    if ($addFields !== null) A::merge($fields, $addFields);
    $item = A::select_default($item, $fields);
    if ($removeFields !== null) {
      foreach ($removeFields as $field) {
        unset($item[$field]);
      }
    }
    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*/[
    'getFields' => 'fields',
    'getAddFields' => 'add_fields',
    'getRemoveFields' => 'remove_fields',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setFields' => 'fields',
    'setAddFields' => 'add_fields',
    'setRemoveFields' => 'remove_fields',
  ];
  #--autogen-dynamic--
}
