diff --git a/src/Exportable.php b/src/Exportable.php index 28b4a76..e747050 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -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. @@ -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 @@ -59,7 +59,7 @@ public function export($path, callable $callback = null) } /** - * @param $path + * @param $path * @param callable|null $callback * * @throws \OpenSpout\Common\Exception\InvalidArgumentException @@ -74,7 +74,7 @@ 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); @@ -82,7 +82,7 @@ public function download($path, callable $callback = null) } /** - * @param $path + * @param $path * @param string $function * @param callable|null $callback * diff --git a/src/FastExcel.php b/src/FastExcel.php index f2e9c19..1f87ed4 100644 --- a/src/FastExcel.php +++ b/src/FastExcel.php @@ -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. @@ -57,12 +54,7 @@ class FastExcel /** * @var callable */ - protected $reader_configurator = null; - - /** - * @var callable - */ - protected $writer_configurator = null; + protected $options_configurator = null; /** * FastExcel constructor. @@ -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; } @@ -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 ); } } diff --git a/src/Importable.php b/src/Importable.php index ff2a3e7..e91c718 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -5,6 +5,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use OpenSpout\Reader\SheetInterface; +use OpenSpout\Writer\Common\AbstractOptions; /** * Trait Importable. @@ -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 diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 0ccf7cd..6dde5c1 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -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); + } } diff --git a/tests/test-dates.xlsx b/tests/test-dates.xlsx new file mode 100644 index 0000000..fc4e2a4 Binary files /dev/null and b/tests/test-dates.xlsx differ