Skip to content

Commit

Permalink
Merge branch 'master' into dont_show_columns_with_underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
vdariel authored Oct 23, 2023
2 parents a67aaf5 + 45f8cb6 commit e6c7bc4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 38 deletions.
12 changes: 6 additions & 6 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use InvalidArgumentException;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\Common\AbstractOptions;
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use OpenSpout\Writer\XLSX\Writer;

/**
* Trait Exportable.
Expand All @@ -34,11 +34,11 @@ trait Exportable
private $rows_style;

/**
* @param \OpenSpout\Reader\ReaderInterface|\OpenSpout\Writer\WriterInterface $reader_or_writer
* @param AbstractOptions $options
*
* @return mixed
*/
abstract protected function setOptions(&$reader_or_writer);
abstract protected function setOptions(&$options);

/**
* @param string $path
Expand All @@ -59,7 +59,7 @@ public function export($path, callable $callback = null)
}

/**
* @param $path
* @param $path
* @param callable|null $callback
*
* @throws \OpenSpout\Common\Exception\InvalidArgumentException
Expand All @@ -74,15 +74,15 @@ public function download($path, callable $callback = null)
if (method_exists(response(), 'streamDownload')) {
return response()->streamDownload(function () use ($path, $callback) {
self::exportOrDownload($path, 'openToBrowser', $callback);
});
}, $path);
}
self::exportOrDownload($path, 'openToBrowser', $callback);

return '';
}

/**
* @param $path
* @param $path
* @param string $function
* @param callable|null $callback
*
Expand Down
63 changes: 33 additions & 30 deletions src/FastExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
use Generator;
use Illuminate\Support\Collection;
use OpenSpout\Reader\CSV\Options as CsvReaderOptions;
use OpenSpout\Reader\CSV\Reader;
use OpenSpout\Reader\ReaderInterface;
use OpenSpout\Writer\Common\AbstractOptions;
use OpenSpout\Writer\CSV\Options as CsvWriterOptions;
use OpenSpout\Writer\CSV\Writer;
use OpenSpout\Writer\WriterInterface;

/**
* Class FastExcel.
Expand Down Expand Up @@ -57,12 +54,7 @@ class FastExcel
/**
* @var callable
*/
protected $reader_configurator = null;

/**
* @var callable
*/
protected $writer_configurator = null;
protected $options_configurator = null;

/**
* FastExcel constructor.
Expand Down Expand Up @@ -161,11 +153,12 @@ public function configureCsv($delimiter = ',', $enclosure = '"', $encoding = 'UT
* @param callable|null $callback
*
* @return $this
*
* @deprecated Has no effect with spout v4
* @see configureOptionsUsing
*/
public function configureReaderUsing(?callable $callback = null)
{
$this->reader_configurator = $callback;

return $this;
}

Expand All @@ -175,39 +168,49 @@ public function configureReaderUsing(?callable $callback = null)
* @param callable|null $callback
*
* @return $this
*
* @deprecated Has no effect with spout v4
* @see configureOptionsUsing
*/
public function configureWriterUsing(?callable $callback = null)
{
$this->writer_configurator = $callback;
return $this;
}

/**
* Configure the underlying Spout Reader options using a callback.
*
* @param callable|null $callback
*
* @return $this
*/
public function configureOptionsUsing(?callable $callback = null)
{
$this->options_configurator = $callback;

return $this;
}

/**
* @param \OpenSpout\Reader\ReaderInterface|\OpenSpout\Writer\WriterInterface $reader_or_writer
* @param AbstractOptions $options
*/
protected function setOptions(&$reader_or_writer)
protected function setOptions(&$options)
{
if ($reader_or_writer instanceof CsvReaderOptions || $reader_or_writer instanceof CsvWriterOptions) {
$reader_or_writer->FIELD_DELIMITER = $this->csv_configuration['delimiter'];
$reader_or_writer->FIELD_ENCLOSURE = $this->csv_configuration['enclosure'];
if ($reader_or_writer instanceof CsvReaderOptions) {
$reader_or_writer->ENCODING = $this->csv_configuration['encoding'];
if ($options instanceof CsvReaderOptions || $options instanceof CsvWriterOptions) {
$options->FIELD_DELIMITER = $this->csv_configuration['delimiter'];
$options->FIELD_ENCLOSURE = $this->csv_configuration['enclosure'];
if ($options instanceof CsvReaderOptions) {
$options->ENCODING = $this->csv_configuration['encoding'];
}
if ($reader_or_writer instanceof CsvWriterOptions) {
$reader_or_writer->SHOULD_ADD_BOM = $this->csv_configuration['bom'];
if ($options instanceof CsvWriterOptions) {
$options->SHOULD_ADD_BOM = $this->csv_configuration['bom'];
}
}

if ($reader_or_writer instanceof ReaderInterface && is_callable($this->reader_configurator)) {
call_user_func(
$this->reader_configurator,
$reader_or_writer
);
} elseif ($reader_or_writer instanceof WriterInterface && is_callable($this->writer_configurator)) {
if (is_callable($this->options_configurator)) {
call_user_func(
$this->writer_configurator,
$reader_or_writer
$this->options_configurator,
$options
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Importable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use OpenSpout\Reader\SheetInterface;
use OpenSpout\Writer\Common\AbstractOptions;

/**
* Trait Importable.
Expand All @@ -21,11 +22,11 @@ trait Importable
private $sheet_number = 1;

/**
* @param \OpenSpout\Reader\ReaderInterface|\OpenSpout\Writer\WriterInterface $reader_or_writer
* @param AbstractOptions $options
*
* @return mixed
*/
abstract protected function setOptions(&$reader_or_writer);
abstract protected function setOptions(&$options);

/**
* @param string $path
Expand Down
29 changes: 29 additions & 0 deletions tests/FastExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,33 @@ public function testExportWithHeaderStyle()

unlink($file);
}

/**
* @throws \OpenSpout\Common\Exception\IOException
* @throws \OpenSpout\Common\Exception\UnsupportedTypeException
* @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException
*/
public function testImportXlsxWithCustomDateOption()
{
// Default options, dates will end parsed
$collection = (new FastExcel())->import(__DIR__.'/test-dates.xlsx');

$this->assertEquals(collect([
['col1' => new \DateTimeImmutable('2022-01-02 00:00:00.000000')],
['col1' => new \DateTimeImmutable('2022-01-03 00:00:00.000000')],
]), $collection);

$collection = (new FastExcel())
->configureOptionsUsing(function ($options) {
if ($options instanceof Options) {
$options->SHOULD_FORMAT_DATES = true;
}
})
->import(__DIR__.'/test-dates.xlsx');

$this->assertEquals(collect([
['col1' => '1/2/2022'],
['col1' => '1/3/2022'],
]), $collection);
}
}
Binary file added tests/test-dates.xlsx
Binary file not shown.

0 comments on commit e6c7bc4

Please sign in to comment.