Skip to content

Commit

Permalink
Resolve issue #102
Browse files Browse the repository at this point in the history
The enclosure character should also be stripped when
The BOM character is removed to properly output CSV content
  • Loading branch information
nyamsprod committed Jun 4, 2015
1 parent ca52426 commit c26e80e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
#Changelog
All Notable changes to `League\Csv` will be documented in this file

## 7.1.2 - 2015-06-XX

- Enclosures should be removed when a BOM sequence is stripped [bug fix #102](http://github.com/thephpleague/csv/issues/99)

## 7.1.1 - 2015-05-20

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions src/Config/Output.php
Expand Up @@ -129,11 +129,11 @@ public function getInputBOM()
{
if (! $this->input_bom) {
$bom = [
self::BOM_UTF8,
self::BOM_UTF16_BE,
self::BOM_UTF16_LE,
self::BOM_UTF32_BE,
self::BOM_UTF32_LE,
self::BOM_UTF16_BE,
self::BOM_UTF16_LE,
self::BOM_UTF8,
];
$csv = $this->getIterator();
$csv->setFlags(SplFileObject::READ_CSV);
Expand Down
16 changes: 12 additions & 4 deletions src/Modifier/QueryFilter.php
Expand Up @@ -88,6 +88,11 @@ protected function isBomStrippable()
return ! empty($bom) && $this->strip_bom;
}

/**
* {@inheritdoc}
*/
abstract public function getInputBom();

/**
* Set LimitIterator Offset
*
Expand Down Expand Up @@ -255,18 +260,21 @@ protected function applyBomStripping(Iterator $iterator)
return new MapIterator($iterator, function ($row, $index) use ($bom) {
if (0 == $index) {
$row[0] = mb_substr($row[0], mb_strlen($bom));
$enclosure = $this->getEnclosure();
//enclosure should be remove when a BOM sequence is stripped
if ($row[0][0] === $enclosure && mb_substr($row[0], -1, 1) == $enclosure) {
$row[0] = mb_substr($row[0], 1, -1);
}
}

return $row;
});
}

/**
* Returns the BOM sequence of the given CSV
*
* @return string
* {@inheritdoc}
*/
abstract public function getInputBom();
abstract public function getEnclosure();

/**
* Filter the Iterator
Expand Down
10 changes: 7 additions & 3 deletions src/Reader.php
Expand Up @@ -17,6 +17,7 @@
use Iterator;
use League\Csv\Modifier;
use LimitIterator;
use SplFileObject;

/**
* A class to manage extracting and filtering a CSV
Expand Down Expand Up @@ -229,18 +230,21 @@ protected function getAssocKeys($offset_or_keys)
*/
protected function getRow($offset)
{
$iterator = new LimitIterator($this->getIterator(), $offset, 1);
$csv = $this->getIterator();
$csv->setFlags($this->getFlags() & ~SplFileObject::READ_CSV);

$iterator = new LimitIterator($csv, $offset, 1);
$iterator->rewind();
$res = $iterator->current();
if (empty($res)) {
throw new InvalidArgumentException('the specified row does not exist or is empty');
}

if (0 == $offset && $this->isBomStrippable()) {
$res[0] = mb_substr($res[0], mb_strlen($this->getInputBom()));
$res = mb_substr($res, mb_strlen($this->getInputBom()));
}

return $res;
return str_getcsv($res, $this->delimiter, $this->enclosure, $this->escape);
}

/**
Expand Down

0 comments on commit c26e80e

Please sign in to comment.