diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-05-16 14:47:56 +0200 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-05-16 14:47:56 +0200 |
commit | c2ad46a6b2f67ba9bc092b4ed6ee1ec0c3976d0f (patch) | |
tree | 5149b21f198838fc03bda703e96056312c57f7be | |
parent | 62608f0157b58a118072158b16083bfc94816f8b (diff) | |
parent | 2bef289a1ea1da59f4ab58231e51fc1ea6abc37e (diff) | |
download | csv-c2ad46a6b2f67ba9bc092b4ed6ee1ec0c3976d0f.zip csv-c2ad46a6b2f67ba9bc092b4ed6ee1ec0c3976d0f.tar.gz csv-c2ad46a6b2f67ba9bc092b4ed6ee1ec0c3976d0f.tar.bz2 |
Merge pull request #40 from thephpleague/dev
library is restructured using traits
-rw-r--r-- | src/AbstractCsv.php | 286 | ||||
-rw-r--r-- | src/Config/Controls.php | 266 | ||||
-rw-r--r-- | src/Config/StreamFilter.php (renamed from src/Stream/Filter.php) | 4 | ||||
-rw-r--r-- | src/Iterator/Filter.php | 2 | ||||
-rw-r--r-- | src/Iterator/SortBy.php | 2 | ||||
-rw-r--r-- | src/Reader.php | 25 | ||||
-rw-r--r-- | src/Writer.php | 26 | ||||
-rw-r--r-- | test/CsvTest.php | 2 | ||||
-rw-r--r-- | test/ReaderTest.php | 6 | ||||
-rw-r--r-- | test/WriterTest.php | 4 |
10 files changed, 330 insertions, 293 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 3a31b09..f493919 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -14,7 +14,6 @@ namespace League\Csv; use DomDocument; use JsonSerializable; -use Traversable; use SplFileInfo; use SplFileObject; use SplTempFileObject; @@ -23,8 +22,8 @@ use InvalidArgumentException; use IteratorAggregate; use LimitIterator; use CallbackFilterIterator; -use League\Csv\Iterator\MapIterator; -use League\Csv\Stream\Filter; +use League\Csv\Config\StreamFilter; +use League\Csv\Config\Controls; /** * An abstract class to enable basic CSV manipulation @@ -38,42 +37,12 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * Stream Filter Trait */ - use Filter; + use StreamFilter; /** - * the field delimiter (one character only) - * - * @var string - */ - protected $delimiter = ','; - - /** - * the field enclosure character (one character only) - * - * @var string - */ - protected $enclosure = '"'; - - /** - * the field escape character (one character only) - * - * @var string - */ - protected $escape = '\\'; - - /** - * the \SplFileObject flags holder - * - * @var integer - */ - protected $flags = SplFileObject::READ_CSV; - - /** - * Charset Encoding for the CSV - * - * @var string + * Controls Trait */ - protected $encodingFrom = 'UTF-8'; + use Controls; /** * The constructor path @@ -143,91 +112,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** - * Instantiate a AbstractCsv extended class - * - * @param string $class_name the Class to load {@link Writer} or {@link Reader} - * @param string $open_mode the file open mode flag - * - * @return \League\Csv\AbstractCSv - */ - protected function newInstance($class_name, $open_mode) - { - $obj = new $class_name($this->path, $open_mode); - $obj->delimiter = $this->delimiter; - $obj->enclosure = $this->enclosure; - $obj->escape = $this->escape; - $obj->flags = $this->flags; - $obj->encodingFrom = $this->encodingFrom; - - return $obj; - } - - /** - * Instantiate a {@link Writer} class from the current object - * - * @param string $open_mode the file open mode flag - * - * @return \League\Csv\Writer - */ - public function newWriter($open_mode = 'r+') - { - return $this->newInstance('\League\Csv\Writer', $open_mode); - } - - /** - * Instantiate a {@link Reader} class from the current object - * - * @param string $open_mode the file open mode flag - * - * @return \League\Csv\Reader - */ - public function newReader($open_mode = 'r+') - { - return $this->newInstance('\League\Csv\Reader', $open_mode); - } - - /** - * 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'))); - } - - /** - * set the field delimeter - * - * @param string $delimiter - * - * @return self - * - * @throws \InvalidArgumentException If $delimeter is not a single character - */ - public function setDelimiter($delimiter = ',') - { - if (1 != mb_strlen($delimiter)) { - throw new InvalidArgumentException('The delimiter must be a single character'); - } - $this->delimiter = $delimiter; - - return $this; - } - - /** - * return the current field delimiter - * - * @return string - */ - public function getDelimiter() - { - return $this->delimiter; - } - - /** * Detect the CSV file delimiter * * @param integer $nb_rows @@ -245,14 +129,14 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate throw new InvalidArgumentException('`$nb_rows` must be a valid positive integer'); } - //detect and validate the possible delimiters + //validate the possible delimiters $delimiters = array_filter($delimiters, function ($str) { return 1 == mb_strlen($str); }); $delimiters = array_merge([$this->delimiter, ',', ';', "\t"], $delimiters); $delimiters = array_unique($delimiters); - //detecting the possible delimiter + //detect the possible delimiter $res = []; foreach ($delimiters as $delim) { $iterator = $this->getIterator(); @@ -271,97 +155,13 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate $res = array_keys(array_filter($res)); if (! $res) { return null; - } elseif (count($res) == 1) { + } elseif (1 == count($res)) { return $res[0]; } throw new RuntimeException('too many delimiters were found: `'.implode('`,`', $res).'`'); } /** - * set the field enclosure - * - * @param string $enclosure - * - * @return self - * - * @throws \InvalidArgumentException If $enclosure is not a single character - */ - public function setEnclosure($enclosure = '"') - { - if (1 != mb_strlen($enclosure)) { - throw new InvalidArgumentException('The enclosure must be a single character'); - } - $this->enclosure = $enclosure; - - return $this; - } - - /** - * return the current field enclosure - * - * @return string - */ - public function getEnclosure() - { - return $this->enclosure; - } - - /** - * set the field escape character - * - * @param string $escape - * - * @return self - * - * @throws \InvalidArgumentException If $escape is not a single character - */ - public function setEscape($escape = "\\") - { - if (1 != mb_strlen($escape)) { - throw new InvalidArgumentException('The escape character must be a single character'); - } - $this->escape = $escape; - - return $this; - } - - /** - * return the current field escape character - * - * @return string - */ - public function getEscape() - { - return $this->escape; - } - - /** - * Set the Flags associated to the CSV SplFileObject - * - * @return self - */ - public function setFlags($flags) - { - if (false === filter_var($flags, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { - throw new InvalidArgumentException('you should use a `SplFileObject` Constant'); - } - - $this->flags = $flags|SplFileObject::READ_CSV|SplFileObject::DROP_NEW_LINE; - - return $this; - } - - /** - * Returns the file Flags - * - * @return integer - */ - public function getFlags() - { - return $this->flags; - } - - /** * Return the CSV Iterator * * @return \SplFileObject @@ -379,76 +179,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** - * DEPRECATION WARNING! This method will be removed in the next major point release - * - * @deprecated deprecated since version 5.5 - */ - 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 - */ - public function getEncoding() - { - return $this->getEncodingFrom(); - } - - /** - * Set the CSV encoding charset - * - * @param string $str - * - * @return self - */ - public function setEncodingFrom($str) - { - $str = str_replace('_', '-', $str); - $str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH]); - if (empty($str)) { - throw new InvalidArgumentException('you should use a valid charset'); - } - $this->encodingFrom = strtoupper($str); - - return $this; - } - - /** - * Get the source CSV encoding charset - * - * @return string - */ - public function getEncodingFrom() - { - return $this->encodingFrom; - } - - /** - * Convert Csv file into UTF-8 - * - * @return \Traversable - */ - protected function convertToUtf8(Traversable $iterator) - { - if (strpos($this->encodingFrom, 'UTF-8') !== false) { - return $iterator; - } - - return new MapIterator($iterator, function ($row) { - foreach ($row as &$value) { - $value = mb_convert_encoding($value, 'UTF-8', $this->encodingFrom); - } - unset($value); - - return $row; - }); - } - - /** * JsonSerializable Interface * * @return array diff --git a/src/Config/Controls.php b/src/Config/Controls.php new file mode 100644 index 0000000..61bbe77 --- /dev/null +++ b/src/Config/Controls.php @@ -0,0 +1,266 @@ +<?php +/** +* This file is part of the League.csv library +* +* @license http://opensource.org/licenses/MIT +* @link https://github.com/thephpleague/csv/ +* @version 5.5.0 +* @package League.csv +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ +namespace League\Csv\Config; + +use Traversable; +use SplFileObject; +use InvalidArgumentException; +use League\Csv\Iterator\MapIterator; + +/** + * A trait to configure and check CSV file and content + * + * @package League.csv + * @since 5.5.0 + * + */ +trait Controls +{ + /** + * the field delimiter (one character only) + * + * @var string + */ + protected $delimiter = ','; + + /** + * the field enclosure character (one character only) + * + * @var string + */ + protected $enclosure = '"'; + + /** + * the field escape character (one character only) + * + * @var string + */ + protected $escape = '\\'; + + /** + * the \SplFileObject flags holder + * + * @var integer + */ + protected $flags = SplFileObject::READ_CSV; + + /** + * Charset Encoding for the CSV + * + * @var string + */ + protected $encodingFrom = 'UTF-8'; + + /** + * set the field delimeter + * + * @param string $delimiter + * + * @return self + * + * @throws \InvalidArgumentException If $delimeter is not a single character + */ + public function setDelimiter($delimiter = ',') + { + if (1 != mb_strlen($delimiter)) { + throw new InvalidArgumentException('The delimiter must be a single character'); + } + $this->delimiter = $delimiter; + + return $this; + } + + /** + * return the current field delimiter + * + * @return string + */ + public function getDelimiter() + { + return $this->delimiter; + } + + /** + * set the field enclosure + * + * @param string $enclosure + * + * @return self + * + * @throws \InvalidArgumentException If $enclosure is not a single character + */ + public function setEnclosure($enclosure = '"') + { + if (1 != mb_strlen($enclosure)) { + throw new InvalidArgumentException('The enclosure must be a single character'); + } + $this->enclosure = $enclosure; + + return $this; + } + + /** + * return the current field enclosure + * + * @return string + */ + public function getEnclosure() + { + return $this->enclosure; + } + + /** + * set the field escape character + * + * @param string $escape + * + * @return self + * + * @throws \InvalidArgumentException If $escape is not a single character + */ + public function setEscape($escape = "\\") + { + if (1 != mb_strlen($escape)) { + throw new InvalidArgumentException('The escape character must be a single character'); + } + $this->escape = $escape; + + return $this; + } + + /** + * return the current field escape character + * + * @return string + */ + public function getEscape() + { + return $this->escape; + } + + /** + * Set the Flags associated to the CSV SplFileObject + * + * @param integer $flags + * + * @return self + */ + public function setFlags($flags) + { + if (false === filter_var($flags, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { + throw new InvalidArgumentException('you should use a `SplFileObject` Constant'); + } + + $this->flags = $flags|SplFileObject::READ_CSV|SplFileObject::DROP_NEW_LINE; + + return $this; + } + + /** + * Returns the file Flags + * + * @return integer + */ + public function getFlags() + { + return $this->flags; + } + + /** + * DEPRECATION WARNING! This method will be removed in the next major point release + * + * @deprecated deprecated since version 5.5 + * + * @param string $str + * + * @return self + */ + 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 + */ + public function setEncodingFrom($str) + { + $str = str_replace('_', '-', $str); + $str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH]); + if (empty($str)) { + throw new InvalidArgumentException('you should use a valid charset'); + } + $this->encodingFrom = strtoupper($str); + + return $this; + } + + /** + * Get the source CSV encoding charset + * + * @return string + */ + public function getEncodingFrom() + { + return $this->encodingFrom; + } + + /** + * Convert Csv file into UTF-8 + * + * @return \Traversable + */ + protected function convertToUtf8(Traversable $iterator) + { + if (strpos($this->encodingFrom, 'UTF-8') !== false) { + return $iterator; + } + + return new MapIterator($iterator, function ($row) { + foreach ($row as &$value) { + $value = mb_convert_encoding($value, 'UTF-8', $this->encodingFrom); + } + unset($value); + + 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/Stream/Filter.php b/src/Config/StreamFilter.php index 0e24ae9..58267e1 100644 --- a/src/Stream/Filter.php +++ b/src/Config/StreamFilter.php @@ -10,7 +10,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace League\Csv\Stream; +namespace League\Csv\Config; use SplFileInfo; use SplTempFileObject; @@ -26,7 +26,7 @@ use OutOfBoundsException; * @since 5.5.0 * */ -trait Filter +trait StreamFilter { /** * collection of stream filters diff --git a/src/Iterator/Filter.php b/src/Iterator/Filter.php index b04de5e..f279a97 100644 --- a/src/Iterator/Filter.php +++ b/src/Iterator/Filter.php @@ -33,8 +33,6 @@ trait Filter protected $iterator_filters = []; /** - * Set the Iterator filter method - * * DEPRECATION WARNING! This method will be removed in the next major point release * * @deprecated deprecated since version 5.1 diff --git a/src/Iterator/SortBy.php b/src/Iterator/SortBy.php index 8e4b2e0..ad462d2 100644 --- a/src/Iterator/SortBy.php +++ b/src/Iterator/SortBy.php @@ -33,8 +33,6 @@ trait SortBy protected $iterator_sort_by = []; /** - * Set the Iterator SortBy method - * * DEPRECATION WARNING! This method will be removed in the next major point release * * @deprecated deprecated since version 5.2 diff --git a/src/Reader.php b/src/Reader.php index 473aa7c..a8ad730 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -185,8 +185,6 @@ class Reader extends AbstractCsv } /** - * Return a single column from the CSV data - * * DEPRECATION WARNING! This method will be removed in the next major point release * * @deprecated deprecated since version 5.4 @@ -237,9 +235,32 @@ class Reader extends AbstractCsv * 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); } + + /** + * Create a {@link Writer} instance from a {@link Reader} object + * + * @param string $open_mode the file open mode flag + * + * @return \League\Csv\Writer object + */ + public function newWriter($open_mode = 'r+') + { + $csv = new Writer($this->path, $open_mode); + $csv->setDelimiter($this->delimiter); + $csv->setEnclosure($this->enclosure); + $csv->setEscape($this->escape); + $csv->setFlags($this->flags); + $csv->setEncodingFrom($this->encodingFrom); + + return $csv; + } } diff --git a/src/Writer.php b/src/Writer.php index 3349f7c..8602fed 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -15,6 +15,7 @@ namespace League\Csv; use Traversable; use SplFileObject; use InvalidArgumentException; +use RuntimeException; use OutOfBoundsException; /** @@ -278,7 +279,7 @@ class Writer extends AbstractCsv $data = $this->validateRow($data); $data = $this->sanitizeColumnsContent($data); if (! $this->isColumnsCountConsistent($data)) { - throw new InvalidArgumentException( + throw new RuntimeException( 'You are trying to add '.count($data).' columns to a CSV that requires '.$this->columns_count.' columns per row.' ); @@ -318,9 +319,32 @@ class Writer extends AbstractCsv * 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); } + + /** + * Create a {@link Reader} instance from a {@link Writer} object + * + * @param string $open_mode the file open mode flag + * + * @return \League\Csv\Reader object + */ + public function newReader($open_mode = 'r+') + { + $csv = new Reader($this->path, $open_mode); + $csv->setDelimiter($this->delimiter); + $csv->setEnclosure($this->enclosure); + $csv->setEscape($this->escape); + $csv->setFlags($this->flags); + $csv->setEncodingFrom($this->encodingFrom); + + return $csv; + } } diff --git a/test/CsvTest.php b/test/CsvTest.php index 722b330..cc6c9c7 100644 --- a/test/CsvTest.php +++ b/test/CsvTest.php @@ -248,7 +248,7 @@ EOF; { $this->assertSame(json_encode($this->expected), json_encode($this->csv)); $csv = Reader::createFromString($rawCsv); - $csv->setEncoding('iso-8859-15'); + $csv->setEncodingFrom('iso-8859-15'); json_encode($csv); $this->assertEquals(JSON_ERROR_NONE, json_last_error()); } diff --git a/test/ReaderTest.php b/test/ReaderTest.php index 8e81225..fb3f919 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -206,7 +206,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase return array_map('strtoupper', $value); }; - $this->assertSame(['JOHN', 'JANE'], $this->csv->fetchCol(0, $func)); + $this->assertSame(['JOHN', 'JANE'], $this->csv->fetchColumn(0, $func)); } /** @@ -214,7 +214,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase */ public function testFetchColFailure() { - $this->csv->fetchCol('toto'); + $this->csv->fetchColumn('toto'); } /** @@ -250,7 +250,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase public function testGetWriter() { - $writer = $this->csv->getWriter(); + $writer = $this->csv->newWriter(); $writer->insertOne(['toto', 'le', 'herisson']); $expected = <<<EOF <table class="table-csv-data"> diff --git a/test/WriterTest.php b/test/WriterTest.php index 95ddd43..ff67e73 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -115,7 +115,7 @@ class WriterTest extends PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException RuntimeException */ public function testColumsCountConsistency() { @@ -127,7 +127,7 @@ class WriterTest extends PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException RuntimeException */ public function testAutoDetectColumnsCount() { |