-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
975c385
commit 89ab731
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |