Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Commit

Permalink
Upgrade to Box Spout 3 and prepare for v2 (#6)
Browse files Browse the repository at this point in the history
* Dropped support for PHP 7.1
* Added support for PHP 8
* Upgraded Box Spout to v3
  • Loading branch information
nikazooz authored Mar 20, 2021
1 parent 8110f33 commit 917080f
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 42 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4]
php: [7.2, 7.3, 7.4, 8.0]
laravel: [8, 7, 6, 5.8, 5.7, 5.6, 5.5]
dependency-version: [prefer-stable]
os: [ubuntu-latest]
Expand All @@ -36,12 +36,14 @@ jobs:
exclude:
- laravel: 8
php: 7.2
- laravel: 8
php: 7.1
- laravel: 7
php: 7.1
- laravel: 6
php: 7.1
- laravel: 5.8
php: 8.0
- laravel: 5.7
php: 8.0
- laravel: 5.6
php: 8.0
- laravel: 5.5
php: 8.0

name: PHP${{ matrix.php }} - L${{ matrix.laravel }}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Versions will be supported for a limited amount of time.
| Version | Laravel Version | Php Version | Box Spout | Support |
| ------- | --------------- | ----------- | --------- | ---------------------- |
| 1.* | 5.5 - 8.* | ^7.1 | ^2.7 | Bug and security fixes |
| 2.* | 5.5 - 8.* | >=7.2 | ^3.2 | New features |
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
],
"require": {
"ext-json": "*",
"php": "^7.1",
"box/spout": "^2.7",
"php": "^7.2|^8.0",
"box/spout": "^3.2",
"illuminate/support": "^5.5|^6.0|^7.0|^8.0"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions src/Factories/ReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Nikazooz\Simplesheet\Factories;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\ReaderFactory as SpoutReaderFactory;
use Box\Spout\Reader\CSV\Reader as CsvReader;
use Box\Spout\Reader\ReaderFactory as SpoutReaderFactory;
use Box\Spout\Reader\ReaderInterface;
use Nikazooz\Simplesheet\Concerns\MapsCsvSettings;
use Nikazooz\Simplesheet\Concerns\WithCustomCsvSettings;
Expand All @@ -31,12 +31,12 @@ public static function make($type, $import): ReaderInterface
protected static function makeUnconfiguredReader($type)
{
if (Simplesheet::TSV === $type) {
return SpoutReaderFactory::create(Type::CSV)
return SpoutReaderFactory::createFromType(Type::CSV)
->setFieldDelimiter("\t")
->setShouldPreserveEmptyRows(true);
}

return SpoutReaderFactory::create($type)->setShouldPreserveEmptyRows(true);
return SpoutReaderFactory::createFromType($type)->setShouldPreserveEmptyRows(true);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Factories/WriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Nikazooz\Simplesheet\Factories;

use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Writer\WriterFactory as SpoutWriterFactory;
use Box\Spout\Reader\CSV\Manager\OptionsManager;
use Box\Spout\Writer\Common\Creator\WriterFactory as SpoutWriterFactory;
use Box\Spout\Writer\WriterInterface;
use Nikazooz\Simplesheet\Concerns\MapsCsvSettings;
use Nikazooz\Simplesheet\Concerns\WithCustomCsvSettings;
Expand All @@ -30,15 +32,19 @@ public static function make($type, $export): WriterInterface
return static::makeCsvWriter($type, $export);
}

return SpoutWriterFactory::create($type);
return SpoutWriterFactory::createFromType($type);
}

/**
* @return \Nikazooz\Simplesheet\Writers\CsvWriter
*/
protected static function makeCsvWriter($type, $export): WriterInterface
{
$writer = (new CsvWriter())->setGlobalFunctionsHelper(new GlobalFunctionsHelper());
$writer = (new CsvWriter(
new OptionsManager(),
new GlobalFunctionsHelper(),
new HelperFactory()
));

static::applyCsvSettings(static::getCsvConfig());

Expand Down
2 changes: 1 addition & 1 deletion src/Imports/HeadingRowExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function extract(SheetInterface $sheet, $importable): array
break;
}

$rows[] = $row;
$rows[] = $row->toArray();
}

$headingRow = head($rows);
Expand Down
2 changes: 1 addition & 1 deletion src/Imports/ProcessesRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function iterateRowsBetween($sheet, int $startRow, int $endRow = null)
break;
}

yield $rowNumber => $row;
yield $rowNumber => $row->toArray();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Simplesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function raw($export, string $writerType)
* @param object $export
* @param string $fileName
* @param string|null $writerType
* @return string
* @return \Nikazooz\Simplesheet\Files\TemporaryFile
*
* @throws \Nikazooz\Simplesheet\NoTypeDetectedException
*/
Expand Down
42 changes: 26 additions & 16 deletions src/Writers/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Nikazooz\Simplesheet\Writers;

use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\CSV\Writer;

class CsvWriter extends Writer
Expand Down Expand Up @@ -75,42 +77,40 @@ protected function openWriter()
if ($this->includeSeparatorLine) {
$this->globalFunctionsHelper->fputs(
$this->filePointer,
'sep='.$this->fieldDelimiter.$this->lineEnding
'sep='.$this->getFieldDelimiter().$this->lineEnding
);
}
}

