Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| SpreadsheetParser | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| parseSpreadsheet | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\Parser\Spreadsheet |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\Parser\Spreadsheet; |
| 16 | |
| 17 | use PhpOffice\PhpSpreadsheet\IOFactory; |
| 18 | use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
| 19 | |
| 20 | /** |
| 21 | * Spreadsheet parser class. |
| 22 | * |
| 23 | * @package phpOMS\Utils\Parser\Spreadsheet |
| 24 | * @license OMS License 2.0 |
| 25 | * @link https://jingga.app |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | class SpreadsheetParser |
| 29 | { |
| 30 | /** |
| 31 | * Spreadsheet to string |
| 32 | * |
| 33 | * @param string $path Path |
| 34 | * |
| 35 | * @return string |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public static function parseSpreadsheet(string $path, string $output = 'json') : string |
| 40 | { |
| 41 | if ($output === 'json') { |
| 42 | $spreadsheet = IOFactory::load($path); |
| 43 | |
| 44 | $sheetCount = $spreadsheet->getSheetCount(); |
| 45 | $csv = []; |
| 46 | |
| 47 | for ($i = 0; $i < $sheetCount; ++$i) { |
| 48 | $csv[] = $spreadsheet->getSheet($i)->toArray(null, true, true, true); |
| 49 | } |
| 50 | |
| 51 | $json = \json_encode($csv); |
| 52 | |
| 53 | return $json === false ? '' : $json; |
| 54 | } elseif ($output === 'pdf') { |
| 55 | $spreadsheet = IOFactory::load($path); |
| 56 | |
| 57 | $spreadsheet->getActiveSheet()->setShowGridLines(false); |
| 58 | $spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); |
| 59 | |
| 60 | IOFactory::registerWriter('custom', \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter::class); |
| 61 | |
| 62 | /** @var \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter $writer */ |
| 63 | $writer = IOFactory::createWriter($spreadsheet, 'custom'); |
| 64 | |
| 65 | return $writer->toPdfString(); |
| 66 | } elseif ($output === 'html') { |
| 67 | $spreadsheet = IOFactory::load($path); |
| 68 | |
| 69 | IOFactory::registerWriter('custom', \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter::class); |
| 70 | /** @var \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter $writer */ |
| 71 | $writer = IOFactory::createWriter($spreadsheet, 'custom'); |
| 72 | |
| 73 | return $writer->generateHtmlAll(); |
| 74 | } |
| 75 | |
| 76 | return ''; |
| 77 | } |
| 78 | } |