diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-01-14 13:22:39 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-01-14 13:22:39 +0100 |
commit | cec42302e0eab1afa4203907b1cb749bc42919dc (patch) | |
tree | 3a9784e49aa3a2df49bbc191392444424b8d27fe | |
parent | eaba6681ae03d4f2dce03263e20f0527aa0b473e (diff) | |
download | csv-cec42302e0eab1afa4203907b1cb749bc42919dc.zip csv-cec42302e0eab1afa4203907b1cb749bc42919dc.tar.gz csv-cec42302e0eab1afa4203907b1cb749bc42919dc.tar.bz2 |
Adding Countable and ArrayAccess Interval to Bakame\Csv\Reader
-rw-r--r-- | src/Bakame/Csv/Reader.php | 86 | ||||
-rw-r--r-- | test/Bakame/Csv/ReaderTest.php | 23 |
2 files changed, 101 insertions, 8 deletions
diff --git a/src/Bakame/Csv/Reader.php b/src/Bakame/Csv/Reader.php index 1eb5519..53af1df 100644 --- a/src/Bakame/Csv/Reader.php +++ b/src/Bakame/Csv/Reader.php @@ -32,9 +32,12 @@ */ namespace Bakame\Csv; -use SplFileObject; -use InvalidArgumentException; +use ArrayAccess; use CallbackFilterIterator; +use Countable; +use InvalidArgumentException; +use RuntimeException; +use SplFileObject; /** * A Reader to ease CSV parsing in PHP 5.4+ @@ -43,7 +46,7 @@ use CallbackFilterIterator; * @since 3.0.0 * */ -class Reader implements ReaderInterface +class Reader implements ReaderInterface, Countable, ArrayAccess { use CsvControlsTrait; @@ -69,6 +72,13 @@ class Reader implements ReaderInterface private $limit = 0; /** + * Total numbers of Row + * + * @var integer + */ + private $rowCount = 0; + + /** * The constructor * * @param SplFileObject $file The CSV file Object @@ -295,13 +305,12 @@ class Reader implements ReaderInterface */ public function fetchAssoc(array $keys, callable $callable = null) { - $nbKeys = count($keys); - $keys = array_unique(array_filter($keys, function ($value) { - return is_scalar($value); + $validKeys = array_unique(array_filter($keys, function ($value) { + return is_string($value) || is_integer($value); })); - if (count($keys) != $nbKeys) { - throw new InvalidArgumentException('The named keys should be unique strings'); + if ($keys !== $validKeys) { + throw new InvalidArgumentException('The named keys should be unique strings Or integer'); } $res = []; @@ -369,4 +378,65 @@ class Reader implements ReaderInterface return ob_get_clean(); } + + /** + * Countable Interface - Returns the numbers of rows + * + * @return integer + */ + public function count() + { + if (! $this->rowCount) { + foreach ($this->file as $key => $value) { + + } + $this->rowCount = $key + 1; + } + + return $this->rowCount; + } + + /** + * Array Access Interface - Return a given row + * + * @param integer $offset + * + * @return array + */ + public function offsetGet($offset) + { + return $this->fetchOne($offset); + } + + /** + * Array Access Interface - Is the row exists + * + * @param integer $offset + * + * @return boolean + */ + public function offsetExists($offset) + { + return ! is_null($this->fetchOne($offset)); + } + + /** + * Array Access Interface - set row + * + * @throws RuntimeException Not implemented in a reader + */ + public function offsetSet($offset, $value) + { + throw new RuntimeException('This is a Reader, setting data is forbidden'); + } + + /** + * Array Access Interface - remove a row + * + * @throws RuntimeException Not implemented in a reader + */ + public function offsetUnset($offset) + { + throw new RuntimeException('This is a Reader, deleting data is forbidden'); + } } diff --git a/test/Bakame/Csv/ReaderTest.php b/test/Bakame/Csv/ReaderTest.php index c687287..bc18237 100644 --- a/test/Bakame/Csv/ReaderTest.php +++ b/test/Bakame/Csv/ReaderTest.php @@ -189,4 +189,27 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $res); $this->assertSame($this->expected[1], $res[0]); } + + public function testCountable() + { + $this->assertCount(count($this->expected), $this->csv); + } + + /** + * @expectedException \RuntimeException + */ + public function testArrayAccess() + { + $this->assertSame($this->expected[0], $this->csv[0]); + $this->assertTrue(isset($this->csv[0])); + $this->csv[3] = "fdsfqsfdsf"; + } + + /** + * @expectedException \RuntimeException + */ + public function testArrayAccessFailure() + { + unset($this->csv[3]); + } } |