Skip to content

Commit

Permalink
rename EntityFactory for writers and readers box#526
Browse files Browse the repository at this point in the history
  • Loading branch information
madflow authored and adrilo committed Sep 3, 2018
1 parent e1acdc1 commit 8a1c48b
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 97 deletions.
20 changes: 10 additions & 10 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Finally, **_Spout 3.0 only supports PHP 7.1 and above_**, as other PHP versions

Reader changes
--------------
Creating a reader should now be done through the Reader `EntityFactory`, instead of using the `ReaderFactory`:
Creating a reader should now be done through the Reader `ReaderEntityFactory`, instead of using the `ReaderFactory`:
```php
use Box\Spout\Reader\Common\Creator\EntityFactory; // namespace is no longer "Box\Spout\Reader"
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory; // namespace is no longer "Box\Spout\Reader"
...
$reader = EntityFactory::createReader(Type::XLSX);
$reader = ReaderEntityFactory::createReader(Type::XLSX);
```

When iterating over the spreadsheet rows, Spout now returns `Row` objects, instead of an array containing row values. Accessing the row values should now be done this way:
Expand All @@ -37,23 +37,23 @@ foreach ($reader->getSheetIterator() as $sheet) {

Writer changes
--------------
Writer creation follows the same change as the reader. It should now be done through the Writer `EntityFactory`, instead of using the `WriterFactory`:
Writer creation follows the same change as the reader. It should now be done through the Writer `WriterEntityFactory`, instead of using the `WriterFactory`:
```php
use Box\Spout\Writer\Common\Creator\EntityFactory; // namespace is no longer "Box\Spout\Writer"
use Box\Spout\Writer\Common\Creator\WriterEntityFactory; // namespace is no longer "Box\Spout\Writer"
...
$writer = EntityFactory::createWriter(Type::ODS);
$writer = WriterEntityFactory::createWriter(Type::ODS);
```

Adding rows is also done differently: instead of passing an array, the writer now takes in a `Row` object (or an array of `Row`). Creating such objects can easily be done this way:
```php
// Adding a row from an array of values (2.x equivalent)
$cellValues = ['foo', 12345];
$row1 = EntityFactory::createRowFromArray($cellValues, $rowStyle);
$row1 = WriterEntityFactory::createRowFromArray($cellValues, $rowStyle);

// Adding a row from an array of Cell
$cell1 = EntityFactory::createCell('foo', $cellStyle1); // this cell has its own style
$cell2 = EntityFactory::createCell(12345, $cellStyle2); // this cell has its own style
$row2 = EntityFactory::createRow([$cell1, $cell2]);
$cell1 = WriterEntityFactory::createCell('foo', $cellStyle1); // this cell has its own style
$cell2 = WriterEntityFactory::createCell(12345, $cellStyle2); // this cell has its own style
$row2 = WriterEntityFactory::createRow([$cell1, $cell2]);

$writer->addRows([$row1, $row2]);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Box\Spout\Reader\ReaderInterface;

/**
* Class EntityFactory
* Class ReaderEntityFactory
* Factory to create external entities
*/
class EntityFactory
class ReaderEntityFactory
{
/**
* This creates an instance of the appropriate reader, given the type of the file to be read
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Reader/ODS/Creator/HelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Box\Spout\Reader\ODS\Helper\SettingsHelper;

/**
* Class EntityFactory
* Class HelperFactory
* Factory to create helpers
*/
class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Box\Spout\Reader\XLSX\SheetIterator;

/**
* Class EntityFactory
* Class InternalEntityFactory
* Factory to create entities
*/
class InternalEntityFactory implements InternalEntityFactoryInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Box\Spout\Writer\WriterInterface;

/**
* Class EntityFactory
* Class WriterEntityFactory
* Factory to create external entities
*/
class EntityFactory
class WriterEntityFactory
{
/**
* This creates an instance of the appropriate writer, given the type of the file to be written
Expand Down
4 changes: 2 additions & 2 deletions tests/Spout/Reader/CSV/SheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Box\Spout\Reader\CSV;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\EntityFactory;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -33,7 +33,7 @@ public function testReaderShouldReturnCorrectSheetInfos()
private function openFileAndReturnSheet($fileName)
{
$resourcePath = $this->getResourcePath($fileName);
$reader = EntityFactory::createReader(Type::CSV);
$reader = ReaderEntityFactory::createReader(Type::CSV);
$reader->open($resourcePath);

$sheet = $reader->getSheetIterator()->current();
Expand Down
12 changes: 6 additions & 6 deletions tests/Spout/Reader/ODS/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\EntityFactory;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\Reader\Exception\IteratorNotRewindableException;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -354,7 +354,7 @@ public function testReadShouldThrowIfTryingToRewindRowIterator()
$this->expectException(IteratorNotRewindableException::class);

$resourcePath = $this->getResourcePath('one_sheet_with_strings.ods');
$reader = EntityFactory::createReader(Type::ODS);
$reader = ReaderEntityFactory::createReader(Type::ODS);
$reader->open($resourcePath);

foreach ($reader->getSheetIterator() as $sheet) {
Expand All @@ -379,7 +379,7 @@ public function testReadMultipleTimesShouldRewindReader()
$resourcePath = $this->getResourcePath('two_sheets_with_strings.ods');

/** @var Reader $reader */
$reader = EntityFactory::createReader(Type::ODS);
$reader = ReaderEntityFactory::createReader(Type::ODS);
$reader->open($resourcePath);

foreach ($reader->getSheetIterator() as $sheet) {
Expand Down Expand Up @@ -423,7 +423,7 @@ public function testReadWithUnsupportedCustomStreamWrapper()
$this->expectException(IOException::class);

/** @var \Box\Spout\Reader\ODS\Reader $reader */
$reader = EntityFactory::createReader(Type::ODS);
$reader = ReaderEntityFactory::createReader(Type::ODS);
$reader->open('unsupported://foobar');
}

Expand All @@ -435,7 +435,7 @@ public function testReadWithSupportedCustomStreamWrapper()
$this->expectException(IOException::class);

/** @var \Box\Spout\Reader\ODS\Reader $reader */
$reader = EntityFactory::createReader(Type::ODS);
$reader = ReaderEntityFactory::createReader(Type::ODS);
$reader->open('php://memory');
}

Expand Down Expand Up @@ -531,7 +531,7 @@ private function getAllRowsForFile($fileName, $shouldFormatDates = false, $shoul
$resourcePath = $this->getResourcePath($fileName);

/** @var \Box\Spout\Reader\ODS\Reader $reader */
$reader = EntityFactory::createReader(Type::ODS);
$reader = ReaderEntityFactory::createReader(Type::ODS);
$reader->setShouldFormatDates($shouldFormatDates);
$reader->setShouldPreserveEmptyRows($shouldPreserveEmptyRows);
$reader->open($resourcePath);
Expand Down
4 changes: 2 additions & 2 deletions tests/Spout/Reader/ODS/SheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Box\Spout\Reader\ODS;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\EntityFactory;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -61,7 +61,7 @@ public function testReaderShouldReturnCorrectSheetVisibility()
private function openFileAndReturnSheets($fileName)
{
$resourcePath = $this->getResourcePath($fileName);
$reader = EntityFactory::createReader(Type::ODS);
$reader = ReaderEntityFactory::createReader(Type::ODS);
$reader->open($resourcePath);

$sheets = [];
Expand Down
10 changes: 5 additions & 5 deletions tests/Spout/Reader/XLSX/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\EntityFactory;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -570,7 +570,7 @@ public function testReadMultipleTimesShouldRewindReader()
$allRows = [];
$resourcePath = $this->getResourcePath('two_sheets_with_inline_strings.xlsx');

$reader = EntityFactory::createReader(Type::XLSX);
$reader = ReaderEntityFactory::createReader(Type::XLSX);
$reader->open($resourcePath);

foreach ($reader->getSheetIterator() as $sheet) {
Expand Down Expand Up @@ -624,7 +624,7 @@ public function testReadWithUnsupportedCustomStreamWrapper()
$this->expectException(IOException::class);

/** @var \Box\Spout\Reader\XLSX\Reader $reader */
$reader = EntityFactory::createReader(Type::XLSX);
$reader = ReaderEntityFactory::createReader(Type::XLSX);
$reader->open('unsupported://foobar');
}

Expand All @@ -636,7 +636,7 @@ public function testReadWithSupportedCustomStreamWrapper()
$this->expectException(IOException::class);

/** @var \Box\Spout\Reader\XLSX\Reader $reader */
$reader = EntityFactory::createReader(Type::XLSX);
$reader = ReaderEntityFactory::createReader(Type::XLSX);
$reader->open('php://memory');
}

Expand Down Expand Up @@ -701,7 +701,7 @@ private function getAllRowsForFile($fileName, $shouldFormatDates = false, $shoul
$resourcePath = $this->getResourcePath($fileName);

/** @var \Box\Spout\Reader\XLSX\Reader $reader */
$reader = EntityFactory::createReader(Type::XLSX);
$reader = ReaderEntityFactory::createReader(Type::XLSX);
$reader->setShouldFormatDates($shouldFormatDates);
$reader->setShouldPreserveEmptyRows($shouldPreserveEmptyRows);
$reader->open($resourcePath);
Expand Down
4 changes: 2 additions & 2 deletions tests/Spout/Reader/XLSX/SheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Box\Spout\Reader\XLSX;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\EntityFactory;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -49,7 +49,7 @@ public function testReaderShouldReturnCorrectSheetVisibility()
private function openFileAndReturnSheets($fileName)
{
$resourcePath = $this->getResourcePath($fileName);
$reader = EntityFactory::createReader(Type::XLSX);
$reader = ReaderEntityFactory::createReader(Type::XLSX);
$reader->open($resourcePath);

$sheets = [];
Expand Down
14 changes: 7 additions & 7 deletions tests/Spout/Writer/CSV/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper;
use PHPUnit\Framework\TestCase;
Expand All @@ -32,7 +32,7 @@ public function testWriteShouldThrowExceptionIfCannotOpenFileForWriting()
$this->createUnwritableFolderIfNeeded();
$filePath = $this->getGeneratedUnwritableResourcePath($fileName);

$writer = EntityFactory::createWriter(Type::CSV);
$writer = WriterEntityFactory::createWriter(Type::CSV);
@$writer->openToFile($filePath);
$writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close();
Expand All @@ -45,7 +45,7 @@ public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{
$this->expectException(WriterNotOpenedException::class);

$writer = EntityFactory::createWriter(Type::CSV);
$writer = WriterEntityFactory::createWriter(Type::CSV);
$writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close();
}
Expand All @@ -57,7 +57,7 @@ public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter()
{
$this->expectException(WriterNotOpenedException::class);

$writer = EntityFactory::createWriter(Type::CSV);
$writer = WriterEntityFactory::createWriter(Type::CSV);
$writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close();
}
Expand All @@ -69,7 +69,7 @@ public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
{
$this->expectException(InvalidArgumentException::class);

$writer = EntityFactory::createWriter(Type::CSV);
$writer = WriterEntityFactory::createWriter(Type::CSV);
$writer->addRows([['csv--11', 'csv--12']]);
$writer->close();
}
Expand All @@ -83,7 +83,7 @@ public function testCloseShouldNoopWhenWriterIsNotOpened()
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);

$writer = EntityFactory::createWriter(Type::CSV);
$writer = WriterEntityFactory::createWriter(Type::CSV);
$writer->close(); // This call should not cause any error

$writer->openToFile($resourcePath);
Expand Down Expand Up @@ -202,7 +202,7 @@ private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fie
$resourcePath = $this->getGeneratedResourcePath($fileName);

/** @var \Box\Spout\Writer\CSV\Writer $writer */
$writer = EntityFactory::createWriter(Type::CSV);
$writer = WriterEntityFactory::createWriter(Type::CSV);
$writer->setFieldDelimiter($fieldDelimiter);
$writer->setFieldEnclosure($fieldEnclosure);
$writer->setShouldAddBOM($shouldAddBOM);
Expand Down
10 changes: 5 additions & 5 deletions tests/Spout/Writer/ODS/SheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Exception\InvalidSheetNameException;
use Box\Spout\Writer\RowCreationHelper;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testSetSheetNameShouldThrowWhenNameIsAlreadyUsed()
$resourcePath = $this->getGeneratedResourcePath($fileName);

/** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = EntityFactory::createWriter(Type::ODS);
$writer = WriterEntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath);

$customSheetName = 'Sheet name';
Expand Down Expand Up @@ -105,7 +105,7 @@ private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
$resourcePath = $this->getGeneratedResourcePath($fileName);

/** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = EntityFactory::createWriter(Type::ODS);
$writer = WriterEntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath);

$sheet = $writer->getCurrentSheet();
Expand All @@ -125,7 +125,7 @@ private function writeDataToMulitpleSheetsAndReturnSheets($fileName)
$resourcePath = $this->getGeneratedResourcePath($fileName);

/** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = EntityFactory::createWriter(Type::ODS);
$writer = WriterEntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath);

$writer->addRow($this->createRowFromValues(['ods--sheet1--11', 'ods--sheet1--12']));
Expand All @@ -147,7 +147,7 @@ private function writeDataToHiddenSheet($fileName)
$resourcePath = $this->getGeneratedResourcePath($fileName);

/** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = EntityFactory::createWriter(Type::ODS);
$writer = WriterEntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath);

$sheet = $writer->getCurrentSheet();
Expand Down
Loading

0 comments on commit 8a1c48b

Please sign in to comment.