Skip to content

Commit

Permalink
Improve AbstractCsv implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 23, 2023
1 parent 22bca25 commit 509ab51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"require": {
"php": "^8.1.2",
"ext-filter": "*",
"ext-json": "*",
"ext-mbstring": "*"
},
Expand Down
29 changes: 17 additions & 12 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AbstractCsv implements ByteSequence
/**
* @final This method should not be overwritten in child classes
*/
protected function __construct(protected SplFileObject|Stream $document)
protected function __construct(protected readonly SplFileObject|Stream $document)
{
[$this->delimiter, $this->enclosure, $this->escape] = $this->document->getCsvControl();
$this->resetProperties();
Expand All @@ -64,11 +64,6 @@ protected function resetProperties(): void
{
}

public function __destruct()
{
unset($this->document);
}

/**
* @throws UnavailableStream
*/
Expand Down Expand Up @@ -258,15 +253,25 @@ public function output(string $filename = null): int
}

$this->document->rewind();
if (!$this->is_input_bom_included) {
if (-1 === $this->document->fseek(strlen($this->getInputBOM()))) {
throw new RuntimeException('Unable to seek the document.');
}
$this->document->setFlags(0);
if (!$this->is_input_bom_included && -1 === $this->document->fseek(strlen($this->getInputBOM()))) {
throw new RuntimeException('Unable to seek the document.');
}

echo $this->output_bom;
$stream = Stream::createFromString($this->output_bom);
$stream->rewind();

$res1 = $stream->fpassthru();
if (false === $res1) {
throw new RuntimeException('Unable to output the document.');
}

$res2 = $this->document->fpassthru();
if (false === $res2) {
throw new RuntimeException('Unable to output the document.');
}

return strlen($this->output_bom) + (int) $this->document->fpassthru();
return $res1 + $res2;
}

/**
Expand Down

0 comments on commit 509ab51

Please sign in to comment.