Rename *Number to *Index

This commit is contained in:
Adrien Loison 2015-04-29 10:48:31 -07:00
parent c6e943041e
commit cfd3e0ffa3
12 changed files with 134 additions and 108 deletions

View File

@ -14,7 +14,7 @@ use Box\Spout\Reader\Exception\EndOfFileReachedException;
*/ */
abstract class AbstractReader implements ReaderInterface abstract class AbstractReader implements ReaderInterface
{ {
/** @var int Used to keep track of the row number */ /** @var int Used to keep track of the row index */
protected $currentRowIndex = 0; protected $currentRowIndex = 0;
/** @var bool Indicates whether the stream is currently open */ /** @var bool Indicates whether the stream is currently open */

View File

@ -91,12 +91,12 @@ class WorksheetHelper
* default to the data sheet XML file name ("xl/worksheets/sheet2.xml" => "sheet2"). * default to the data sheet XML file name ("xl/worksheets/sheet2.xml" => "sheet2").
* *
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param int $sheetNumberZeroBased Index of the sheet, based on order in [Content_Types].xml (zero-based) * @param int $sheetIndexZeroBased Index of the sheet, based on order in [Content_Types].xml (zero-based)
* @return \Box\Spout\Reader\Sheet Sheet instance * @return \Box\Spout\Reader\Sheet Sheet instance
*/ */
protected function getSheet($sheetDataXMLFilePath, $sheetNumberZeroBased) protected function getSheet($sheetDataXMLFilePath, $sheetIndexZeroBased)
{ {
$sheetId = $sheetNumberZeroBased + 1; $sheetId = $sheetIndexZeroBased + 1;
$sheetName = $this->getDefaultSheetName($sheetDataXMLFilePath); $sheetName = $this->getDefaultSheetName($sheetDataXMLFilePath);
/* /*
@ -126,7 +126,7 @@ class WorksheetHelper
} }
} }
return new Sheet($sheetId, $sheetNumberZeroBased, $sheetName); return new Sheet($sheetId, $sheetIndexZeroBased, $sheetName);
} }
/** /**
@ -204,6 +204,6 @@ class WorksheetHelper
*/ */
public function hasNextWorksheet($currentWorksheet, $allWorksheets) public function hasNextWorksheet($currentWorksheet, $allWorksheets)
{ {
return ($currentWorksheet === null || ($currentWorksheet->getWorksheetNumber() + 1 < count($allWorksheets))); return ($currentWorksheet === null || ($currentWorksheet->getWorksheetIndex() + 1 < count($allWorksheets)));
} }
} }

View File

@ -13,21 +13,21 @@ class Worksheet
/** @var \Box\Spout\Reader\Sheet The "external" sheet */ /** @var \Box\Spout\Reader\Sheet The "external" sheet */
protected $externalSheet; protected $externalSheet;
/** @var int Worksheet number, based on the order of appareance in [Content_Types].xml (zero-based) */ /** @var int Worksheet index, based on the order of appareance in [Content_Types].xml (zero-based) */
protected $worksheetNumber; protected $worksheetIndex;
/** @var string Path of the XML file containing the worksheet data */ /** @var string Path of the XML file containing the worksheet data */
protected $dataXmlFilePath; protected $dataXmlFilePath;
/**\ /**\
* @param \Box\Spout\Reader\Sheet $externalSheet The associated "external" sheet * @param \Box\Spout\Reader\Sheet $externalSheet The associated "external" sheet
* @param int $worksheetNumber Worksheet number, based on the order of appareance in [Content_Types].xml (zero-based) * @param int $worksheetIndex Worksheet index, based on the order of appareance in [Content_Types].xml (zero-based)
* @param string $dataXmlFilePath Path of the XML file containing the worksheet data * @param string $dataXmlFilePath Path of the XML file containing the worksheet data
*/ */
public function __construct($externalSheet, $worksheetNumber, $dataXmlFilePath) public function __construct($externalSheet, $worksheetIndex, $dataXmlFilePath)
{ {
$this->externalSheet = $externalSheet; $this->externalSheet = $externalSheet;
$this->worksheetNumber = $worksheetNumber; $this->worksheetIndex = $worksheetIndex;
$this->dataXmlFilePath = $dataXmlFilePath; $this->dataXmlFilePath = $dataXmlFilePath;
} }
@ -50,8 +50,8 @@ class Worksheet
/** /**
* @return int * @return int
*/ */
public function getWorksheetNumber() public function getWorksheetIndex()
{ {
return $this->worksheetNumber; return $this->worksheetIndex;
} }
} }

