note 86624 deleted from function.fgetcsv by danbrown
| From: | danbrown@php.net | Date: | Wed, 18 Mar 2009 21:59:58 +0000 |
| Subject: | note 86624 deleted from function.fgetcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152113@lists.php.net to get a copy of this message | ||
Note Submitter: cizar at nixar dot org
----
In my opinion, the two examples that use the SPL Iterator Interface don't work as they should.
If you call several times to the method current() then it eventually will reach the eof...
<?php
public function current() {
$this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE,
$this->delimiter);
$this->rowCounter++;
return $this->currentElement;
}
?>
If you want to go next, you need to call the method next(). So here is my solution...
<?php
/**
* @author cizar@nixar.org
*/
class CsvIterator implements Iterator
{
const DEFAULT_DELIMITER = ';';
const DEFAULT_LENGTH = 4096;
/**
* The CSV file handler.
*
* @var resource
* @access private
*/
private $_handler = null;
/**
* The delimiter of the CSV file.
*
* @var string
* @access private
*/
private $_delimiter = null;
/**
* The dafs
*
* @var integer
* @access private
*/
private $_length = null;
/**
* The row counter.
*
* @var integer
* @access private
*/
private $_key = null;
/**
* The element that will be returned on each iteration.
*
* @var mixed
* @access private
*/
private $_current = null;
/**
* This is the constructor. It try to open the CSV file.
*
* @access public
* @param string $filename The fullpath of the CSV file.
* @param string $delimiter The delimiter.
* @param integer $length The amount of bytes to be read on each iteration.
*
* @throws Exception
*/
public function __construct ($filename, $delimiter = self::DEFAULT_DELIMITER, $length =
self::DEFAULT_LENGTH)
{
if (($this->_handler = fopen($filename, 'r')) === false) {
throw new Exception("The file '$filename' cannot be opened");
}
$this->_delimiter = $delimiter;
$this->_length = $length;
}
/**
* This is the destructor. It close the CSV file.
*
* @access public
*/
public function __destruct ()
{
fclose($this->_handler);
}
/**
* This method move the file pointer to the next row.
*
* @access public
*/
public function next ()
{
$this->_read();
$this->_key += 1;
}
/**
* This method reset the file handler.
*
* @access public
*/
public function rewind ()
{
rewind($this->_handler);
$this->_read();
$this->_key = 0;
}
/**
* This method returns the current row number.
*
* @access public
*/
public function key ()
{
return $this->_key;
}
/**
* This methods return the current CSV row data.
*
* @access public
* @return array The row as an one-dimensional array
*/
public function current ()
{
return $this->_current;
}
/**
* This method checks if the current row is readable.
*
* @access public
* @return boolean If the current row is readable.
*/
public function valid ()
{
return $this->_current !== FALSE;
}
/**
* This method read the next row of the CSV file.
*
* @access private
*/
private function _read ()
{
$this->_current = fgetcsv($this->_handler, $this->_length, $this->_delimiter);
}
}