Skip to content

Commit

Permalink
prepare for 6.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 1, 2014
1 parent bcb0fd0 commit a20b9eb
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 62 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#Changelog
All Notable changes to `League\Csv` will be documented in this file

## 6.1.0 - XXXX-XX-XX

### Added
- `Reader::fetchAssoc` now also accepts an integer as first argument representing a row index.

### Deprecated
- Nothing

### Fixed
- Nothing

## 6.0.1 - 2014-11-12

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down Expand Up @@ -332,7 +332,7 @@ public function output($filename = null)
/**
* Validate a variable to be stringable
*
* @param string $str
* @param object|string $str
*
* @return bool
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Controls.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Config/StreamFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Iterator/MapIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Iterator/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down
16 changes: 6 additions & 10 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down Expand Up @@ -99,12 +99,8 @@ public function fetchOne($offset = 0)
$this->setLimit(1);
$iterator = $this->query();
$iterator->rewind();
$res = $iterator->current();
if (! is_array($res)) {
return [];
}

return $res;
return (array) $iterator->current();
}

/**
Expand All @@ -131,7 +127,7 @@ public function fetchAll(callable $callable = null)
* used as the associated named keys
* @param callable $callable a callable function
*
* @throws \InvalidArgumentException If the submitted keys are not integer or strng
* @throws \InvalidArgumentException If the submitted keys are not integer or string
*
* @return array
*/
Expand All @@ -140,12 +136,12 @@ public function fetchAssoc($keys = 0, callable $callable = null)
$keys = $this->formatAssocKeys($keys);
if (! $this->isValidAssocKeys($keys)) {
throw new InvalidArgumentException(
'The named keys should be unique strings Or integer'
'Use a flat non empty array with unique string values'
);
}
$iterator = $this->query($callable);
$iterator = new MapIterator($iterator, function ($row) use ($keys) {
return self::combineArray($keys, $row);
return static::combineArray($keys, $row);
});

return iterator_to_array($iterator, false);
Expand Down Expand Up @@ -175,7 +171,7 @@ protected function formatAssocKeys($keys)
$iterator = new LimitIterator($this->getIterator(), $keys, 1);
$iterator->rewind();

return $iterator->current();
return (array) $iterator->current();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 6.0.1
* @version 6.1.0
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
Expand Down
111 changes: 66 additions & 45 deletions test/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ public function testFetchAssoc()
public function testFetchAssocCallback()
{
$keys = ['firstname', 'lastname', 'email'];
$func = function ($value) {
$res = $this->csv->fetchAssoc($keys, function ($value) {
return array_map('strtoupper', $value);
};
$res = $this->csv->fetchAssoc($keys, $func);
});
foreach ($res as $row) {
$this->assertSame($keys, array_keys($row));
}
Expand All @@ -171,15 +170,78 @@ public function testFetchAssocMoreKeys()
}
}

public function testFetchAssocWithRowIndex()
{
$arr = [
['A', 'B', 'C'],
[1, 2, 3],
['D', 'E', 'F'],
[6, 7, 8],
];

$tmpFile = new SplTempFileObject();
foreach ($arr as $row) {
$tmpFile->fputcsv($row);
}

$csv = Reader::createFromFileObject($tmpFile);
$res = $csv->setOffSet(2)->fetchAssoc(2);
$this->assertSame([['D' => '6', 'E' => '7', 'F' => '8']], $res);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The named keys should be unique strings Or integer
* @expectedExceptionMessage Use a flat non empty array with unique string values
*/
public function testFetchAssocKeyFailure()
{
$this->csv->fetchAssoc([['firstname', 'lastname', 'email', 'age']]);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage the column index must be a positive integer or 0
*/
public function testFetchAssocWithInvalidKey()
{
$arr = [
['A', 'B', 'C'],
[1, 2, 3],
['D', 'E', 'F'],
[6, 7, 8],
];

$tmpFile = new SplTempFileObject();
foreach ($arr as $row) {
$tmpFile->fputcsv($row);
}

$csv = Reader::createFromFileObject($tmpFile);
$csv->fetchAssoc(-23);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Use a flat non empty array with unique string values
*/
public function testFetchAssocWithEmptyArr()
{
$arr = [
['A', 'B', 'C'],
[1, 2, 3],
['D', 'E', 'F'],
[6, 7, 8],
];

$tmpFile = new SplTempFileObject();
foreach ($arr as $row) {
$tmpFile->fputcsv($row);
}

$csv = Reader::createFromFileObject($tmpFile);
$csv->fetchAssoc(23);
}

public function testFetchCol()
{
$this->assertSame(['john', 'jane'], $this->csv->fetchColumn(0));
Expand Down Expand Up @@ -286,45 +348,4 @@ public function testGetWriter2()
$csv = Reader::createFromPath(__DIR__.'/foo.csv')->newWriter('a+');
$this->assertInstanceOf('\League\Csv\Writer', $csv);
}

public function testFetchAssocWithARowIndex()
{
$arr = [
['A', 'B', 'C'],
[1, 2, 3],
['D', 'E', 'F'],
[6, 7, 8],
];

$tmpFile = new SplTempFileObject();
foreach ($arr as $row) {
$tmpFile->fputcsv($row);
}

$csv = Reader::createFromFileObject($tmpFile);
$res = $csv->setOffSet(2)->fetchAssoc(2);
$this->assertSame([['D' => '6', 'E' => '7', 'F' => '8']], $res);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage the column index must be a positive integer or 0
*/
public function testFetchAssocWithInvalidKey()
{
$arr = [
['A', 'B', 'C'],
[1, 2, 3],
['D', 'E', 'F'],
[6, 7, 8],
];

$tmpFile = new SplTempFileObject();
foreach ($arr as $row) {
$tmpFile->fputcsv($row);
}

$csv = Reader::createFromFileObject($tmpFile);
$res = $csv->fetchAssoc(-23);
}
}

0 comments on commit a20b9eb

Please sign in to comment.