-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
159 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.