Skip to content

Commit

Permalink
Merge pull request #2 from ARCANEDEV/develop
Browse files Browse the repository at this point in the history
Updating the package
  • Loading branch information
arcanedev-maroc committed Nov 5, 2016
2 parents 005d42a + 3896236 commit c6a537a
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 43 deletions.
10 changes: 9 additions & 1 deletion config/laravel-excel.php
Expand Up @@ -13,15 +13,23 @@
*/
'drivers' => [
'excel' => [
'options' => [

],
],

'csv' => [

'options' => [
'field-delimiter' => ',',
'field-enclosure' => '"',
'add-bom' => true,
],
],

'ods' => [
'options' => [

],
],
],
];
9 changes: 9 additions & 0 deletions src/Contracts/Exporter.php
Expand Up @@ -30,6 +30,15 @@ public function setSerializer(Serializer $serializer);
*/
public function getType();

/**
* Set the writer options.
*
* @param array $options
*
* @return self
*/
public function setOptions(array $options);

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand Down
34 changes: 28 additions & 6 deletions src/ExporterFactory.php → src/ExporterManager.php
@@ -1,15 +1,15 @@
<?php namespace Arcanedev\LaravelExcel;

use Arcanedev\LaravelExcel\Contracts\ExporterManager;
use Arcanedev\LaravelExcel\Contracts\ExporterManager as ExporterManagerContract;
use Arcanedev\Support\Manager;

/**
* Class ExporterFactory
* Class ExporterManager
*
* @package Arcanedev\LaravelExcel
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class ExporterFactory extends Manager implements ExporterManager
class ExporterManager extends Manager implements ExporterManagerContract
{
/* ------------------------------------------------------------------------------------------------
| Getters & Setters
Expand Down Expand Up @@ -48,7 +48,9 @@ public function make($driver)
*/
public function createExcelDriver()
{
return new Exporters\ExcelExporter;
return new Exporters\ExcelExporter(
$this->getDriverOptions('excel')
);
}

/**
Expand All @@ -58,7 +60,9 @@ public function createExcelDriver()
*/
public function createCsvDriver()
{
return new Exporters\CsvExporter;
return new Exporters\CsvExporter(
$this->getDriverOptions('csv')
);
}

/**
Expand All @@ -68,6 +72,24 @@ public function createCsvDriver()
*/
public function createOpenOfficeDriver()
{
return new Exporters\OpenOfficeExporter;
return new Exporters\OpenOfficeExporter(
$this->getDriverOptions('ods')
);
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Get the driver options.
*
* @param string $driver
*
* @return array
*/
protected function getDriverOptions($driver)
{
return $this->app['config']->get("laravel-excel.drivers.$driver.options", []);
}
}
71 changes: 56 additions & 15 deletions src/Exporters/AbstractExporter.php
Expand Up @@ -26,16 +26,25 @@ abstract class AbstractExporter implements ExporterContract
/** @var \Arcanedev\LaravelExcel\Contracts\Serializer */
protected $serializer;

/** @var \Box\Spout\Writer\WriterInterface */
protected $writer;

/** @var array */
protected $options = [];

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
*/
/**
* AbstractExporter constructor.
*
* @param array $options
*/
public function __construct()
public function __construct(array $options = [])
{
$this->setSerializer(new DefaultSerializer);
$this->setOptions($options);
}

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -66,6 +75,20 @@ public function getType()
return $this->type;
}

