diff --git a/CHANGELOG.md b/CHANGELOG.md index acdaf573..20c27727 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,25 @@ All Notable changes to `Csv` will be documented in this file +## 8.2.1 - 2017-02-22 + +### Added + +- None + +### Deprecated + +- None + +### Fixed + +- internal `Reader::getRow` when using a `StreamIterator` [issue #213](https://github.com/thephpleague/csv/issues/213) +- Removed `@deprecated` from selected methods [issue #208](https://github.com/thephpleague/csv/issues/213) + +### Removed + +- None + ## 8.2.0 - 2017-01-25 ### Added diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php index 9b21786d..35eeba40 100644 --- a/src/Modifier/QueryFilter.php +++ b/src/Modifier/QueryFilter.php @@ -64,10 +64,6 @@ trait QueryFilter /** * Stripping BOM setter * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @param bool $status * * @return $this diff --git a/src/Modifier/RowFilter.php b/src/Modifier/RowFilter.php index 4002ba14..786410fc 100644 --- a/src/Modifier/RowFilter.php +++ b/src/Modifier/RowFilter.php @@ -54,10 +54,6 @@ public function addFormatter(callable $callable) /** * Remove a formatter from the collection * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @param callable $callable * * @return $this @@ -73,10 +69,6 @@ public function removeFormatter(callable $callable) /** * Detect if the formatter is already registered * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @param callable $callable * * @return bool @@ -89,10 +81,6 @@ public function hasFormatter(callable $callable) /** * Remove all registered formatter * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @return $this */ public function clearFormatters() @@ -127,10 +115,6 @@ abstract protected function validateString($str); /** * Remove a validator from the collection * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @param string $name the validator name * * @return $this @@ -146,10 +130,6 @@ public function removeValidator($name) /** * Detect if a validator is already registered * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @param string $name the validator name * * @return bool @@ -164,10 +144,6 @@ public function hasValidator($name) /** * Remove all registered validators * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @return $this */ public function clearValidators() diff --git a/src/Modifier/StreamIterator.php b/src/Modifier/StreamIterator.php index edda2213..122aed7f 100644 --- a/src/Modifier/StreamIterator.php +++ b/src/Modifier/StreamIterator.php @@ -156,18 +156,17 @@ public function setFlags($flags) * @param array $fields * @param string $delimiter * @param string $enclosure - * @param string $escape * * @return int */ - public function fputcsv(array $fields, $delimiter = null, $enclosure = null, $escape = null) + public function fputcsv(array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\') { return fputcsv( $this->stream, $fields, - null !== $delimiter ? $this->filterControl($delimiter, 'delimiter') : $this->delimiter, - null !== $enclosure ? $this->filterControl($enclosure, 'enclosure') : $this->enclosure, - null !== $escape ? $this->filterControl($escape, 'escape') : $this->escape + $this->filterControl($delimiter, 'delimiter'), + $this->filterControl($enclosure, 'enclosure'), + $this->filterControl($escape, 'escape') ); } diff --git a/src/Reader.php b/src/Reader.php index 2e3e2029..7f7940a6 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -17,7 +17,6 @@ use Iterator; use League\Csv\Modifier\MapIterator; use LimitIterator; -use SplFileObject; /** * A class to manage extracting and filtering a CSV @@ -50,10 +49,6 @@ public function fetchAll(callable $callable = null) /** * Fetch the next row from a result set * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * @param callable|null $callable a callable function to be applied to each Iterator item * * @return Iterator @@ -83,10 +78,6 @@ protected function applyCallable(Iterator $iterator, callable $callable = null) /** * Applies a callback function on the CSV * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * The callback function must return TRUE in order to continue * iterating over the iterator. * @@ -166,6 +157,7 @@ public function fetchColumn($column_index = 0, callable $callable = null) * DEPRECATION WARNING! This method will be removed in the next major point release * * @deprecated deprecated since version 8.2 + * @see Reader::fetchPairs * * Fetches an associative array of all rows as key-value pairs (first * column is the key, second column is the value). @@ -226,10 +218,6 @@ public function fetchPairs($offset_index = 0, $value_index = 1, callable $callab /** * Fetch the next row from a result set * - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 8.2 - * * The rows are presented as associated arrays * The callable function will be applied to each row * @@ -329,19 +317,23 @@ protected function isValidKey($value) protected function getRow($offset) { $fileObj = $this->getIterator(); - $fileObj->setFlags(SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); $iterator = new LimitIterator($fileObj, $offset, 1); $iterator->rewind(); - $line = $iterator->current(); - - if (empty($line)) { + $row = $iterator->current(); + if (empty($row)) { throw new InvalidArgumentException('the specified row does not exist or is empty'); } - if (0 === $offset && $this->isBomStrippable()) { - $line = mb_substr($line, mb_strlen($this->getInputBOM())); + if (0 !== $offset || !$this->isBomStrippable()) { + return $row; + } + + $bom_length = mb_strlen($this->getInputBOM()); + $row[0] = mb_substr($row[0], $bom_length); + if ($this->enclosure == mb_substr($row[0], 0, 1) && $this->enclosure == mb_substr($row[0], -1, 1)) { + $row[0] = mb_substr($row[0], 1, -1); } - return str_getcsv($line, $this->delimiter, $this->enclosure, $this->escape); + return $row; } } diff --git a/test/StreamIteratorTest.php b/test/StreamIteratorTest.php index f59b439f..4fe2bc56 100644 --- a/test/StreamIteratorTest.php +++ b/test/StreamIteratorTest.php @@ -6,6 +6,7 @@ use League\Csv\Reader; use League\Csv\Writer; use PHPUnit_Framework_TestCase; +use SplFileObject; /** * @group stream @@ -117,10 +118,9 @@ public function testGetReader() { $fp = fopen('php://temp', 'r+'); $csv = Writer::createFromStream($fp); - $expected = [ - ['john', 'doe', 'john.doe@example.com'], - 'john,doe,john.doe@example.com', + ['john', 'doe', 'john.doe@e"xample.com'], + 'john,doe,john.doe@e"xample.com', ]; foreach ($expected as $row) { @@ -128,7 +128,15 @@ public function testGetReader() } $reader = $csv->newReader(); - $this->assertSame(['john', 'doe', 'john.doe@example.com'], $reader->fetchOne(0)); + $this->assertSame(['john', 'doe', 'john.doe@e"xample.com'], $reader->fetchOne(0)); + } + + public function testInsertNormalFile() + { + $csv = new StreamIterator(fopen(__DIR__.'/data/foo.csv', 'a+')); + $csv->fputcsv(['jane', 'doe', 'jane.doe@example.com']); + $csv->setFlags(SplFileObject::READ_CSV); + $this->assertContains(['jane', 'doe', 'jane.doe@example.com'], $csv); } public function testToString() diff --git a/test/data/tmp.txt b/test/data/tmp.txt new file mode 100644 index 00000000..c8cac97f --- /dev/null +++ b/test/data/tmp.txt @@ -0,0 +1,2 @@ +1st +2nd