View File

@ -13,21 +13,21 @@ class Sheet
/** @var int ID of the sheet */ /** @var int ID of the sheet */
protected $id; protected $id;
/** @var int Number of the sheet, based on order of creation (zero-based) */ /** @var int Index of the sheet, based on order of creation (zero-based) */
protected $number; protected $index;
/** @var string Name of the sheet */ /** @var string Name of the sheet */
protected $name; protected $name;
/** /**
* @param int $sheetId ID of the sheet * @param int $sheetId ID of the sheet
* @param int $sheetNumber Number of the sheet, based on order of creation (zero-based) * @param int $sheetIndex Index of the sheet, based on order of creation (zero-based)
* @param string $sheetName Name of the sheet * @param string $sheetName Name of the sheet
*/ */
function __construct($sheetId, $sheetNumber, $sheetName) function __construct($sheetId, $sheetIndex, $sheetName)
{ {
$this->id = $sheetId; $this->id = $sheetId;
$this->number = $sheetNumber; $this->index = $sheetIndex;
$this->name = $sheetName; $this->name = $sheetName;
} }
@ -40,11 +40,11 @@ class Sheet
} }
/** /**
* @return int Number of the sheet, based on order of creation (zero-based) * @return int Index of the sheet, based on order of creation (zero-based)
*/ */
public function getNumber() public function getIndex()
{ {
return $this->number; return $this->index;
} }
/** /**

View File

@ -132,8 +132,8 @@ class XLSX extends AbstractReader
if ($this->currentWorksheet === null) { if ($this->currentWorksheet === null) {
$nextWorksheet = $this->worksheets[0]; $nextWorksheet = $this->worksheets[0];
} else { } else {
$currentWorksheetNumber = $this->currentWorksheet->getWorksheetNumber(); $currentWorksheetIndex = $this->currentWorksheet->getWorksheetIndex();
$nextWorksheet = $this->worksheets[$currentWorksheetNumber + 1]; $nextWorksheet = $this->worksheets[$currentWorksheetIndex + 1];
} }
$this->initXmlReaderForWorksheetData($nextWorksheet); $this->initXmlReaderForWorksheetData($nextWorksheet);

View File

@ -67,8 +67,8 @@ class Workbook
*/ */
public function addNewSheet() public function addNewSheet()
{ {
$newSheetNumber = count($this->worksheets); $newSheetIndex = count($this->worksheets);
$sheet = new Sheet($newSheetNumber); $sheet = new Sheet($newSheetIndex);
$worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder();
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings); $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings);

View File