/**
* Adds data to the currently opened writer.
*
* @param array $dataRow Array containing data to be written.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param \Box\Spout\Common\Entity\Row $row
* @param \Box\Spout\Writer\Style\Style $style Ignored here since CSV does not support styling.
* @return void
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
*/
protected function addRowToWriter(array $dataRow, $style)
protected function addRowToWriter(Row $row)
{
if (PHP_EOL !== $this->lineEnding) {
return $this->customAddRowToWriter($dataRow);
return $this->customAddRowToWriter($row);
}

parent::addRowToWriter($dataRow, $style);
parent::addRowToWriter($row);
}

/**
* Adds data to the currently opened writer.
*
* @param array $dataRow Array containing data to be written.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param \Box\Spout\Common\Entity\Row $row
* @return void
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
*/
protected function customAddRowToWriter($dataRow)
protected function customAddRowToWriter(Row $row)
{
$wasWriteSuccessful = $this->globalFunctionsHelper->fputs(
$this->filePointer,
$this->prepareRowForWriting($dataRow)
$this->prepareRowForWriting($row)
);

if ($wasWriteSuccessful === false) {
Expand All @@ -124,14 +124,14 @@ protected function customAddRowToWriter($dataRow)
}

/**
* @param array $row
* @param \Box\Spout\Common\Entity\Row $row
* @return string
*/
protected function prepareRowForWriting($row)
protected function prepareRowForWriting(Row $row)
{
return implode($this->fieldDelimiter, array_map(function ($cell) {
return implode($this->getFieldDelimiter(), array_map(function ($cell) {
return $this->encloseString($cell);
}, $row)).$this->lineEnding;
}, $row->toArray())).$this->lineEnding;
}

/**
Expand All @@ -145,9 +145,19 @@ protected function encloseString($str)
}

return vsprintf('%s%s%s', [
$this->fieldEnclosure,
$this->getFieldEnclosure(),
addslashes($str),
$this->fieldEnclosure,
$this->getFieldEnclosure(),
]);
}

protected function getFieldDelimiter()
{
return $this->optionsManager->getOption(Options::FIELD_DELIMITER);
}

protected function getFieldEnclosure()
{
return $this->optionsManager->getOption(Options::FIELD_ENCLOSURE);
}
}
14 changes: 10 additions & 4 deletions src/Writers/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Nikazooz\Simplesheet\Writers;

use Box\Spout\Writer\AbstractMultiSheetsWriter;
use Box\Spout\Common\Entity\Cell;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Writer\WriterInterface;
use Box\Spout\Writer\WriterMultiSheetsAbstract;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Nikazooz\Simplesheet\Concerns\FromArray;
Expand Down Expand Up @@ -170,7 +172,7 @@ public static function mapArraybleRow($row)
{
// When dealing with eloquent models, we'll skip the relations
// as we won't be able to display them anyway.
if (method_exists($row, 'attributesToArray')) {
if (is_object($row) && method_exists($row, 'attributesToArray')) {
return $row->attributesToArray();
}

Expand All @@ -194,7 +196,11 @@ public static function mapArraybleRow($row)
*/
public function appendRow($row)
{
$this->spoutWriter->addRow($row);
$cells = array_map(function ($value) {
return new Cell($value);
}, $row);

$this->spoutWriter->addRow(new Row($cells, null));
}

/**
Expand Down Expand Up @@ -234,7 +240,7 @@ protected function multipleSheetsAreNotSupported()
*/
protected function multipleSheetsAreSupported()
{
return $this->spoutWriter instanceof AbstractMultiSheetsWriter;
return $this->spoutWriter instanceof WriterMultiSheetsAbstract;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/Concerns/FromCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public function can_export_with_multiple_sheets_from_collection()
foreach ($export->sheets() as $sheetIndex => $sheet) {
$worksheet = $this->getSheetByIndex($reader, $sheetIndex);

$this->assertEquals($sheet->collection()->toArray(), array_values(iterator_to_array($worksheet->getRowIterator())));
$this->assertEquals($sheet->collection()->toArray(), array_values(array_map(function ($row) {
return $row->toArray();
}, iterator_to_array($worksheet->getRowIterator()))));
$this->assertEquals($sheet->title(), $worksheet->getName());
}

Expand Down
8 changes: 5 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nikazooz\Simplesheet\Tests;

use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Reader\Common\Creator\ReaderFactory;
use Box\Spout\Reader\ReaderInterface;
use Closure;
use Illuminate\Contracts\Queue\Job;
Expand All @@ -24,7 +24,7 @@ class TestCase extends OrchestraTestCase
*/
public function read(string $filePath, string $writerType): ReaderInterface
{
$reader = ReaderFactory::create($writerType);
$reader = ReaderFactory::createFromType($writerType);

$reader->open($filePath);

Expand All @@ -43,7 +43,9 @@ protected function readAsArray(string $filePath, string $writerType, int $sheetI

$sheet = $this->getSheetByIndex($reader, $sheetIndex);

return array_values(iterator_to_array($sheet->getRowIterator()));
return array_values(array_map(function ($row) {
return $row->toArray();
}, iterator_to_array($sheet->getRowIterator())));
}

/**
Expand Down

1 comment on commit 917080f

@whernandez0989
Copy link

@whernandez0989 whernandez0989 commented on 917080f Mar 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello,
the first is when I use the chain method
metodo chian

error laravel chain

If you need any other reference about the error you can tell me and I will see how I get it. Thanks

Please sign in to comment.