From 0f5361ed7e3ac945f17df0d4c511f9bc5cce00e6 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Fri, 10 Jan 2014 13:42:57 +0100 Subject: [PATCH 1/2] Bug fix in the Reader class to allow fetchAssoc to combine array when the number of items does not match --- src/Bakame/Csv/Reader.php | 27 ++++++++++++++++++++++++--- test/Bakame/Csv/ReaderTest.php | 12 ++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Bakame/Csv/Reader.php b/src/Bakame/Csv/Reader.php index dc7f37ba..cc31e784 100644 --- a/src/Bakame/Csv/Reader.php +++ b/src/Bakame/Csv/Reader.php @@ -6,7 +6,7 @@ * @copyright 2013 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 3.0.0 +* @version 3.0.1 * @package Bakame.csv * * MIT LICENSE @@ -143,6 +143,27 @@ public function fetchAll(callable $callable = null) return $res; } + /** + * Intelligent Array Combine + * + * @param array $keys + * @param array $value + * + * @return array + */ + private static function combineKeyValue(array $keys, array $value) + { + $nbKeys = count($keys); + $diff = $nbKeys - count($value); + if ($diff > 0) { + $value = array_merge($value, array_fill(0, $diff, null)); + } elseif ($diff < 0) { + $value = array_slice($value, 0, $nbKeys); + } + + return array_combine($keys, $value); + } + /** * {@inheritdoc} */ @@ -153,13 +174,13 @@ public function fetchAssoc(array $keys, callable $callable = null) $this->file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); if (is_null($callable)) { foreach ($this->file as $row) { - $res[] = array_combine($keys, $row); + $res[] = self::combineKeyValue($keys, $row); } return $res; } foreach ($this->file as $row) { - $res[] = array_combine($keys, $callable($row)); + $res[] = self::combineKeyValue($keys, $callable($row)); } return $res; diff --git a/test/Bakame/Csv/ReaderTest.php b/test/Bakame/Csv/ReaderTest.php index b061b062..8ccd49f1 100644 --- a/test/Bakame/Csv/ReaderTest.php +++ b/test/Bakame/Csv/ReaderTest.php @@ -57,6 +57,18 @@ public function testFetchAssoc() foreach ($res as $index => $row) { $this->assertSame($keys, array_keys($row)); } + + $keys = ['firstname']; + $res = $this->reader->fetchAssoc($keys); + $this->assertSame([['firstname' => 'foo'], ['firstname' => 'foo']], $res); + + $keys = ['firstname', 'lastname', 'email', 'age']; + $res = $this->reader->fetchAssoc($keys); + foreach ($res as $index => $row) { + $this->assertCount(4, array_values($row)); + $this->assertNull($row['age']); + } + } public function testFetchCol() From 1c37432acae6cb81f7c66d33fc4301090b24a399 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Fri, 10 Jan 2014 13:43:11 +0100 Subject: [PATCH 2/2] Bump version to 3.0.1 --- README.md | 6 ++++++ src/Bakame/Csv/Codec.php | 2 +- src/Bakame/Csv/CsvControlsTrait.php | 2 +- src/Bakame/Csv/ReaderInterface.php | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 155ce804..628921b4 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,10 @@ $data = $reader->fetchAll(function ($value) { This method returns a sequentials array of all CSV rows. the rows are associative arrays where the key are given to the method using a array. +**Of Note:** +* If the number of values in a CSV row is lesser than the number of named keys, the method will add `null` values to compensate for the missing values. +* If the number of values in a CSV row is greater that the number of named keys the exceeding values will be drop from the result set. + ```php $data = $reader->fetchAssoc(['firstname', 'lastname', 'email']); // will return something like this : @@ -173,6 +177,8 @@ $data = $reader->fetchAssoc(['firstname', 'lastname', 'email'], function ($value // ``` + + #### `Reader::fetchCol` This method returns an sequentials array for a given CSV column. diff --git a/src/Bakame/Csv/Codec.php b/src/Bakame/Csv/Codec.php index b1fb6eee..383693f6 100644 --- a/src/Bakame/Csv/Codec.php +++ b/src/Bakame/Csv/Codec.php @@ -6,7 +6,7 @@ * @copyright 2013 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 3.0.0 +* @version 3.0.1 * @package Bakame.csv * * MIT LICENSE diff --git a/src/Bakame/Csv/CsvControlsTrait.php b/src/Bakame/Csv/CsvControlsTrait.php index 200308ba..85945ef9 100644 --- a/src/Bakame/Csv/CsvControlsTrait.php +++ b/src/Bakame/Csv/CsvControlsTrait.php @@ -6,7 +6,7 @@ * @copyright 2013 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 3.0.0 +* @version 3.0.1 * @package Bakame.csv * * MIT LICENSE diff --git a/src/Bakame/Csv/ReaderInterface.php b/src/Bakame/Csv/ReaderInterface.php index 55c42a4a..39a9142b 100644 --- a/src/Bakame/Csv/ReaderInterface.php +++ b/src/Bakame/Csv/ReaderInterface.php @@ -6,7 +6,7 @@ * @copyright 2013 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 3.0.0 +* @version 3.0.1 * @package Bakame.csv * * MIT LICENSE