diff options
-rw-r--r-- | src/AbstractCsv.php | 26 | ||||
-rw-r--r-- | src/Reader.php | 10 | ||||
-rw-r--r-- | src/Writer.php | 32 | ||||
-rw-r--r-- | test/WriterTest.php | 24 |
4 files changed, 49 insertions, 43 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index d448887..e200a0b 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -189,31 +189,37 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * Detect the CSV file delimiter * - * @param integer $nbRows + * @param integer $nb_rows * @param array $delimiters additional delimiters * * @return string * - * @throws \InvalidArgumentException If $nbRows value is invalid + * @throws \InvalidArgumentException If $nb_rows value is invalid * @throws \RuntimeException If too many delimiters are found */ - public function detectDelimiter($nbRows = 1, array $delimiters = []) + public function detectDelimiter($nb_rows = 1, array $delimiters = []) { - $nbRows = filter_var($nbRows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); - if (! $nbRows) { - throw new InvalidArgumentException('`$nbRows` must be a valid positive integer'); + $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'); } + + //detect and 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); + + //"reduce" the csv length to a maximum of $nb_rows $iterator = new CallbackFilterIterator( - new LimitIterator($this->getIterator(), 0, $nbRows), + new LimitIterator($this->getIterator(), 0, $nb_rows), function ($row) { return is_array($row) && count($row) > 1; } ); + + //detecting the possible delimiter $res = []; foreach ($delimiters as $delim) { $iterator->setCsvControl($delim, $this->enclosure, $this->escape); @@ -507,14 +513,14 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * Return a HTML table representation of the CSV Table * - * @param string $classname optional classname + * @param string $class_name optional classname * * @return string */ - public function toHTML($classname = 'table-csv-data') + public function toHTML($class_name = 'table-csv-data') { $doc = $this->toXML('table', 'tr', 'td'); - $doc->documentElement->setAttribute('class', $classname); + $doc->documentElement->setAttribute('class', $class_name); return $doc->saveHTML($doc->documentElement); } diff --git a/src/Reader.php b/src/Reader.php index 2bad873..1d55d73 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -193,21 +193,21 @@ class Reader extends AbstractCsv * * @throws \InvalidArgumentException If the column index is not a positive integer or 0 */ - public function fetchCol($columnIndex = 0, callable $callable = null) + public function fetchCol($column_index = 0, callable $callable = null) { - if (false === filter_var($columnIndex, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { + if (false === filter_var($column_index, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { throw new InvalidArgumentException( 'the column index must be a positive integer or 0' ); } $iterator = $this->query($callable); - $iterator = new MapIterator($iterator, function ($row) use ($columnIndex) { - if (! array_key_exists($columnIndex, $row)) { + $iterator = new MapIterator($iterator, function ($row) use ($column_index) { + if (! array_key_exists($column_index, $row)) { return null; } - return $row[$columnIndex]; + return $row[$column_index]; }); return iterator_to_array($iterator, false); diff --git a/src/Writer.php b/src/Writer.php index 8cbfa8c..01d8c42 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -73,14 +73,14 @@ class Writer extends AbstractCsv * * @var integer */ - protected $column_count = -1; + protected $columns_count = -1; /** * should the class detect the column count based the inserted row * * @var boolean */ - protected $detect_column_count = false; + protected $detect_columns_count = false; /** * Tell the class how to handle null value @@ -119,13 +119,13 @@ class Writer extends AbstractCsv * * @throws \InvalidArgumentException If $value is lesser than -1 */ - public function setColumnCount($value) + public function setColumnsCount($value) { if (false === filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => -1]])) { throw new InvalidArgumentException('the column count must an integer greater or equals to -1'); } - $this->detect_column_count = false; - $this->column_count = $value; + $this->detect_columns_count = false; + $this->columns_count = $value; return $this; } @@ -135,21 +135,21 @@ class Writer extends AbstractCsv * * @return integer */ - public function getColumnCount() + public function getColumnsCount() { - return $this->column_count; + return $this->columns_count; } /** - * The method will set the $column_count property according to the next inserted row + * The method will set the $columns_count property according to the next inserted row * and therefore will also validate the next line whatever length it has no matter - * the current $column_count property value. + * the current $columns_count property value. * * @return self */ - public function autoDetectColumnCount() + public function autodetectColumnsCount() { - $this->detect_column_count = true; + $this->detect_columns_count = true; return $this; } @@ -185,15 +185,15 @@ class Writer extends AbstractCsv } $row = $this->formatRow($row); - if ($this->detect_column_count) { - $this->column_count = count($row); - $this->detect_column_count = false; + if ($this->detect_columns_count) { + $this->columns_count = count($row); + $this->detect_columns_count = false; } - if (-1 != $this->column_count && count($row) != $this->column_count) { + if (-1 != $this->columns_count && count($row) != $this->columns_count) { throw new InvalidArgumentException( 'You are trying to add '.count($row).' columns to a CSV - that requires '.$this->column_count.' columns.' + that requires '.$this->columns_count.' columns.' ); } diff --git a/test/WriterTest.php b/test/WriterTest.php index bf922c0..490d197 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -87,35 +87,35 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException */ - public function testColumCountSetterGetter() + public function testColumsCountSetterGetter() { - $this->assertSame(-1, $this->csv->getColumnCount()); - $this->csv->setColumnCount(3); - $this->assertSame(3, $this->csv->getColumnCount()); - $this->csv->setColumnCount('toto'); + $this->assertSame(-1, $this->csv->getColumnsCount()); + $this->csv->setColumnsCount(3); + $this->assertSame(3, $this->csv->getColumnsCount()); + $this->csv->setColumnsCount('toto'); } /** * @expectedException InvalidArgumentException */ - public function testColumCountConsistency() + public function testColumsCountConsistency() { $this->csv->insertOne(['john', 'doe', 'john.doe@example.com']); - $this->csv->setColumnCount(2); + $this->csv->setColumnsCount(2); $this->csv->insertOne(['jane', 'jane.doe@example.com']); - $this->csv->setColumnCount(3); + $this->csv->setColumnsCount(3); $this->csv->insertOne(['jane', 'jane.doe@example.com']); } /** * @expectedException InvalidArgumentException */ - public function testAutoDetectColumnCount() + public function testAutoDetectColumnsCount() { - $this->csv->autoDetectColumnCount(); - $this->assertSame(-1, $this->csv->getColumnCount()); + $this->csv->autodetectColumnsCount(); + $this->assertSame(-1, $this->csv->getColumnsCount()); $this->csv->insertOne(['john', 'doe', 'john.doe@example.com']); - $this->assertSame(3, $this->csv->getColumnCount()); + $this->assertSame(3, $this->csv->getColumnsCount()); $this->csv->insertOne(['jane', 'jane.doe@example.com']); } |