124 lines
3.1 KiB
PHP
124 lines
3.1 KiB
PHP
|
<?php
|
||
|
namespace nur\io\line;
|
||
|
|
||
|
use IteratorAggregate;
|
||
|
use nur\A;
|
||
|
use nur\b\ICloseable;
|
||
|
use nur\b\io\EOFException;
|
||
|
use nur\b\io\IReader;
|
||
|
use nur\b\io\Tfilter;
|
||
|
use nur\b\params\Parametrable;
|
||
|
use nur\b\params\Tparametrable;
|
||
|
use nur\data\types\Metadata;
|
||
|
use nur\io\Tencoding;
|
||
|
use nur\reader;
|
||
|
|
||
|
/**
|
||
|
* Class LineReader
|
||
|
*
|
||
|
* --autogen-properties-and-methods--
|
||
|
* @method string|null getInputEncoding()
|
||
|
* @method string|null getOutputEncoding()
|
||
|
* @method setInput($value)
|
||
|
* @method string|null setInputEncoding(?string $value)
|
||
|
* @method string|null setOutputEncoding(?string $value)
|
||
|
*/
|
||
|
class LineReader extends Parametrable implements IteratorAggregate, ICloseable {
|
||
|
use Tparametrable, Tencoding, Tfilter;
|
||
|
|
||
|
function __construct($input=null, ?array $params=null) {
|
||
|
A::set_nn($params, "input", $input);
|
||
|
parent::__construct($params);
|
||
|
}
|
||
|
|
||
|
function setEncodingFilter(string $from, string $to="utf-8"): void {
|
||
|
$this->_setEncodingFilter($from, $to);
|
||
|
}
|
||
|
|
||
|
const PARAMETRABLE_PARAMS_SCHEMA = [
|
||
|
"input" => [null, null, "fichier en entrée"],
|
||
|
"input_encoding" => ["?string", null, "encoding en entrée"],
|
||
|
"output_encoding" => ["?string", null, "encoding en sortie"],
|
||
|
];
|
||
|
|
||
|
protected $ppInput;
|
||
|
|
||
|
function pp_setInput($input): void {
|
||
|
if ($input instanceof IReader) $this->reader = $input;
|
||
|
else $this->ppInput = $input;
|
||
|
}
|
||
|
|
||
|
protected function afterSetParametrableParams(array $modifiedKeys, ?Metadata $md=null): void {
|
||
|
$this->encodingInput__afterSetParametrableParams($modifiedKeys);
|
||
|
}
|
||
|
|
||
|
/** @var IReader */
|
||
|
protected $reader;
|
||
|
|
||
|
function getIterator() {
|
||
|
$reader = $this->reader;
|
||
|
if ($reader === null) {
|
||
|
$reader = $this->reader = reader::with($this->ppInput);
|
||
|
$this->_rwAppendFilters($this->reader);
|
||
|
}
|
||
|
try {
|
||
|
while (true) {
|
||
|
try {
|
||
|
yield $reader->readLine();
|
||
|
} catch (EOFException $e) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
} finally {
|
||
|
$this->close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function close(): void {
|
||
|
if ($this->reader !== null) {
|
||
|
$this->reader->close();
|
||
|
$this->reader = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#############################################################################
|
||
|
const _AUTOGEN_CONSTS = [
|
||
|
"" => [self::class, "_AUTOGEN_CONSTS", self::class],
|
||
|
];
|
||
|
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*/[
|
||
|
'getInputEncoding' => 'input_encoding',
|
||
|
'getOutputEncoding' => 'output_encoding',
|
||
|
];
|
||
|
const _AUTO_SETTERS = /*autogen*/[
|
||
|
'setInput' => 'input',
|
||
|
'setInputEncoding' => 'input_encoding',
|
||
|
'setOutputEncoding' => 'output_encoding',
|
||
|
];
|
||
|
#--autogen-dynamic--
|
||
|
}
|