@ -96,8 +96,8 @@ EOD;
*/ */
public function getId() public function getId()
{ {
// sheet number is zero-based, while ID is 1-based // sheet index is zero-based, while ID is 1-based
return $this->externalSheet->getNumber() + 1; return $this->externalSheet->getIndex() + 1;
} }
/** /**

View File

@ -12,27 +12,27 @@ class Sheet
{ {
const DEFAULT_SHEET_NAME_PREFIX = 'Sheet'; const DEFAULT_SHEET_NAME_PREFIX = 'Sheet';
/** @var int Number of the sheet, based on order of creation (zero-based) */ /** @var int Index of the sheet, based on order of creation (zero-based) */
protected $sheetNumber; protected $index;
/** @var string Name of the sheet */ /** @var string Name of the sheet */
protected $name; protected $name;
/** /**
* @param $sheetNumber Number of the sheet, based on order of creation (zero-based) * @param int $sheetIndex Index of the sheet, based on order of creation (zero-based)
*/ */
function __construct($sheetNumber) function __construct($sheetIndex)
{ {
$this->sheetNumber = $sheetNumber; $this->index = $sheetIndex;
$this->name = self::DEFAULT_SHEET_NAME_PREFIX . ($sheetNumber + 1); $this->name = self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1);
} }
/** /**
* @return int Number of the sheet, based on order of creation (zero-based) * @return int Index of the sheet, based on order of creation (zero-based)
*/ */
public function getNumber() public function getIndex()
{ {
return $this->sheetNumber; return $this->index;
} }
/** /**

View File

@ -0,0 +1,52 @@
<?php
namespace Box\Spout\Reader;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
/**
* Class SheetTest
*
* @package Box\Spout\Reader
*/
class SheetTest extends \PHPUnit_Framework_TestCase
{
use TestUsingResource;
/**
* @return void
*/
public function testNextSheetShouldReturnCorrectSheetInfos()
{
$sheets = $this->openFileAndReturnSheets('two_sheets_with_custom_names.xlsx');
$this->assertEquals('CustomName1', $sheets[0]->getName());
$this->assertEquals(0, $sheets[0]->getIndex());
$this->assertEquals(1, $sheets[0]->getId());
$this->assertEquals('CustomName2', $sheets[1]->getName());
$this->assertEquals(1, $sheets[1]->getIndex());
$this->assertEquals(2, $sheets[1]->getId());
}
/**
* @param string $fileName
* @return Sheet[]
*/
private function openFileAndReturnSheets($fileName)
{
$resourcePath = $this->getResourcePath($fileName);
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($resourcePath);
$sheets = [];
while ($reader->hasNextSheet()) {
$sheets[] = $reader->nextSheet();
}
$reader->close();
return $sheets;
}
}

View File

@ -200,32 +200,6 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
$this->assertEquals([], $allRows, 'Sheet with no cells should be correctly processed.'); $this->assertEquals([], $allRows, 'Sheet with no cells should be correctly processed.');
} }
/**
* @return void
*/
public function testNextSheetShouldReturnCorrectSheetInfos()
{
$resourcePath = $this->getResourcePath('two_sheets_with_custom_names.xlsx');
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($resourcePath);
/** @var \Box\Spout\Reader\Sheet[] $sheets */
$sheets = [];
while ($reader->hasNextSheet()) {
$sheets[] = $reader->nextSheet();
}
$reader->close();
$this->assertEquals('CustomName1', $sheets[0]->getName());
$this->assertEquals(0, $sheets[0]->getNumber());
$this->assertEquals(1, $sheets[0]->getId());
$this->assertEquals('CustomName2', $sheets[1]->getName());
$this->assertEquals(1, $sheets[1]->getNumber());
$this->assertEquals(2, $sheets[1]->getId());
}
/** /**
* @param string $fileName * @param string $fileName
* @return array All the read rows the given file * @return array All the read rows the given file

View File

@ -17,13 +17,13 @@ class SheetTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testGetSheetNumber() public function testGetSheetIndex()
{ {
$sheets = $this->writeDataAndReturnSheets('test_get_sheet_number.xlsx'); $sheets = $this->writeDataAndReturnSheets('test_get_sheet_index.xlsx');
$this->assertEquals(2, count($sheets), '2 sheets should have been created'); $this->assertEquals(2, count($sheets), '2 sheets should have been created');
$this->assertEquals(0, $sheets[0]->getNumber(), 'The first sheet should be number 0'); $this->assertEquals(0, $sheets[0]->getIndex(), 'The first sheet should be index 0');
$this->assertEquals(1, $sheets[1]->getNumber(), 'The second sheet should be number 1'); $this->assertEquals(1, $sheets[1]->getIndex(), 'The second sheet should be index 1');
} }
/** /**
@ -38,6 +38,28 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Sheet2', $sheets[1]->getName(), 'Invalid name for the second sheet'); $this->assertEquals('Sheet2', $sheets[1]->getName(), 'Invalid name for the second sheet');
} }
/**
* @return void
*/
public function testSetSheetNameShouldCreateSheetWithCustomName()
{
$fileName = 'test_set_name_should_create_sheet_with_custom_name.xlsx';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath);
$customSheetName = 'CustomName';
$sheet = $writer->getCurrentSheet();
$sheet->setName($customSheetName);
$writer->addRow(['xlsx--11', 'xlsx--12']);
$writer->close();
$this->assertSheetNameEquals($customSheetName, $resourcePath, "The sheet name should have been changed to '$customSheetName'");
}
/** /**
* @param string $fileName * @param string $fileName
* @return Sheet[] * @return Sheet[]
@ -59,4 +81,18 @@ class SheetTest extends \PHPUnit_Framework_TestCase
return $writer->getSheets(); return $writer->getSheets();
} }
/**
* @param string $expectedName
* @param string $resourcePath
* @param string $message
* @return void
*/
private function assertSheetNameEquals($expectedName, $resourcePath, $message = '')
{
$pathToWorkbookFile = $resourcePath . '#xl/workbook.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains('<sheet name="' . $expectedName . '"', $xmlContents, $message);
}
} }

