172 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			172 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nur\mapper\fsv;
 | |
| 
 | |
| use nur\b\io\EOFException;
 | |
| use nur\b\io\IReader;
 | |
| use nur\b\io\Tfilter;
 | |
| use nur\b\params\Tparametrable;
 | |
| use nur\mapper\base\encoding_utils;
 | |
| use nur\mapper\base\Producer;
 | |
| use nur\reader;
 | |
| use nur\str;
 | |
| 
 | |
| /**
 | |
|  * Class FsvReader: produit un flux de données à partir d'une source au format
 | |
|  * FSV.
 | |
|  *
 | |
|  * --autogen-properties-and-methods--
 | |
|  * @method setInput($value)
 | |
|  * @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 FsvReader extends Producer {
 | |
|   use Tparametrable, Tfilter;
 | |
| 
 | |
|   function __construct($input=null, ?array $fsvSchema=null) {
 | |
|     parent::__construct();
 | |
|     $this->fsv2assoc = new Fsv2AssocMapper();
 | |
|     $this->setFsvSchema($fsvSchema);
 | |
|     $this->setInput($input);
 | |
|   }
 | |
| 
 | |
|   const PARAMETRABLE_PARAMS_SCHEMA = [
 | |
|     "input" => [null, null, "fichier en entrée"],
 | |
|   ];
 | |
| 
 | |
|   static function _get_parametrable_params_schema(): array {
 | |
|     return array_merge(self::PARAMETRABLE_PARAMS_SCHEMA
 | |
|       , Fsv2AssocMapper::PARAMETRABLE_PARAMS_SCHEMA
 | |
|       , encoding_utils::PARAMETRABLE_PARAMS_SCHEMA);
 | |
|   }
 | |
| 
 | |
|   protected $ppInput;
 | |
| 
 | |
|   function pp_setInput($input): void {
 | |
|     if ($input instanceof IReader) $this->reader = $input;
 | |
|     else $this->ppInput = $input;
 | |
|   }
 | |
| 
 | |
|   /** @var Fsv2AssocMapper */
 | |
|   protected $fsv2assoc;
 | |
|   function pp_setFsvSchema($fsvSchema): void {
 | |
|     $this->fsv2assoc->setFsvSchema($fsvSchema);
 | |
|   }
 | |
|   function pp_setMapEmpty($emptyIs=null, bool $mapEmpty=true): void {
 | |
|     $this->fsv2assoc->setMapEmpty($emptyIs, $mapEmpty);
 | |
|   }
 | |
|   function pp_setOutputSeq(bool $outputSeq=true): void {
 | |
|     $this->fsv2assoc->setOutputSeq($outputSeq);
 | |
|   }
 | |
|   function pp_setSkipLines(int $skipLines): void {
 | |
|     $this->fsv2assoc->setSkipLines($skipLines);
 | |
|   }
 | |
|   function pp_setInputEncoding(?string $inputEncoding): void {
 | |
|     $this->fsv2assoc->setInputEncoding($inputEncoding);
 | |
|   }
 | |
|   function pp_setOutputEncoding(?string $outputEncoding): void {
 | |
|     $this->fsv2assoc->setOutputEncoding($outputEncoding);
 | |
|   }
 | |
| 
 | |
|   /** @var IReader */
 | |
|   protected $reader;
 | |
| 
 | |
|   protected function setup(): void {
 | |
|     if ($this->reader === null) {
 | |
|       $this->reader = reader::with($this->ppInput);
 | |
|       $this->_rwAppendFilters($this->reader);
 | |
|     }
 | |
|     if ($this->sharedOobdManager !== null) {
 | |
|       $this->fsv2assoc->setSharedOobdManager($this->sharedOobdManager);
 | |
|     }
 | |
|     $this->fsv2assoc->ensureSetup();
 | |
|   }
 | |
| 
 | |
|   protected function teardown(): void {
 | |
|     $this->fsv2assoc->close();
 | |
|     if ($this->reader !== null) {
 | |
|       $this->reader->close();
 | |
|       $this->reader = null;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   function producer() {
 | |
|     $parser = $this->fsv2assoc;
 | |
|     $reader = $this->reader;
 | |
|     $resource = $reader->getResource();
 | |
|     if ($resource !== null) {
 | |
|       while (!$parser->_parseLine()) {
 | |
|         if (fgets($resource) === false) break;
 | |
|       }
 | |
|       if ($parser->_outputKeys()) yield $parser->_getKeys();
 | |
|       while (($line = fgets($resource)) !== false) {
 | |
|         $line = str::strip_nl($line);
 | |
|         yield $parser->_parseColumns($line);
 | |
|       }
 | |
|     } else {
 | |
|       while (true) {
 | |
|         try {
 | |
|           $line = $reader->readLine();
 | |
|         } catch (EOFException $e) {
 | |
|           break;
 | |
|         }
 | |
|         if (!$parser->_parseLine()) continue;
 | |
|         if ($parser->_outputKeys()) yield $parser->_getKeys();
 | |
|         yield $parser->_parseColumns($line);
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   #############################################################################
 | |
|   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*/[
 | |
|     'getInput' => 'input',
 | |
|     'getFsvSchema' => 'fsv_schema',
 | |
|     'getMapEmpty' => 'map_empty',
 | |
|     'isOutputSeq' => 'output_seq',
 | |
|     'getSkipLines' => 'skip_lines',
 | |
|     'getInputEncoding' => 'input_encoding',
 | |
|     'getOutputEncoding' => 'output_encoding',
 | |
|   ];
 | |
|   const _AUTO_SETTERS = /*autogen*/[
 | |
|     'setInput' => 'input',
 | |
|     'setFsvSchema' => 'fsv_schema',
 | |
|     'setMapEmpty' => 'map_empty',
 | |
|     'setOutputSeq' => 'output_seq',
 | |
|     'setSkipLines' => 'skip_lines',
 | |
|     'setInputEncoding' => 'input_encoding',
 | |
|     'setOutputEncoding' => 'output_encoding',
 | |
|   ];
 | |
|   #--autogen-dynamic--
 | |
| }
 |