Skip to content

Commit

Permalink
remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 3, 2024
1 parent ed35871 commit d1b0be7
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 206 deletions.
143 changes: 143 additions & 0 deletions src/Common/PhpSpreadsheetUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

declare(strict_types=1);

namespace LeKoala\SpreadCompat\Common;

use Exception;
use Generator;
use LeKoala\SpreadCompat\SpreadCompat;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\BaseReader;
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter;

trait PhpSpreadsheetUtils
{
protected function getReaderClass(): string
{
throw new Exception("Method not implemented");
}

protected function getWriterClass(): string
{
throw new Exception("Method not implemented");
}

protected function getMimetype(): string
{
throw new Exception("Method not implemented");
}

protected function getReader(): BaseReader
{
$class = $this->getReaderClass();
/** @var \PhpOffice\PhpSpreadsheet\Reader\Xls|\PhpOffice\PhpSpreadsheet\Reader\Xlsx $reader */
$reader = new ($class);
// We are only interested in cell data
$reader->setReadDataOnly(true);
return $reader;
}

protected function readSpreadsheet(Spreadsheet $spreadsheet): Generator
{
$headers = null;
foreach ($spreadsheet->getActiveSheet()->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) {
$v = $cell->getValue();
$data[] = $v;
}
if (empty($data) || $data[0] === null) {
continue;
}
if ($this->assoc) {
if ($headers === null) {
$headers = $data;
continue;
}
$data = array_combine($headers, $data);
}
yield $data;
}
}

public function readString(
string $contents,
...$opts
): Generator {
$filename = SpreadCompat::getTempFilename();
file_put_contents($filename, $contents);
yield from $this->readFile($filename, ...$opts);
unlink($filename);
}

public function readFile(
string $filename,
...$opts
): Generator {
$this->configure(...$opts);
$spreadsheet = $this->getReader()->load($filename);
yield from $this->readSpreadsheet($spreadsheet);
}

protected function getWriter(iterable $source): BaseWriter
{
$spreadsheet = new Spreadsheet();
if (!is_array($source)) {
$source = iterator_to_array($source);
}
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($source);
if ($this->autofilter) {
$sheet->setAutoFilter($this->autofilter);
}
if ($this->freezePane) {
$sheet->freezePane($this->freezePane);
}
$class = $this->getWriterClass();
/** @var \PhpOffice\PhpSpreadsheet\Writer\Xls|\PhpOffice\PhpSpreadsheet\Writer\Xlsx $writer */
$writer = new ($class)($spreadsheet);
return $writer;
}

public function writeString(iterable $data, ...$opts): string
{
$filename = SpreadCompat::getTempFilename();
$this->writeFile($data, $filename);
$contents = file_get_contents($filename);
if (!$contents) {
$contents = "";
}
unlink($filename);
return $contents;
}

public function writeFile(iterable $data, string $filename, ...$opts): bool
{
$this->configure(...$opts);
$writer = $this->getWriter($data);
$writer->save($filename);
return true;
}

public function output(iterable $data, string $filename, ...$opts): void
{
$this->configure(...$opts);
$writer = $this->getWriter($data);

header('Content-Type: ' . $this->getMimetype());
header(
'Content-Disposition: attachment; ' .
'filename="' . rawurlencode($filename) . '"; ' .
'filename*=UTF-8\'\'' . rawurlencode($filename)
);
header('Cache-Control: max-age=0');
header('Pragma: public');

ob_end_clean();
ob_start();
$writer->save('php://output');
}
}
111 changes: 8 additions & 103 deletions src/Xls/PhpSpreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,26 @@

namespace LeKoala\SpreadCompat\Xls;

use Generator;
use RuntimeException;
use LeKoala\SpreadCompat\SpreadCompat;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use LeKoala\SpreadCompat\Common\PhpSpreadsheetUtils;
use PhpOffice\PhpSpreadsheet\Reader\Xls as ReaderXls;
use PhpOffice\PhpSpreadsheet\Writer\Xls as WriterXls;

class PhpSpreadsheet extends XlsAdapter
{
protected function getReader(): ReaderXls
{
$reader = new ReaderXls();
// We are only interested in cell data
$reader->setReadDataOnly(true);
return $reader;
}

protected function readSpreadsheet(Spreadsheet $spreadsheet): Generator
{
$headers = null;
foreach ($spreadsheet->getActiveSheet()->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) {
$v = $cell->getValue();
$data[] = $v;
}
if (empty($data) || $data[0] === null) {
continue;
}
if ($this->assoc) {
if ($headers === null) {
$headers = $data;
continue;
}
$data = array_combine($headers, $data);
}
yield $data;
}
}

public function readString(
string $contents,
...$opts
): Generator {
$filename = SpreadCompat::getTempFilename();
file_put_contents($filename, $contents);
yield from $this->readFile($filename);
unlink($filename);
}
use PhpSpreadsheetUtils;

public function readFile(
string $filename,
...$opts
): Generator {
$this->configure(...$opts);
$spreadsheet = $this->getReader()->load($filename);
yield from $this->readSpreadsheet($spreadsheet);
}

protected function getWriter(iterable $source): WriterXls
protected function getReaderClass(): string
{
$spreadsheet = new Spreadsheet();
if (!is_array($source)) {
$source = iterator_to_array($source);
}
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($source);
if ($this->autofilter) {
$sheet->setAutoFilter($this->autofilter);
}
if ($this->freezePane) {
$sheet->freezePane($this->freezePane);
}
$writer = new WriterXls($spreadsheet);
return $writer;
return ReaderXls::class;
}

public function writeString(iterable $data, ...$opts): string
protected function getWriterClass(): string
{
$filename = SpreadCompat::getTempFilename();
$this->writeFile($data, $filename);
$contents = file_get_contents($filename);
if (!$contents) {
$contents = "";
}
unlink($filename);
return $contents;
return WriterXls::class;
}

public function writeFile(iterable $data, string $filename, ...$opts): bool
protected function getMimetype(): string
{
$this->configure(...$opts);
$writer = $this->getWriter($data);
$writer->save($filename);
return true;
}

public function output(iterable $data, string $filename, ...$opts): void
{
$this->configure(...$opts);
$writer = $this->getWriter($data);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header(
'Content-Disposition: attachment; ' .
'filename="' . rawurlencode($filename) . '"; ' .
'filename*=UTF-8\'\'' . rawurlencode($filename)
);
header('Cache-Control: max-age=0');
header('Pragma: public');

ob_end_clean();
ob_start();
$writer->save('php://output');
return 'application/vnd.ms-excel';
}
}
Loading

0 comments on commit d1b0be7

Please sign in to comment.