diff options
-rw-r--r-- | CHANGELOG.md | 14 | ||||
-rw-r--r-- | src/AbstractCsv.php | 97 | ||||
-rw-r--r-- | src/Config/Controls.php | 113 | ||||
-rw-r--r-- | src/Config/StreamFilter.php | 2 | ||||
-rw-r--r-- | src/Iterator/Filter.php | 22 | ||||
-rw-r--r-- | src/Iterator/Interval.php | 6 | ||||
-rw-r--r-- | src/Iterator/MapIterator.php | 2 | ||||
-rw-r--r-- | src/Iterator/SortBy.php | 22 | ||||
-rw-r--r-- | src/Reader.php | 35 | ||||
-rw-r--r-- | src/Writer.php | 16 | ||||
-rw-r--r-- | test/CsvTest.php | 26 | ||||
-rw-r--r-- | test/ReaderTest.php | 10 | ||||
-rw-r--r-- | test/WriterTest.php | 2 |
13 files changed, 122 insertions, 245 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6b5c7..ee5d557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,18 +6,20 @@ All Notable changes to `League\Csv` will be documented in this file ### Added - Stream Filter API in `League\Csv\AbstractCsv` - named constructors `createFromPath` and `createFromFileObject` in `League\Csv\AbstractCsv` to ease CSV object instantiation -- `detectDelimiters` in `League\Csv\AbstractCsv` to replace and remove the use of `Exception` in `detectDelimiter` -- `setEncodingFrom` and `setDecodingFrom` in `League\Csv\AbstractCsv` +- `detectDelimiterList` in `League\Csv\AbstractCsv` to replace and remove the use of `Exception` in `detectDelimiter` +- `setEncodingFrom` and `setDecodingFrom` in `League\Csv\AbstractCsv` to replace `setEncoding` and `getEncoding` for naming consistency ### Deprecated -- `detectDelimiter` in `League\Csv\AbstractCsv` in favor of `detectDelimiters` -- `setEncoding` and `getEncoding` in `League\Csv\AbstractCsv` replaced by `setEncodingFrom` and `setDecodingFrom` for naming consistency +- Nothing ### Fixed - `League\Csv\Reader::each` more stric `$callable` MUST returns true ### Remove -- Nothing +- `detectDelimiter` +- `setEncoding` and `getEncoding` +- `League\Csv\Reader::setSortBy` +- `League\Csv\Reader::setFilter` ### Security - Nothing @@ -126,7 +128,7 @@ All Notable changes to `League\Csv` will be documented in this file - Nothing ### Remove -- Change namespace from `Bakame\Csv` to `League\Csv` +- Nothing ### Security - Nothing diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 13eb463..f20d21a 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -12,15 +12,12 @@ */ namespace League\Csv; -use CallbackFilterIterator; use DomDocument; use InvalidArgumentException; use IteratorAggregate; use JsonSerializable; use League\Csv\Config\Controls; use League\Csv\Config\StreamFilter; -use LimitIterator; -use RuntimeException; use SplFileInfo; use SplFileObject; use SplTempFileObject; @@ -244,86 +241,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** - * detect the actual number of row according to a delimiter - * - * @param string $delimiter a CSV delimiter - * @param integer $nb_rows the number of row to consider - * - * @return integer - */ - protected function fetchRowsCountByDelimiter($delimiter, $nb_rows = 1) - { - $iterator = $this->getIterator(); - $iterator->setCsvControl($delimiter, $this->enclosure, $this->escape); - //"reduce" the csv length to a maximum of $nb_rows - $iterator = new LimitIterator($iterator, 0, $nb_rows); - //return the parse rows - $iterator = new CallbackFilterIterator($iterator, function ($row) { - return is_array($row) && count($row) > 1; - }); - - return count(iterator_to_array($iterator, false)); - } - - /** - * Detect the CSV file delimiter - * - * @param integer $nb_rows - * @param string[] $delimiters additional delimiters - * - * @return string[] - * - * @throws \InvalidArgumentException If $nb_rows value is invalid - */ - public function detectDelimiters($nb_rows = 1, array $delimiters = []) - { - $nb_rows = filter_var($nb_rows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); - if (! $nb_rows) { - throw new InvalidArgumentException('`$nb_rows` must be a valid positive integer'); - } - - $delimiters = array_filter($delimiters, function ($str) { - return 1 == mb_strlen($str); - }); - $delimiters = array_merge([$this->delimiter, ',', ';', "\t"], $delimiters); - $delimiters = array_unique($delimiters); - $res = array_fill_keys($delimiters, 0); - array_walk($res, function (&$value, $delim) use ($nb_rows) { - $value = $this->fetchRowsCountByDelimiter($delim, $nb_rows); - }); - - arsort($res, SORT_NUMERIC); - - return array_keys(array_filter($res)); - } - - /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.5 - * - * Detect the CSV file delimiter - * - * @param integer $nb_rows - * @param string[] $delimiters additional delimiters - * - * @return null|string - * - * @throws \InvalidArgumentException If $nb_rows value is invalid - * @throws \RuntimeException If too many delimiters are found - */ - public function detectDelimiter($nb_rows = 1, array $delimiters = []) - { - $res = $this->detectDelimiters($nb_rows, $delimiters); - if (! $res) { - return null; - } elseif (1 == count($res)) { - return $res[0]; - } - throw new RuntimeException('too many delimiters were found: `'.implode('`,`', $res).'`'); - } - - /** * Return the CSV Iterator * * @return \SplFileObject @@ -430,4 +347,16 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate return $doc->saveHTML($doc->documentElement); } + + /** + * Validate a variable to be stringable + * + * @param mixed $str + * + * @return boolean + */ + public static function isValidString($str) + { + return is_scalar($str) || (is_object($str) && method_exists($str, '__toString')); + } } diff --git a/src/Config/Controls.php b/src/Config/Controls.php index 50a5065..f1239bc 100644 --- a/src/Config/Controls.php +++ b/src/Config/Controls.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -12,8 +12,10 @@ */ namespace League\Csv\Config; +use CallbackFilterIterator; use InvalidArgumentException; use League\Csv\Iterator\MapIterator; +use LimitIterator; use SplFileObject; use Traversable; @@ -66,7 +68,7 @@ trait Controls * * @param string $delimiter * - * @return self + * @return $this * * @throws \InvalidArgumentException If $delimeter is not a single character */ @@ -91,11 +93,72 @@ trait Controls } /** + * detect the actual number of row according to a delimiter + * + * @param string $delimiter a CSV delimiter + * @param integer $nb_rows the number of row to consider + * + * @return integer + */ + protected function fetchRowsCountByDelimiter($delimiter, $nb_rows = 1) + { + $iterator = $this->getIterator(); + $iterator->setCsvControl($delimiter, $this->enclosure, $this->escape); + //"reduce" the csv length to a maximum of $nb_rows + $iterator = new LimitIterator($iterator, 0, $nb_rows); + //return the parse rows + $iterator = new CallbackFilterIterator($iterator, function ($row) { + return is_array($row) && count($row) > 1; + }); + + return count(iterator_to_array($iterator, false)); + } + + /** + * Detect the CSV file delimiter + * + * @param integer $nb_rows + * @param string[] $delimiters additional delimiters + * + * @return string[] + * + * @throws \InvalidArgumentException If $nb_rows value is invalid + */ + public function detectDelimiterList($nb_rows = 1, array $delimiters = []) + { + $nb_rows = filter_var($nb_rows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); + if (! $nb_rows) { + throw new InvalidArgumentException('`$nb_rows` must be a valid positive integer'); + } + + $delimiters = array_filter($delimiters, function ($str) { + return 1 == mb_strlen($str); + }); + $delimiters = array_merge([$this->delimiter, ',', ';', "\t"], $delimiters); + $delimiters = array_unique($delimiters); + $res = array_fill_keys($delimiters, 0); + array_walk($res, function (&$value, $delim) use ($nb_rows) { + $value = $this->fetchRowsCountByDelimiter($delim, $nb_rows); + }); + + arsort($res, SORT_NUMERIC); + + return array_keys(array_filter($res)); + } + + /** + * return a SplFileOjbect + * + * @return \SplFileOjbect + */ + abstract public function getIterator(); + + /** * set the field enclosure * * @param string $enclosure * - * @return self + * @return $this * * @throws \InvalidArgumentException If $enclosure is not a single character */ @@ -124,7 +187,7 @@ trait Controls * * @param string $escape * - * @return self + * @return $this * * @throws \InvalidArgumentException If $escape is not a single character */ @@ -153,7 +216,7 @@ trait Controls * * @param integer $flags * - * @return self + * @return $this * * @throws \InvalidArgumentException If the argument is not a valid integer */ @@ -179,37 +242,11 @@ trait Controls } /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.5 - * - * @param string $str - * - * @return static The invoked object - */ - public function setEncoding($str) - { - return $this->setEncodingFrom($str); - } - - /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.5 - * - * @return string - */ - public function getEncoding() - { - return $this->getEncodingFrom(); - } - - /** * Set the CSV encoding charset * * @param string $str * - * @return self + * @return $this */ public function setEncodingFrom($str) { @@ -253,16 +290,4 @@ trait Controls return $row; }); } - - /** - * Validate a variable to be stringable - * - * @param mixed $str - * - * @return boolean - */ - public static function isValidString($str) - { - return is_scalar($str) || (is_object($str) && method_exists($str, '__toString')); - } } diff --git a/src/Config/StreamFilter.php b/src/Config/StreamFilter.php index 90c2cff..0514ad4 100644 --- a/src/Config/StreamFilter.php +++ b/src/Config/StreamFilter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Iterator/Filter.php b/src/Iterator/Filter.php index 3554f13..503a988 100644 --- a/src/Iterator/Filter.php +++ b/src/Iterator/Filter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -33,25 +33,11 @@ trait Filter protected $iterator_filters = []; /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.1 - * - * @param callable $callable - * - * @return static The invoked object - */ - public function setFilter(callable $callable) - { - return $this->addFilter($callable); - } - - /** * Set the Iterator filter method * * @param callable $callable * - * @return static The invoked object + * @return $this */ public function addFilter(callable $callable) { @@ -65,7 +51,7 @@ trait Filter * * @param callable $callable * - * @return static The invoked object + * @return $this */ public function removeFilter(callable $callable) { @@ -92,7 +78,7 @@ trait Filter /** * Remove all registered callable filter * - * @return static The invoked object + * @return $this */ public function clearFilter() { diff --git a/src/Iterator/Interval.php b/src/Iterator/Interval.php index 50b49ec..e8c3b4b 100644 --- a/src/Iterator/Interval.php +++ b/src/Iterator/Interval.php @@ -3,7 +3,7 @@ * This file is part of the League.csv library * * @license http://opensource.org/licenses/MIT -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -43,7 +43,7 @@ trait Interval * * @param $offset * - * @return static The invoked object + * @return $this */ public function setOffset($offset = 0) { @@ -60,7 +60,7 @@ trait Interval * * @param integer $limit * - * @return static The invoked object + * @return $this */ public function setLimit($limit = -1) { diff --git a/src/Iterator/MapIterator.php b/src/Iterator/MapIterator.php index 3239902..19e89f4 100644 --- a/src/Iterator/MapIterator.php +++ b/src/Iterator/MapIterator.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Iterator/SortBy.php b/src/Iterator/SortBy.php index e05d53b..eb929c7 100644 --- a/src/Iterator/SortBy.php +++ b/src/Iterator/SortBy.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -33,25 +33,11 @@ trait SortBy protected $iterator_sort_by = []; /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.2 - * - * @param callable $callable - * - * @return static The invoked object - */ - public function setSortBy(callable $callable) - { - return $this->addSortBy($callable); - } - - /** * Set an Iterator sorting callable function * * @param callable $callable * - * @return static The invoked object + * @return $this */ public function addSortBy(callable $callable) { @@ -65,7 +51,7 @@ trait SortBy * * @param callable $callable * - * @return static The invoked object + * @return $this */ public function removeSortBy(callable $callable) { @@ -92,7 +78,7 @@ trait SortBy /** * Remove all registered callable * - * @return static The invoked object + * @return $this */ public function clearSortBy() { diff --git a/src/Reader.php b/src/Reader.php index d901725..13c478b 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -191,25 +191,6 @@ class Reader extends AbstractCsv } /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.4 - * - * The callable function will be applied to each value to be return - * - * @param integer $column_index field Index - * @param callable $callable a callable function - * - * @return array - * - * @throws \InvalidArgumentException If the column index is not a positive integer or 0 - */ - public function fetchCol($column_index = 0, callable $callable = null) - { - return $this->fetchColumn($column_index, $callable); - } - - /** * Return a single column from the CSV data * * The callable function will be applied to each value to be return @@ -240,18 +221,4 @@ class Reader extends AbstractCsv return iterator_to_array($iterator, false); } - - /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.5 - * - * @param string $open_mode the file open mode flag - * - * @return \League\Csv\Writer object - */ - public function getWriter($open_mode = 'r+') - { - return $this->newWriter($open_mode); - } } diff --git a/src/Writer.php b/src/Writer.php index 0ae9154..49973c5 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 5.5.0 +* @version 6.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -315,18 +315,4 @@ class Writer extends AbstractCsv return $this; } - - /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.5 - * - * @param string $open_mode the file open mode flag - * - * @return \League\Csv\Reader object - */ - public function getReader($open_mode = 'r+') - { - return $this->newReader($open_mode); - } } diff --git a/test/CsvTest.php b/test/CsvTest.php index c912a18..4934211 100644 --- a/test/CsvTest.php +++ b/test/CsvTest.php @@ -100,33 +100,29 @@ class CsvTest extends PHPUnit_Framework_TestCase $this->csv->setDelimiter('foo'); } - public function testDetectDelimiter() + public function testDetectDelimiterList() { - $this->assertSame($this->csv->detectDelimiter(), ','); + $this->assertSame([','], $this->csv->detectDelimiterList()); } /** * @expectedException InvalidArgumentException * @expectedExceptionMessage `$nb_rows` must be a valid positive integer */ - public function testDetectDelimiterWithInvalidRowLimit() + public function testDetectDelimiterListWithInvalidRowLimit() { - $this->csv->detectDelimiter(-4); + $this->csv->detectDelimiterList(-4); } - public function testDetectDelimiterWithNoCSV() + public function testDetectDelimiterListWithNoCSV() { $file = new SplTempFileObject; $file->fwrite("How are you today ?\nI'm doing fine thanks!"); $csv = Writer::createFromFileObject($file); - $this->assertNull($csv->detectDelimiter(5, ['toto', '|'])); + $this->assertSame([], $csv->detectDelimiterList(5, ['toto', '|'])); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage 'too many delimiters were found: `;`,`|`' - */ - public function testDetectDelimiterWithInconsistentCSV() + public function testDetectDelimiterListWithInconsistentCSV() { $data = new SplTempFileObject; $data->setCsvControl(';'); @@ -137,7 +133,7 @@ class CsvTest extends PHPUnit_Framework_TestCase $data->fputcsv(['toto', 'tata', 'tutu']); $csv = Writer::createFromFileObject($data); - $csv->detectDelimiter(5, ['|']); + $this->assertSame(['|', ';'], $csv->detectDelimiterList(5, ['|'])); } /** @@ -180,10 +176,10 @@ class CsvTest extends PHPUnit_Framework_TestCase public function testEncoding() { $expected = 'iso-8859-15'; - $this->csv->setEncoding($expected); - $this->assertSame(strtoupper($expected), $this->csv->getEncoding()); + $this->csv->setEncodingFrom($expected); + $this->assertSame(strtoupper($expected), $this->csv->getEncodingFrom()); - $this->csv->setEncoding(''); + $this->csv->setEncodingFrom(''); } public function testToString() diff --git a/test/ReaderTest.php b/test/ReaderTest.php index 7773d6e..f18324c 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -71,7 +71,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase $func = function ($row) { return ! in_array('jane', $row); }; - $this->csv->setFilter($func); + $this->csv->addFilter($func); $this->assertCount(1, $this->csv->fetchAll()); @@ -97,7 +97,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase $func = function ($rowA, $rowB) { return strcmp($rowA[0], $rowB[0]); }; - $this->csv->setSortBy($func); + $this->csv->addSortBy($func); $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll()); $this->csv->addSortBy($func); @@ -114,7 +114,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase $func = function ($rowA, $rowB) { return strcmp($rowA[0], $rowB[0]); }; - $csv->setSortBy($func); + $csv->addSortBy($func); $this->assertSame([ ['john', 'doe', 'john.doe@example.com'], ['john', 'doe', 'john.doe@example.com'] @@ -182,7 +182,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase public function testFetchCol() { - $this->assertSame(['john', 'jane'], $this->csv->fetchCol(0)); + $this->assertSame(['john', 'jane'], $this->csv->fetchColumn(0)); $this->assertSame(['john', 'jane'], $this->csv->fetchColumn()); } @@ -283,7 +283,7 @@ EOF; public function testGetWriter2() { - $csv = Reader::createFromPath(__DIR__.'/foo.csv')->getWriter('a+'); + $csv = Reader::createFromPath(__DIR__.'/foo.csv')->newWriter('a+'); $this->assertInstanceOf('\League\Csv\Writer', $csv); } } diff --git a/test/WriterTest.php b/test/WriterTest.php index 7b514c6..aedee30 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -214,7 +214,7 @@ class WriterTest extends PHPUnit_Framework_TestCase $this->csv->insertOne($row); } - $reader = $this->csv->getReader(); + $reader = $this->csv->newReader(); $this->assertSame(['john', 'doe', 'john.doe@example.com'], $reader->fetchOne(0)); } } |