nur-sery/nur_src/mapper/fsv/Fsv2AssocMapper.php

165 lines
4.9 KiB
PHP

<?php
namespace nur\mapper\fsv;
use nur\b\params\Tparametrable;
use nur\b\ValueException;
use nur\mapper\base\encoding_utils;
use nur\mapper\base\Mapper;
/**
* Class Fsv2AssocMapper: mapper qui analyse un flux de lignes au format FSV et
* qui produit un flux de tableaux associatifs
*
* --autogen-properties-and-methods--
* @method int getSkipLines()
* @method setFsvSchema($value)
* @method string|null setMapEmpty(?string $value)
* @method bool setOutputSeq(bool $value)
* @method int setSkipLines(int $value)
* @method string|null setInputEncoding(?string $value)
* @method string|null setOutputEncoding(?string $value)
*/
class Fsv2AssocMapper extends Mapper {
use Tparametrable;
protected function FSV_SCHEMA(): ?array {
return self::FSV_SCHEMA;
} const FSV_SCHEMA = null;
const MAP_EOF = true;
function __construct(?array $fsvSchema=null, ?iterable $source=null) {
parent::__construct($source);
if ($fsvSchema === null) $fsvSchema = $this->FSV_SCHEMA();
$fsvSchema = new FsvSchema($fsvSchema);
$this->pp_setFsvSchema($fsvSchema);
}
const PARAMETRABLE_PARAMS_SCHEMA = [
"fsv_schema" => [null, null, "schéma des données en entrée"],
"map_empty" => ["?string", null, "valeur adoptée pour les chaines vides"],
"output_seq" => ["bool", null, "faut-il générer en-têtes et données en tableaux séquentiels?"],
"skip_lines" => ["int", null, "nombre de lignes à sauter en entrée"],
];
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->setInputEncoding($inputEncoding);
}
function pp_setOutputEncoding(?string $outputEncoding): void {
$this->fsvSchema->setDataEncoding($outputEncoding);
}
function pp_setMapEmpty($mapEmpty=null): void {
$this->fsvSchema->setMapEmpty($mapEmpty);
}
function pp_setOutputSeq(bool $outputSeq=true): void {
$this->fsvSchema->setOutputSeq($outputSeq);
}
function getFsvSchema(): array {
return $this->fsvSchema->getFsvSchema();
}
function getFsvColumns(): array {
return $this->fsvSchema->getFsvColumns();
}
/** @var int */
protected $ppSkipLines = 0;
protected function setup(): void {
parent::setup();
$fsvSchema = $this->fsvSchema;
$fsvSchema->checkFsvSchema();
$this->setOvalue(fsv_defaults::OKEY_FSV_SCHEMA, $fsvSchema->getFsvSchema());
$this->setOvalue(fsv_defaults::OKEY_FSV_COLUMNS, $fsvSchema->getFsvColumns());
}
function _parseLine(): bool {
if ($this->ppSkipLines > 0) {
$this->ppSkipLines--;
return false;
}
return true;
}
function _outputKeys(bool $reset=true): bool {
return $this->fsvSchema->_outputKeys($reset);
}
function _getKeys(): array {
return $this->fsvSchema->_getKeys();
}
function _parseColumns(string $line): array {
return $this->fsvSchema->parseRow($line);
}
function mapper($item) {
if (!$this->_parseLine()) return $this->mapToNone();
$arrays = [];
if ($this->_outputKeys()) $arrays[] = $this->_getKeys();
if (!$this->eof) $arrays[] = $this->_parseColumns($item);
return $this->mapTo($arrays);
}
#############################################################################
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',
'getMapEmpty' => 'map_empty',
'isOutputSeq' => 'output_seq',
'getSkipLines' => 'skip_lines',
'getInputEncoding' => 'input_encoding',
'getOutputEncoding' => 'output_encoding',
];
const _AUTO_SETTERS = /*autogen*/[
'setFsvSchema' => 'fsv_schema',
'setMapEmpty' => 'map_empty',
'setOutputSeq' => 'output_seq',
'setSkipLines' => 'skip_lines',
'setInputEncoding' => 'input_encoding',
'setOutputEncoding' => 'output_encoding',
];
#--autogen-dynamic--
}