From 70d905d19f595f51cba352aa568af47e2b7ef8dd Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Fri, 30 Oct 2015 10:23:57 +0100 Subject: [PATCH] Improve Internal Code --- src/AbstractCsv.php | 2 +- src/Reader.php | 33 +++++++++++++++------------------ test/ReaderTest.php | 9 +++++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 2c353a52..bd075373 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -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; } diff --git a/src/Reader.php b/src/Reader.php index 9da44a61..e422d312 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -207,9 +207,7 @@ 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]])) { @@ -217,7 +215,7 @@ protected function getAssocKeys($offset_or_keys) } $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; }; @@ -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'); } /** diff --git a/test/ReaderTest.php b/test/ReaderTest.php index e16610c5..d650c510 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -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