Skip to content

Commit

Permalink
Rework of Iterators for existing cells only
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Apr 30, 2015
1 parent 75bb9d7 commit 7ccb900
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 168 deletions.
101 changes: 18 additions & 83 deletions Classes/PHPExcel/Worksheet/CellIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,28 @@
* @package PHPExcel_Worksheet
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Worksheet_CellIterator implements Iterator
abstract class PHPExcel_Worksheet_CellIterator
{
/**
* PHPExcel_Worksheet to iterate
*
* @var PHPExcel_Worksheet
*/
private $_subject;

/**
* Row index
*
* @var int
*/
private $_rowIndex;
protected $_subject;

/**
* Current iterator position
*
* @var int
* @var mixed
*/
private $_position = 0;
protected $_position = null;

/**
* Loop only existing cells
* Iterate only existing cells
*
* @var boolean
*/
private $_onlyExistingCells = true;

/**
* Create a new cell iterator
*
* @param PHPExcel_Worksheet $subject
* @param int $rowIndex
*/
public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1) {
// Set subject and row index
$this->_subject = $subject;
$this->_rowIndex = $rowIndex;
}
protected $_onlyExistingCells = false;

/**
* Destructor
Expand All @@ -84,63 +65,6 @@ public function __destruct() {
unset($this->_subject);
}

/**
* Rewind iterator
*/
public function rewind() {
$this->_position = 0;
}

/**
* Current PHPExcel_Cell
*
* @return PHPExcel_Cell
*/
public function current() {
return $this->_subject->getCellByColumnAndRow($this->_position, $this->_rowIndex);
}

/**
* Current key
*
* @return int
*/
public function key() {
return $this->_position;
}

/**
* Next value
*/
public function next() {
++$this->_position;
}

/**
* Are there any more PHPExcel_Cell instances available?
*
* @return boolean
*/
public function valid() {
// columnIndexFromString() returns an index based at one,
// treat it as a count when comparing it to the base zero
// position.
$columnCount = PHPExcel_Cell::columnIndexFromString($this->_subject->getHighestColumn());

if ($this->_onlyExistingCells) {
// If we aren't looking at an existing cell, either
// because the first column doesn't exist or next() has
// been called onto a nonexistent cell, then loop until we
// find one, or pass the last column.
while ($this->_position < $columnCount &&
!$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) {
++$this->_position;
}
}

return $this->_position < $columnCount;
}

/**
* Get loop only existing cells
*
Expand All @@ -150,12 +74,23 @@ public function getIterateOnlyExistingCells() {
return $this->_onlyExistingCells;
}

/**
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
*
* @throws PHPExcel_Exception
*/
abstract protected function adjustForExistingOnlyRange() {
}

/**
* Set the iterator to loop only existing cells
*
* @param boolean $value
* @throws PHPExcel_Exception
*/
public function setIterateOnlyExistingCells($value = true) {
$this->_onlyExistingCells = $value;
$this->_onlyExistingCells = (boolean) $value;

$this->adjustForExistingOnlyRange();
}
}
88 changes: 44 additions & 44 deletions Classes/PHPExcel/Worksheet/ColumnCellIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,28 @@
* @package PHPExcel_Worksheet
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
class PHPExcel_Worksheet_ColumnCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
{
/**
* PHPExcel_Worksheet to iterate
*
* @var PHPExcel_Worksheet
*/
private $_subject;

/**
* Column index
*
* @var string
*/
private $_columnIndex;

/**
* Current iterator position
*
* @var int
*/
private $_position = 1;
protected $_columnIndex;

/**
/**
* Start position
*
* @var int
*/
private $_startRow = 1;
protected $_startRow = 1;

/**
* End position
*
* @var int
*/
private $_endRow = 1;

/**
* Loop only existing cells
*
* @var boolean
*/
private $_onlyExistingCells = true;
protected $_endRow = 1;

/**
* Create a new row iterator
Expand Down Expand Up @@ -106,10 +85,12 @@ public function __destruct() {
* (Re)Set the start row and the current row pointer
*
* @param integer $startRow The row number at which to start iterating
* @return PHPExcel_Worksheet_RowIterator
* @return PHPExcel_Worksheet_ColumnCellIterator
* @throws PHPExcel_Exception
*/
public function resetStart($startRow = 1) {
$this->_startRow = $startRow;
$this->adjustForExistingOnlyRange();
$this->seek($startRow);

return $this;
Expand All @@ -119,10 +100,12 @@ public function resetStart($startRow = 1) {
* (Re)Set the end row
*
* @param integer $endRow The row number at which to stop iterating
* @return PHPExcel_Worksheet_RowIterator
* @return PHPExcel_Worksheet_ColumnCellIterator
* @throws PHPExcel_Exception
*/
public function resetEnd($endRow = null) {
$this->_endRow = ($endRow) ? $endRow : $this->_subject->getHighestRow();
$this->adjustForExistingOnlyRange();

return $this;
}
Expand All @@ -131,12 +114,14 @@ public function resetEnd($endRow = null) {
* Set the row pointer to the selected row
*
* @param integer $row The row number to set the current pointer at
* @return PHPExcel_Worksheet_RowIterator
* @return PHPExcel_Worksheet_ColumnCellIterator
* @throws PHPExcel_Exception
*/
public function seek($row = 1) {
if (($row < $this->_startRow) || ($row > $this->_endRow)) {
throw new PHPExcel_Exception("Row $row is out of range ({$this->_startRow} - {$this->_endRow})");
} elseif ($this->_onlyExistingCells && !($this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $row))) {
throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
}
$this->_position = $row;

Expand All @@ -151,7 +136,7 @@ public function rewind() {
}

/**
* Return the current row in this worksheet
* Return the current cell in this worksheet column
*
* @return PHPExcel_Worksheet_Row
*/
Expand All @@ -172,7 +157,11 @@ public function key() {
* Set the iterator to its next value
*/
public function next() {
++$this->_position;
do {
++$this->_position;
} while (($this->_onlyExistingCells) &&
(!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_position)) &&
($this->_position <= $this->_endRow));
}

/**
Expand All @@ -183,7 +172,11 @@ public function prev() {
throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->_startRow} - {$this->_endRow})");
}

--$this->_position;
do {
--$this->_position;
} while (($this->_onlyExistingCells) &&
(!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_position)) &&
($this->_position >= $this->_startRow));
}

/**
Expand All @@ -196,20 +189,27 @@ public function valid() {
}

/**
* Get loop only existing cells
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
*
* @return boolean
* @throws PHPExcel_Exception
*/
public function getIterateOnlyExistingCells() {
return $this->_onlyExistingCells;
protected function adjustForExistingOnlyRange() {
if ($this->_onlyExistingCells) {
while ((!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_startRow)) &&
($this->_startRow <= $this->_endRow)) {
++$this->_startRow;
}
if ($this->_startRow > $this->_endRow) {
throw new PHPExcel_Exception('No cells exist within the specified range');
}
while ((!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_endRow)) &&
($this->_endRow >= $this->_startRow)) {
--$this->_endRow;
}
if ($this->_endRow < $this->_startRow) {
throw new PHPExcel_Exception('No cells exist within the specified range');
}
}
}

/**
* Set the iterator to loop only existing cells
*
* @param boolean $value
*/
public function setIterateOnlyExistingCells($value = true) {
$this->_onlyExistingCells = $value;
}
}
Loading

0 comments on commit 7ccb900

Please sign in to comment.