|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Spatie\SimpleExcel; |
| 4 | + |
| 5 | +use OpenSpout\Common\Exception\IOException; |
| 6 | +use OpenSpout\Common\Exception\UnsupportedTypeException; |
| 7 | +use OpenSpout\Reader\CSV\Options as CSVOptions; |
| 8 | +use OpenSpout\Reader\CSV\Reader as CSVReader; |
| 9 | +use OpenSpout\Reader\ODS\Options as ODSOptions; |
| 10 | +use OpenSpout\Reader\ODS\Reader as ODSReader; |
| 11 | +use OpenSpout\Reader\ReaderInterface; |
| 12 | +use OpenSpout\Reader\XLSX\Options as XLSXOptions; |
| 13 | +use OpenSpout\Reader\XLSX\Reader as XLSXReader; |
| 14 | + |
| 15 | +/** |
| 16 | + * @internal overwritten from openspout/openspout so we can pass Options to the Reader classes |
| 17 | + * Original: \OpenSpout\Reader\Common\Creator\ReaderFactory |
| 18 | + */ |
| 19 | +class ReaderFactory |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Creates a reader by file extension. |
| 23 | + * |
| 24 | + * @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx |
| 25 | + * |
| 26 | + * @throws \OpenSpout\Common\Exception\UnsupportedTypeException |
| 27 | + */ |
| 28 | + public static function createFromFile( |
| 29 | + string $path, |
| 30 | + CSVOptions|XLSXOptions|ODSOptions|null $options = null |
| 31 | + ): ReaderInterface { |
| 32 | + $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
| 33 | + |
| 34 | + return match ($extension) { |
| 35 | + 'csv' => new CSVReader($options), |
| 36 | + 'xlsx' => new XLSXReader($options), |
| 37 | + 'ods' => new ODSReader($options), |
| 38 | + default => throw new UnsupportedTypeException('No readers supporting the given type: '.$extension), |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a reader by mime type. |
| 44 | + * |
| 45 | + * @param string $path the path to the spreadsheet file |
| 46 | + * |
| 47 | + * @throws \OpenSpout\Common\Exception\UnsupportedTypeException |
| 48 | + * @throws \OpenSpout\Common\Exception\IOException |
| 49 | + */ |
| 50 | + public static function createFromFileByMimeType( |
| 51 | + string $path, |
| 52 | + CSVOptions|XLSXOptions|ODSOptions|null $options = null |
| 53 | + ): ReaderInterface { |
| 54 | + if (! file_exists($path)) { |
| 55 | + throw new IOException("Could not open {$path} for reading! File does not exist."); |
| 56 | + } |
| 57 | + |
| 58 | + $mime_type = mime_content_type($path); |
| 59 | + |
| 60 | + return match ($mime_type) { |
| 61 | + 'application/csv', 'text/csv', 'text/plain' => new CSVReader($options), |
| 62 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => new XLSXReader($options), |
| 63 | + 'application/vnd.oasis.opendocument.spreadsheet' => new ODSReader($options), |
| 64 | + default => throw new UnsupportedTypeException('No readers supporting the given type: '.$mime_type), |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @deprecated use createFromFileByMimeType() or createFromFile() instead |
| 70 | + * |
| 71 | + * @throws UnsupportedTypeException |
| 72 | + */ |
| 73 | + public static function createFromType( |
| 74 | + string $readerType, |
| 75 | + CSVOptions|XLSXOptions|ODSOptions|null $options = null |
| 76 | + ): ReaderInterface { |
| 77 | + return match ($readerType) { |
| 78 | + 'csv' => new CSVReader($options), |
| 79 | + 'xlsx' => new XLSXReader($options), |
| 80 | + 'ods' => new ODSReader($options), |
| 81 | + default => throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType), |
| 82 | + }; |
| 83 | + } |
| 84 | +} |
0 commit comments