Skip to content

Commit

Permalink
Merge pull request #3 from nyamsprod/develop
Browse files Browse the repository at this point in the history
Bug Fix Reader::fetchAssoc
  • Loading branch information
nyamsprod committed Jan 10, 2014
2 parents b278afa + 1c37432 commit d4474b8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Bakame/Csv/Codec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Bakame/Csv/CsvControlsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions src/Bakame/Csv/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
*/
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Bakame/Csv/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/Bakame/Csv/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d4474b8

Please sign in to comment.