mirror of
https://github.com/retailcrm/PHPExcel.git
synced 2024-11-25 23:06:03 +03:00
Added RowIterator support for end row, and throws exceptions for invalid arguments
This commit is contained in:
parent
a287bd44df
commit
f96d9cedba
@ -2560,10 +2560,12 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
|
||||
* Get row iterator
|
||||
*
|
||||
* @param integer $startRow The row number at which to start iterating
|
||||
* @param integer $endRow The row number at which to stop iterating
|
||||
*
|
||||
* @return PHPExcel_Worksheet_RowIterator
|
||||
*/
|
||||
public function getRowIterator($startRow = 1) {
|
||||
return new PHPExcel_Worksheet_RowIterator($this,$startRow);
|
||||
public function getRowIterator($startRow = 1, $endRow = null) {
|
||||
return new PHPExcel_Worksheet_RowIterator($this, $startRow, $endRow);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,15 +59,25 @@ class PHPExcel_Worksheet_RowIterator implements Iterator
|
||||
private $_startRow = 1;
|
||||
|
||||
|
||||
/**
|
||||
* End position
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_endRow = 1;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new row iterator
|
||||
*
|
||||
* @param PHPExcel_Worksheet $subject The worksheet to iterate over
|
||||
* @param integer $startRow The row number at which to start iterating
|
||||
* @param integer $endRow Optionally, the row number at which to stop iterating
|
||||
*/
|
||||
public function __construct(PHPExcel_Worksheet $subject = null, $startRow = 1) {
|
||||
public function __construct(PHPExcel_Worksheet $subject = null, $startRow = 1, $endRow = null) {
|
||||
// Set subject
|
||||
$this->_subject = $subject;
|
||||
$this->resetEnd($endRow);
|
||||
$this->resetStart($startRow);
|
||||
}
|
||||
|
||||
@ -86,6 +96,19 @@ class PHPExcel_Worksheet_RowIterator implements Iterator
|
||||
public function resetStart($startRow = 1) {
|
||||
$this->_startRow = $startRow;
|
||||
$this->seek($startRow);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* (Re)Set the end row
|
||||
*
|
||||
* @param integer $endRow The row number at which to stop iterating
|
||||
*/
|
||||
public function resetEnd($endRow = null) {
|
||||
$this->_endRow = ($endRow) ? $endRow : $this->_subject->getHighestRow();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,6 +117,10 @@ class PHPExcel_Worksheet_RowIterator implements Iterator
|
||||
* @param integer $row The row number to set the current pointer at
|
||||
*/
|
||||
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})");
|
||||
}
|
||||
|
||||
$this->_position = $row;
|
||||
}
|
||||
|
||||
@ -133,16 +160,19 @@ class PHPExcel_Worksheet_RowIterator implements Iterator
|
||||
* Set the iterator to its previous value
|
||||
*/
|
||||
public function prev() {
|
||||
if ($this->_position > 1)
|
||||
if ($this->_position <= $this->_startRow) {
|
||||
throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->_startRow} - {$this->_endRow})");
|
||||
}
|
||||
|
||||
--$this->_position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate if more rows exist in the worksheet
|
||||
* Indicate if more rows exist in the worksheet range of rows that we're iterating
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function valid() {
|
||||
return $this->_position <= $this->_subject->getHighestRow();
|
||||
return $this->_position <= $this->_endRow;
|
||||
}
|
||||
}
|
||||
|
87
unitTests/Classes/PHPExcel/Worksheet/RowIteratorTest.php
Normal file
87
unitTests/Classes/PHPExcel/Worksheet/RowIteratorTest.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
class RowIteratorTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public $mockWorksheet;
|
||||
public $mockRow;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
|
||||
}
|
||||
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
|
||||
$this->mockRow = $this->getMockBuilder('PHPExcel_Worksheet_Row')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->mockWorksheet->expects($this->any())
|
||||
->method('getHighestRow')
|
||||
->will($this->returnValue(5));
|
||||
$this->mockWorksheet->expects($this->any())
|
||||
->method('current')
|
||||
->will($this->returnValue($this->mockRow));
|
||||
}
|
||||
|
||||
|
||||
public function testIteratorFullRange()
|
||||
{
|
||||
$iterator = new PHPExcel_Worksheet_RowIterator($this->mockWorksheet);
|
||||
$rowIndexResult = 1;
|
||||
$this->assertEquals($rowIndexResult, $iterator->key());
|
||||
|
||||
foreach($iterator as $key => $row) {
|
||||
$this->assertEquals($rowIndexResult++, $key);
|
||||
$this->assertInstanceOf('PHPExcel_Worksheet_Row', $row);
|
||||
}
|
||||
}
|
||||
|
||||
public function testIteratorStartEndRange()
|
||||
{
|
||||
$iterator = new PHPExcel_Worksheet_RowIterator($this->mockWorksheet, 2, 4);
|
||||
$rowIndexResult = 2;
|
||||
$this->assertEquals($rowIndexResult, $iterator->key());
|
||||
|
||||
foreach($iterator as $key => $row) {
|
||||
$this->assertEquals($rowIndexResult++, $key);
|
||||
$this->assertInstanceOf('PHPExcel_Worksheet_Row', $row);
|
||||
}
|
||||
}
|
||||
|
||||
public function testIteratorSeekAndPrev()
|
||||
{
|
||||
$iterator = new PHPExcel_Worksheet_RowIterator($this->mockWorksheet);
|
||||
$columnIndexResult = 4;
|
||||
$iterator->seek(4);
|
||||
$this->assertEquals($columnIndexResult, $iterator->key(), 2, 4);
|
||||
|
||||
for($i = 1; $i < $columnIndexResult; $i++) {
|
||||
$iterator->prev();
|
||||
$this->assertEquals($columnIndexResult - $i, $iterator->key());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PHPExcel_Exception
|
||||
*/
|
||||
public function testSeekOutOfRange()
|
||||
{
|
||||
$iterator = new PHPExcel_Worksheet_RowIterator($this->mockWorksheet, 2, 4);
|
||||
$iterator->seek(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PHPExcel_Exception
|
||||
*/
|
||||
public function testPrevOutOfRange()
|
||||
{
|
||||
$iterator = new PHPExcel_Worksheet_RowIterator($this->mockWorksheet, 2, 4);
|
||||
$iterator->prev();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user