Skip to content

Commit

Permalink
Adds IteratorReaders
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 27, 2015
1 parent 975c385 commit 89ab731
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Reader/CountableIteratorReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Ddeboer\DataImport\Reader;

/**
* Use a class implementing both \Iterator and \Countable as a reader
*
* This class uses count() on iterators implementing \Countable interface
* and iterator_count in any further cases
*
* Be careful! iterator_count iterates through the whole iterator loading every data into the memory (for example from streams)
* It is not recommended for very big datasets.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class CountableIteratorReader extends IteratorReader implements CountableReader
{
/**
* {@inheritdoc}
*/
public function count()
{
if ($this->iterator instanceof \Countable) {
return count($this->iterator);
}

return iterator_count($this->iterator);
}
}
21 changes: 21 additions & 0 deletions src/Reader/IteratorReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Ddeboer\DataImport\Reader;

use Ddeboer\DataImport\Reader;

/**
* Use an iterator as a reader
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class IteratorReader extends \IteratorIterator implements Reader
{
/**
* {@inheritdoc}
*/
public function getFields()
{
return array_keys($this->current());
}
}

0 comments on commit 89ab731

Please sign in to comment.