Skip to content

Commit

Permalink
Improve Internal Code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 30, 2015
1 parent 8516512 commit 70d905d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static function createFromPath($path, $open_mode = 'r+')
*/
protected static function validateString($str)
{
if (is_scalar($str) || (is_object($str) && method_exists($str, '__toString'))) {
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
return (string) $str;
}

Expand Down
33 changes: 15 additions & 18 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,15 @@ public function fetchAssoc($offset_or_keys = 0, callable $callable = null)
protected function getAssocKeys($offset_or_keys)
{
if (is_array($offset_or_keys)) {
$this->assertValidAssocKeys($offset_or_keys);

return $offset_or_keys;
return $this->validateAssocKeys($offset_or_keys);
}

if (false === filter_var($offset_or_keys, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
throw new InvalidArgumentException('the row index must be a positive integer, 0 or a non empty array');
}

$keys = $this->getRow($offset_or_keys);
$this->assertValidAssocKeys($keys);
$keys = $this->validateAssocKeys($keys);
$filterOutRow = function ($row, $rowIndex) use ($offset_or_keys) {
return is_array($row) && $rowIndex != $offset_or_keys;
};
Expand All @@ -233,23 +231,22 @@ protected function getAssocKeys($offset_or_keys)
*
* @throws InvalidArgumentException If the submitted array fails the assertion
*/
protected function assertValidAssocKeys(array $keys)
protected function validateAssocKeys(array $keys)
{
if (empty($keys) || $keys !== array_unique(array_filter($keys, [$this, 'isValidString']))) {
throw new InvalidArgumentException('Use a flat array with unique string values');
if (empty($keys)) {
throw new InvalidArgumentException('The array can not be empty');
}
}

/**
* Returns whether the submitted value can be used as string
*
* @param mixed $value
*
* @return bool
*/
protected function isValidString($value)
{
return is_scalar($value) || (is_object($value) && method_exists($value, '__toString'));
foreach ($keys as &$str) {
$str = $this->validateString($str);
}
unset($str);

if ($keys == array_unique($keys)) {
return $keys;
}

throw new InvalidArgumentException('The array must contain unique values');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions test/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ public function testFetchAssocWithRowIndex()
$this->assertContains(['D' => '6', 'E' => '7', 'F' => '8'], $res);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testFetchAssocThrowsExceptionWithNonUniqueAssocKeys()
{
$keys = ['firstname', 'lastname', 'firstname'];
$this->csv->fetchAssoc($keys);
}

/**
* @param $expected
* @dataProvider validBOMSequences
Expand Down

0 comments on commit 70d905d

Please sign in to comment.