View File

@ -343,28 +343,6 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 'control&#039;s _x0015_ &quot;character&quot;'); $this->assertInlineDataWasWrittenToSheet($fileName, 1, 'control&#039;s _x0015_ &quot;character&quot;');
} }
/**
* @return void
*/
public function testSetNameShouldCreateSheetWithCustomName()
{
$fileName = 'test_set_name_should_create_sheet_with_custom_name.xlsx';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath);
$customSheetName = 'CustomName';
$sheet = $writer->getCurrentSheet();
$sheet->setName($customSheetName);
$writer->addRow(['xlsx--11', 'xlsx--12']);
$writer->close();
$this->assertSheetNameEquals($customSheetName, $resourcePath, "The sheet name should have been changed to '$customSheetName'");
}
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
@ -422,15 +400,15 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
/** /**
* @param string $fileName * @param string $fileName
* @param int $sheetNumber * @param int $sheetIndex
* @param mixed $inlineData * @param mixed $inlineData
* @param string $message * @param string $message
* @return void * @return void
*/ */
private function assertInlineDataWasWrittenToSheet($fileName, $sheetNumber, $inlineData, $message = '') private function assertInlineDataWasWrittenToSheet($fileName, $sheetIndex, $inlineData, $message = '')
{ {
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetNumber . '.xml'; $pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetIndex . '.xml';
$xmlContents = file_get_contents('zip://' . $pathToSheetFile); $xmlContents = file_get_contents('zip://' . $pathToSheetFile);
$this->assertContains((string)$inlineData, $xmlContents, $message); $this->assertContains((string)$inlineData, $xmlContents, $message);
@ -438,15 +416,15 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
/** /**
* @param string $fileName * @param string $fileName
* @param int $sheetNumber * @param int $sheetIndex
* @param mixed $inlineData * @param mixed $inlineData
* @param string $message * @param string $message
* @return void * @return void
*/ */
private function assertInlineDataWasNotWrittenToSheet($fileName, $sheetNumber, $inlineData, $message = '') private function assertInlineDataWasNotWrittenToSheet($fileName, $sheetIndex, $inlineData, $message = '')
{ {
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetNumber . '.xml'; $pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetIndex . '.xml';
$xmlContents = file_get_contents('zip://' . $pathToSheetFile); $xmlContents = file_get_contents('zip://' . $pathToSheetFile);
$this->assertNotContains((string)$inlineData, $xmlContents, $message); $this->assertNotContains((string)$inlineData, $xmlContents, $message);
@ -466,18 +444,4 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
$this->assertContains($sharedString, $xmlContents, $message); $this->assertContains($sharedString, $xmlContents, $message);
} }
/**
* @param string $expectedName
* @param string $resourcePath
* @param string $message
* @return void
*/
private function assertSheetNameEquals($expectedName, $resourcePath, $message = '')
{
$pathToWorkbookFile = $resourcePath . '#xl/workbook.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains('<sheet name="' . $expectedName . '"', $xmlContents, $message);
}
} }