113 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nur\mapper\json;
 | |
| 
 | |
| use nur\b\io\EOFException;
 | |
| use nur\b\io\IReader;
 | |
| use nur\b\params\Tparametrable;
 | |
| use nur\mapper\base\Producer;
 | |
| use nur\reader;
 | |
| 
 | |
| /**
 | |
|  * Class JsonReader
 | |
|  *
 | |
|  * --autogen-properties-and-methods--
 | |
|  * @method bool isParseWhole()
 | |
|  * @method setInput($value)
 | |
|  * @method bool setParseWhole(bool $value)
 | |
|  */
 | |
| class JsonReader extends Producer {
 | |
|   use Tparametrable;
 | |
| 
 | |
|   function __construct($input=null) {
 | |
|     parent::__construct();
 | |
|     $this->pp_setInput($input);
 | |
|   }
 | |
| 
 | |
|   const PARAMETRABLE_PARAMS_SCHEMA = [
 | |
|     "input" => [null, null, "fichier en entrée"],
 | |
|     "parse_whole" => ["bool", false, "faut-il analyser un objet par ligne"],
 | |
|   ];
 | |
| 
 | |
|   protected $ppInput;
 | |
| 
 | |
|   function pp_setInput($input): void {
 | |
|     if ($input instanceof IReader) $this->reader = $input;
 | |
|     else $this->ppInput = $input;
 | |
|   }
 | |
| 
 | |
|   /** @var bool */
 | |
|   protected $ppParseWhole = false;
 | |
| 
 | |
|   /** @var IReader */
 | |
|   protected $reader;
 | |
| 
 | |
|   protected function setup(): void {
 | |
|     if ($this->reader === null) {
 | |
|       $this->reader = reader::with($this->ppInput);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   function producer() {
 | |
|     $reader = $this->reader;
 | |
|     $flags = JSON_THROW_ON_ERROR;
 | |
|     if ($this->ppParseWhole) {
 | |
|       $contents = $reader->readContents();
 | |
|       yield json_decode($contents, true, 512, $flags);
 | |
|     } else {
 | |
|       while (true) {
 | |
|         try {
 | |
|           $line = $reader->readLine();
 | |
|           yield json_decode($line, true, 512, $flags);
 | |
|         } catch (EOFException $e) {
 | |
|           break;
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   protected function teardown(): void {
 | |
|     if ($this->reader !== null) {
 | |
|       $this->reader->close();
 | |
|       $this->reader = null;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   #############################################################################
 | |
|   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*/[
 | |
|     'getInput' => 'input',
 | |
|     'isParseWhole' => 'parse_whole',
 | |
|   ];
 | |
|   const _AUTO_SETTERS = /*autogen*/[
 | |
|     'setInput' => 'input',
 | |
|     'setParseWhole' => 'parse_whole',
 | |
|   ];
 | |
|   #--autogen-dynamic--
 | |
| }
 |