Skip to content

Commit

Permalink
Merge pull request #12 from mohammedjammeh/feature/upgrade-to-laravel-6
Browse files Browse the repository at this point in the history
This package and its dependencies has been upgraded to use laravel 6.
  • Loading branch information
joaodman authored Mar 26, 2020
2 parents d9f8d70 + 7b74781 commit c52bec6
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 54 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
}
],
"require": {
"php": ">=5.4.0",
"box/spout": "^2.4",
"illuminate/database": "4.*|5.*",
"illuminate/support": "4.*|5.*"
"php": "^7.2.5",
"box/spout": "^3.1",
"illuminate/database": "^6.0.0",
"illuminate/support": "^6.0.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1",
"laravel/laravel": "~5.3"
"phpunit/phpunit": "^7.0",
"phpspec/phpspec": "^6.1.1",
"laravel/laravel": "^6.0.0"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 4 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
27 changes: 17 additions & 10 deletions src/Exporter/AbstractSpreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Cyberduck\LaravelExcel\Exporter;

use Illuminate\Support\Collection;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Illuminate\Database\Query\Builder;
use Cyberduck\LaravelExcel\Serialiser\BasicSerialiser;
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;
Expand Down Expand Up @@ -56,6 +56,8 @@ public function setSerialiser(SerialiserInterface $serialiser)

abstract public function getType();

abstract public function createWriter();

public function save($filename)
{
$writer = $this->create();
Expand All @@ -74,7 +76,7 @@ public function stream($filename)

protected function create()
{
$writer = WriterFactory::create($this->type);
$writer = $this->createWriter();
$this->callbacks->each(function ($elem) use (&$writer) {
call_user_func_array(array($writer, $elem[0]), $elem[1]);
});
Expand All @@ -85,22 +87,27 @@ protected function makeRows($writer)
{
$headerRow = $this->serialiser->getHeaderRow();
if (!empty($headerRow)) {
$writer->addRow($headerRow);
$row = WriterEntityFactory::createRowFromArray($headerRow);
$writer->addRow($row);
}
if ($this->data instanceof Builder) {
if (isset($this->chuncksize)) {
$this->data->chunk($this->chuncksize);
} else {
$data = $this->data->get();
foreach ($data as $record) {
$writer->addRow($this->serialiser->getData($record));
}
$this->addRowsDataToWriter($this->data->get(), $writer);
}
} else {
foreach ($this->data as $record) {
$writer->addRow($this->serialiser->getData($record));
}
$this->addRowsDataToWriter($this->data, $writer);
}
return $writer;
}

public function addRowsDataToWriter($data, $writer)
{
foreach ($data as $record) {
$recordData = $this->serialiser->getData($record);
$row = WriterEntityFactory::createRowFromArray($recordData);
$writer->addRow($row);
}
}
}
6 changes: 6 additions & 0 deletions src/Exporter/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
namespace Cyberduck\LaravelExcel\Exporter;

use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;

class Csv extends AbstractSpreadsheet
{
public function getType()
{
return Type::CSV;
}

public function createWriter()
{
return WriterEntityFactory::createCSVWriter();
}
}
6 changes: 6 additions & 0 deletions src/Exporter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
namespace Cyberduck\LaravelExcel\Exporter;

use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;

class Excel extends AbstractSpreadsheet
{
public function getType()
{
return Type::XLSX;
}

public function createWriter()
{
return WriterEntityFactory::createXLSXWriter();
}
}
6 changes: 6 additions & 0 deletions src/Exporter/OpenOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
namespace Cyberduck\LaravelExcel\Exporter;

use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;

class OpenOffice extends AbstractSpreadsheet
{
public function getType()
{
return Type::ODS;
}

public function createWriter()
{
return WriterEntityFactory::createODSWriter();
}
}
13 changes: 7 additions & 6 deletions src/Importer/AbstractSpreadsheet.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Cyberduck\LaravelExcel\Importer;

use Box\Spout\Reader\ReaderFactory;
use Illuminate\Database\Eloquent\Model;
use Cyberduck\LaravelExcel\Parser\BasicParser;
use Cyberduck\LaravelExcel\Contract\ParserInterface;
Expand Down Expand Up @@ -65,6 +64,8 @@ public function setModel(Model $model)

abstract public function getType();

abstract public function createReader();

public function getCollection()
{
$headers = false;
Expand All @@ -80,9 +81,9 @@ public function getCollection()

foreach ($sheet->getRowIterator() as $rowindex => $row) {
if ($rowindex == 1 && $this->hasHeaderRow) {
$headers = $row;
$headers = $row->toArray();
} else {
$data = $this->parser->transform($row, $headers);
$data = $this->parser->transform($row->toArray(), $headers);

if ($data !== false) {
if ($this->model) {
Expand Down Expand Up @@ -119,9 +120,9 @@ public function save($updateIfEquals = [])

foreach ($sheet->getRowIterator() as $rowindex => $row) {
if ($rowindex == 1 && $this->hasHeaderRow) {
$headers = $row;
$headers = $row->toArray();
} else {
$data = $this->parser->transform($row, $headers);
$data = $this->parser->transform($row->toArray(), $headers);
if ($data !== false) {
$relationships = [];
$when = array_intersect_key($data, $updateIfEquals);
Expand Down Expand Up @@ -160,7 +161,7 @@ public function save($updateIfEquals = [])

protected function open()
{
$reader= ReaderFactory::create($this->type);
$reader = $this->createReader();
$reader->open($this->path);
$this->callbacks->each(function ($elem) use (&$writer) {
call_user_func_array(array($writer, $elem[0]), $elem[1]);
Expand Down
6 changes: 6 additions & 0 deletions src/Importer/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
namespace Cyberduck\LaravelExcel\Importer;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

class Csv extends AbstractSpreadsheet
{
public function getType()
{
return Type::CSV;
}

public function createReader()
{
return ReaderEntityFactory::createCSVReader();
}
}
6 changes: 6 additions & 0 deletions src/Importer/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
namespace Cyberduck\LaravelExcel\Importer;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

class Excel extends AbstractSpreadsheet
{
public function getType()
{
return Type::XLSX;
}

public function createReader()
{
return ReaderEntityFactory::createXLSXReader();
}
}
6 changes: 6 additions & 0 deletions src/Importer/OpenOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
namespace Cyberduck\LaravelExcel\Importer;

use Box\Spout\Common\Type;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

class OpenOffice extends AbstractSpreadsheet
{
public function getType()
{
return Type::ODS;
}

public function createReader()
{
return ReaderEntityFactory::createODSReader();
}
}
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestCase extends BaseTestCase
{
protected $baseUrl = 'http://localhost';

const DEFAULT_ROW = [
protected $defaultRow = [
'field1' => 'A',
'field2' => 'B',
'field3' => 'C',
Expand Down Expand Up @@ -39,7 +39,7 @@ public function createApplication()
*
* @return void
*/
public function setUp()
public function setUp() : void
{
parent::setUp();

Expand All @@ -53,7 +53,7 @@ public function seed($item = 10)
$rows = [];
$limit = ($j>floor($item/50)) ? 50 : ($item-($j*50));
for ($i = 0; $i<$limit; $i++) {
$rows[] = self::DEFAULT_ROW;
$rows[] = $this->defaultRow;
}
DB::table('items')->insert($rows);
}
Expand Down
Loading

0 comments on commit c52bec6

Please sign in to comment.