Skip to content

Commit

Permalink
bug fix preserve fileObject CSV controls
Browse files Browse the repository at this point in the history
When using AbstractCsv::createFromFileObject
the CSV object inherit the SplFileObject csv controls

A php bug was filled because SplFileObject::getCsvControl did not
return the escape caracter (see: https://bugs.php.net/bug.php?id=72646)

The bug is resolved so the patch can be completely applied and the testsuite
is updated accordingly
  • Loading branch information
nyamsprod committed Jul 25, 2016
1 parent 2ec6db1 commit 8a20c56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ public function __destruct()
*/
public static function createFromFileObject(SplFileObject $file)
{
return new static($file);
$csv = new static($file);
$controls = $file->getCsvControl();
$csv->setDelimiter($controls[0]);
$csv->setEnclosure($controls[1]);
if (isset($controls[2])) {
$csv->setEscape($controls[2]);
}

return $csv;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ public function testCreateFromFileObjectWithSplFileObject()
$this->assertInstanceof(Reader::class, $reader);
$this->assertInstanceof(SplFileObject::class, $reader->getIterator());
}


public function testCreateFromFileObjectPreserveFileObjectCsvControls()
{
$delimiter = "\t";
$enclosure = '?';
$escape = '^';
$file = new SplTempFileObject();
$file->setCsvControl($delimiter, $enclosure, $escape);
$obj = Reader::createFromFileObject($file);
$this->assertSame($delimiter, $obj->getDelimiter());
$this->assertSame($enclosure, $obj->getEnclosure());
if (3 === count($file->getCsvControl())) {
$this->assertSame($escape, $obj->getEscape());
}
}
}

0 comments on commit 8a20c56

Please sign in to comment.