Skip to content

Commit

Permalink
Bug fix #213 and #208 (#215)
Browse files Browse the repository at this point in the history
* Bug fix #213 and #208

- Remove the @deprecate message from the Reader class #208
- Bug fix internal Reader::getRow when used with a StreamIterator #213
- Bug fix StreamIterator::fputcsv
  • Loading branch information
nyamsprod authored Feb 23, 2017
1 parent ef7eef7 commit 43fd8b0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 57 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/Modifier/QueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions src/Modifier/RowFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions src/Modifier/StreamIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

Expand Down
32 changes: 12 additions & 20 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Iterator;
use League\Csv\Modifier\MapIterator;
use LimitIterator;
use SplFileObject;

/**
* A class to manage extracting and filtering a CSV
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
}
16 changes: 12 additions & 4 deletions test/StreamIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use League\Csv\Reader;
use League\Csv\Writer;
use PHPUnit_Framework_TestCase;
use SplFileObject;

/**
* @group stream
Expand Down Expand Up @@ -117,18 +118,25 @@ 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) {
$csv->insertOne($row);
}

$reader = $csv->newReader();
$this->assertSame(['john', 'doe', '[email protected]'], $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', '[email protected]']);
$csv->setFlags(SplFileObject::READ_CSV);
$this->assertContains(['jane', 'doe', '[email protected]'], $csv);
}

public function testToString()
Expand Down
2 changes: 2 additions & 0 deletions test/data/tmp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1st
2nd

0 comments on commit 43fd8b0

Please sign in to comment.