From a09786c7b97721fadd5d209f578598fe02e793a6 Mon Sep 17 00:00:00 2001 From: Alex Szeliga Date: Thu, 15 Feb 2024 23:00:55 +0000 Subject: [PATCH 1/8] allow user to define file type --- src/Exportable.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index 8b063ec..f309bc4 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -44,9 +44,9 @@ abstract protected function setOptions(&$options); * * @return string */ - public function export($path, callable $callback = null) + public function export($path, callable $callback = null, $ext = null) { - self::exportOrDownload($path, 'openToFile', $callback); + self::exportOrDownload($path, 'openToFile', $callback, $ext); return realpath($path) ?: $path; } @@ -62,14 +62,14 @@ public function export($path, callable $callback = null) * * @return \Symfony\Component\HttpFoundation\StreamedResponse|string */ - public function download($path, callable $callback = null) + public function download($path, callable $callback = null, $ext = null) { if (method_exists(response(), 'streamDownload')) { return response()->streamDownload(function () use ($path, $callback) { self::exportOrDownload($path, 'openToBrowser', $callback); }, $path); } - self::exportOrDownload($path, 'openToBrowser', $callback); + self::exportOrDownload($path, 'openToBrowser', $callback, $ext); return ''; } @@ -85,12 +85,12 @@ public function download($path, callable $callback = null) * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException * @throws \OpenSpout\Common\Exception\SpoutException */ - private function exportOrDownload($path, $function, callable $callback = null) + private function exportOrDownload($path, $function, callable $callback = null, $ext = null) { - if (Str::endsWith($path, 'csv')) { + if (Str::endsWith($path, 'csv') || $ext === 'csv') { $options = new \OpenSpout\Writer\CSV\Options(); $writer = new \OpenSpout\Writer\CSV\Writer($options); - } elseif (Str::endsWith($path, 'ods')) { + } elseif (Str::endsWith($path, 'ods') || $ext === 'ods') { $options = new \OpenSpout\Writer\ODS\Options(); $writer = new \OpenSpout\Writer\ODS\Writer($options); } else { From 4166802f3536131dc1078d6aa5e93df7dd33bfca Mon Sep 17 00:00:00 2001 From: Alex Szeliga Date: Thu, 15 Feb 2024 23:09:12 +0000 Subject: [PATCH 2/8] fix docbloc --- src/Exportable.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Exportable.php b/src/Exportable.php index f309bc4..e933a6d 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -36,6 +36,7 @@ abstract protected function setOptions(&$options); /** * @param string $path * @param callable|null $callback + * @param string $ext * * @throws \OpenSpout\Common\Exception\InvalidArgumentException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException @@ -54,6 +55,7 @@ public function export($path, callable $callback = null, $ext = null) /** * @param $path * @param callable|null $callback + * @param string $ext * * @throws \OpenSpout\Common\Exception\InvalidArgumentException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException From b579ee11b2ee4d6294adff5502926f7d5f338e0a Mon Sep 17 00:00:00 2001 From: Alex Szeliga Date: Fri, 16 Feb 2024 14:21:41 +0000 Subject: [PATCH 3/8] reduce code complexity --- src/Exportable.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index e933a6d..30b7d12 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -4,7 +4,6 @@ use Generator; use Illuminate\Support\Collection; -use Illuminate\Support\Str; use InvalidArgumentException; use OpenSpout\Common\Entity\Row; use OpenSpout\Common\Entity\Style\Style; @@ -36,7 +35,6 @@ abstract protected function setOptions(&$options); /** * @param string $path * @param callable|null $callback - * @param string $ext * * @throws \OpenSpout\Common\Exception\InvalidArgumentException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException @@ -45,9 +43,9 @@ abstract protected function setOptions(&$options); * * @return string */ - public function export($path, callable $callback = null, $ext = null) + public function export($path, callable $callback = null) { - self::exportOrDownload($path, 'openToFile', $callback, $ext); + self::exportOrDownload($path, 'openToFile', $callback); return realpath($path) ?: $path; } @@ -55,7 +53,6 @@ public function export($path, callable $callback = null, $ext = null) /** * @param $path * @param callable|null $callback - * @param string $ext * * @throws \OpenSpout\Common\Exception\InvalidArgumentException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException @@ -64,14 +61,14 @@ public function export($path, callable $callback = null, $ext = null) * * @return \Symfony\Component\HttpFoundation\StreamedResponse|string */ - public function download($path, callable $callback = null, $ext = null) + 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, $ext); + self::exportOrDownload($path, 'openToBrowser', $callback); return ''; } @@ -89,10 +86,10 @@ public function download($path, callable $callback = null, $ext = null) */ private function exportOrDownload($path, $function, callable $callback = null, $ext = null) { - if (Str::endsWith($path, 'csv') || $ext === 'csv') { + if (str_ends_with($path, 'csv')) { $options = new \OpenSpout\Writer\CSV\Options(); $writer = new \OpenSpout\Writer\CSV\Writer($options); - } elseif (Str::endsWith($path, 'ods') || $ext === 'ods') { + } elseif (str_ends_with($path, 'ods')) { $options = new \OpenSpout\Writer\ODS\Options(); $writer = new \OpenSpout\Writer\ODS\Writer($options); } else { @@ -101,6 +98,12 @@ private function exportOrDownload($path, $function, callable $callback = null, $ } $this->setOptions($options); + + // extract file type for writing to php://output + if (str_starts_with($path,'php://export')) { + $path = explode(';', $path)[0]; + } + /* @var \OpenSpout\Writer\WriterInterface $writer */ $writer->$function($path); From 4933618075865e5d27c310276dc218d667be8174 Mon Sep 17 00:00:00 2001 From: Alex Szeliga Date: Fri, 16 Feb 2024 14:27:24 +0000 Subject: [PATCH 4/8] typo --- src/Exportable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index 30b7d12..34b48a8 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -100,7 +100,7 @@ private function exportOrDownload($path, $function, callable $callback = null, $ $this->setOptions($options); // extract file type for writing to php://output - if (str_starts_with($path,'php://export')) { + if (str_starts_with($path,'php://output')) { $path = explode(';', $path)[0]; } From 3c9b11b4a7bb18fa24b5b8acdedf5b46c5eb178a Mon Sep 17 00:00:00 2001 From: Alex Szeliga Date: Fri, 16 Feb 2024 19:48:47 +0000 Subject: [PATCH 5/8] remove leftover optional param --- src/Exportable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index 34b48a8..b16b684 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -84,7 +84,7 @@ public function download($path, callable $callback = null) * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException * @throws \OpenSpout\Common\Exception\SpoutException */ - private function exportOrDownload($path, $function, callable $callback = null, $ext = null) + private function exportOrDownload($path, $function, callable $callback = null) { if (str_ends_with($path, 'csv')) { $options = new \OpenSpout\Writer\CSV\Options(); From 82fb60e81b718477ba504aaebe140992927ebc14 Mon Sep 17 00:00:00 2001 From: Alex Szeliga Date: Fri, 16 Feb 2024 19:51:56 +0000 Subject: [PATCH 6/8] style guides --- src/Exportable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index b16b684..d3ccc89 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -100,7 +100,7 @@ private function exportOrDownload($path, $function, callable $callback = null) $this->setOptions($options); // extract file type for writing to php://output - if (str_starts_with($path,'php://output')) { + if (str_starts_with($path, 'php://output')) { $path = explode(';', $path)[0]; } From f823cd9b535d39bea54fb94fbb36e6a138ef4e27 Mon Sep 17 00:00:00 2001 From: Daryl Richter Date: Mon, 19 Feb 2024 11:11:20 -0500 Subject: [PATCH 7/8] slightly more generic? --- src/Exportable.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index d3ccc89..9054040 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -86,12 +86,18 @@ public function download($path, callable $callback = null) */ private function exportOrDownload($path, $function, callable $callback = null) { - if (str_ends_with($path, 'csv')) { - $options = new \OpenSpout\Writer\CSV\Options(); - $writer = new \OpenSpout\Writer\CSV\Writer($options); - } elseif (str_ends_with($path, 'ods')) { - $options = new \OpenSpout\Writer\ODS\Options(); - $writer = new \OpenSpout\Writer\ODS\Writer($options); + $ext = strtoupper(substr($path, -3)); + if (in_array($ext, ['CSV', 'ODS'], true)) { + $optionCls = "\OpenSpout\Writer\{$ext}\Options"; + $writerCls = "\OpenSpout\Writer\{$ext}\Writer"; + $options = new $optionCls(); + $writer = new $writerClass($options); + // if (str_ends_with($path, 'csv')) { + // $options = new \OpenSpout\Writer\CSV\Options(); + // $writer = new \OpenSpout\Writer\CSV\Writer($options); + // } elseif (str_ends_with($path, 'ods')) { + // $options = new \OpenSpout\Writer\ODS\Options(); + // $writer = new \OpenSpout\Writer\ODS\Writer($options); } else { $options = new \OpenSpout\Writer\XLSX\Options(); $writer = new \OpenSpout\Writer\XLSX\Writer($options); From 18b486d0c33161530a3a624c3115fd4be9068ee3 Mon Sep 17 00:00:00 2001 From: Daryl Richter Date: Mon, 19 Feb 2024 13:30:44 -0500 Subject: [PATCH 8/8] clean --- src/Exportable.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index 9054040..366f446 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -87,17 +87,12 @@ public function download($path, callable $callback = null) private function exportOrDownload($path, $function, callable $callback = null) { $ext = strtoupper(substr($path, -3)); - if (in_array($ext, ['CSV', 'ODS'], true)) { + $knownExts = ['CSV', 'ODS']; + if (in_array($ext, $knownExts, true)) { $optionCls = "\OpenSpout\Writer\{$ext}\Options"; $writerCls = "\OpenSpout\Writer\{$ext}\Writer"; $options = new $optionCls(); $writer = new $writerClass($options); - // if (str_ends_with($path, 'csv')) { - // $options = new \OpenSpout\Writer\CSV\Options(); - // $writer = new \OpenSpout\Writer\CSV\Writer($options); - // } elseif (str_ends_with($path, 'ods')) { - // $options = new \OpenSpout\Writer\ODS\Options(); - // $writer = new \OpenSpout\Writer\ODS\Writer($options); } else { $options = new \OpenSpout\Writer\XLSX\Options(); $writer = new \OpenSpout\Writer\XLSX\Writer($options);