/**
* Set the writer options.
*
* @param array $options
*
* @return self
*/
public function setOptions(array $options)
{
$this->options = $options;

return $this;
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand All @@ -91,10 +114,10 @@ public function load(Collection $data)
*/
public function save($filename)
{
$writer = $this->create();
$writer->openToFile($filename);
$this->makeRows($writer);
$writer->close();
$this->create();
$this->writer->openToFile($filename);
$this->makeRows();
$this->close();
}

/**
Expand All @@ -104,30 +127,48 @@ public function save($filename)
*/
public function stream($filename)
{
$writer = $this->create();
$writer->openToBrowser($filename);
$this->makeRows($writer);
$writer->close();
$this->create();
$this->writer->openToBrowser($filename);
$this->makeRows();
$this->close();
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* @return \Box\Spout\Writer\WriterInterface
* Create the writer.
*/
protected function create()
{
return WriterFactory::create($this->type);
$this->writer = WriterFactory::create($this->type);
$this->loadOptions();
}

/**
* Load the writer options.
*/
abstract protected function loadOptions();

/**
* Close the writer.
*/
protected function close()
{
$this->writer->close();
}

/**
* @param \Box\Spout\Writer\WriterInterface $writer
* Make rows.
*/
protected function makeRows(&$writer)
protected function makeRows()
{
if ( ! empty($headerRow = $this->serializer->getHeader()))
$writer->addRow($headerRow);
$this->writer->addRow($headerRow);

foreach ($this->data as $record) {
$writer->addRow(
$this->writer->addRow(
$this->serializer->getData($record)
);
}
Expand Down
26 changes: 26 additions & 0 deletions src/Exporters/CsvExporter.php
@@ -1,6 +1,7 @@
<?php namespace Arcanedev\LaravelExcel\Exporters;

use Box\Spout\Common\Type;
use Illuminate\Support\Arr;

/**
* Class CsvExporter
Expand All @@ -14,5 +15,30 @@ class CsvExporter extends AbstractExporter
| Properties
| ------------------------------------------------------------------------------------------------
*/
/** @var string */
protected $type = Type::CSV;

/** @var \Box\Spout\Writer\CSV\Writer */
protected $writer;

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Load the writer options.
*/
protected function loadOptions()
{
$this->writer
->setFieldDelimiter(
Arr::get($this->options, 'field-delimiter', ';')
)
->setFieldEnclosure(
Arr::get($this->options, 'field-enclosure', '"')
)
->setShouldAddBOM(
Arr::get($this->options, 'add-bom', true)
);
}
}
12 changes: 12 additions & 0 deletions src/Exporters/ExcelExporter.php
Expand Up @@ -15,4 +15,16 @@ class ExcelExporter extends AbstractExporter
| ------------------------------------------------------------------------------------------------
*/
protected $type = Type::XLSX;

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Load the writer options.
*/
protected function loadOptions()
{
//
}
}
12 changes: 12 additions & 0 deletions src/Exporters/OpenOfficeExporter.php
Expand Up @@ -15,4 +15,16 @@ class OpenOfficeExporter extends AbstractExporter
| ------------------------------------------------------------------------------------------------
*/
protected $type = Type::ODS;

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Load the writer options.
*/
protected function loadOptions()
{
//
}
}
36 changes: 29 additions & 7 deletions src/ImporterFactory.php → src/ImporterManager.php
@@ -1,15 +1,15 @@
<?php namespace Arcanedev\LaravelExcel;

use Arcanedev\LaravelExcel\Contracts\ImporterManager;
use Arcanedev\LaravelExcel\Contracts\ImporterManager as ImporterManagerContract;
use Arcanedev\Support\Manager;

/**
* Class ImporterFactory
* Class ImporterManager
*
* @package Arcanedev\LaravelExcel
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class ImporterFactory extends Manager implements ImporterManager
class ImporterManager extends Manager implements ImporterManagerContract
{
/* ------------------------------------------------------------------------------------------------
| Getters & Setters
Expand All @@ -22,7 +22,7 @@ class ImporterFactory extends Manager implements ImporterManager
*/
public function getDefaultDriver()
{
return $this->app['config']['laravel-excel.default'];
return $this->app['config']->get('laravel-excel.default');
}

/* ------------------------------------------------------------------------------------------------
Expand All @@ -48,7 +48,9 @@ public function make($driver)
*/
public function createExcelDriver()
{
return new Importers\ExcelImporter;
return new Importers\ExcelImporter(
$this->getDriverOptions('excel')
);
}

/**
Expand All @@ -58,7 +60,9 @@ public function createExcelDriver()
*/
public function createCsvDriver()
{
return new Importers\CsvImporter;
return new Importers\CsvImporter(
$this->getDriverOptions('csv')
);
}

/**
Expand All @@ -68,6 +72,24 @@ public function createCsvDriver()
*/
public function createOpenOfficeDriver()
{
return new Importers\OpenOfficeImporter;
return new Importers\OpenOfficeImporter(
$this->getDriverOptions('ods')
);
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Get the driver options.
*
* @param string $driver
*
* @return array
*/
protected function getDriverOptions($driver)
{
return $this->app['config']->get("laravel-excel.drivers.$driver.options", []);
}
}

0 comments on commit c6a537a

Please sign in to comment.