Type::CSV, 'ods' => Type::ODS, 'xlsx' => Type::XLSX, ]; /** * This creates an instance of the appropriate reader, given the type of the file to be read * * @param string $readerType Type of the reader to instantiate * @throws \Box\Spout\Common\Exception\UnsupportedTypeException * @return ReaderInterface */ public static function create($readerType) { switch ($readerType) { case Type::CSV: return self::getCSVReader(); case Type::XLSX: return self::getXLSXReader(); case Type::ODS: return self::getODSReader(); default: throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType); } } /** * Creates a reader by file extension * * @param string The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx * @throws \Box\Spout\Common\Exception\IOException * @throws \Box\Spout\Common\Exception\UnsupportedTypeException * @return ReaderInterface */ public static function createFromFile(string $path) { if (!\is_file($path)) { throw new IOException( sprintf('Could not open "%s" for reading! File does not exist.', $path) ); } $ext = \pathinfo($path, PATHINFO_EXTENSION); $ext = \strtolower($ext); $readerType = self::$extensionReaderMap[$ext] ?? null; if ($readerType === null) { throw new UnsupportedTypeException( sprintf('No readers supporting the file extension "%s".', $ext) ); } return self::create($readerType); } /** * @return CSVReader */ private static function getCSVReader() { $optionsManager = new CSVOptionsManager(); $helperFactory = new HelperFactory(); $entityFactory = new CSVInternalEntityFactory($helperFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory); } /** * @return XLSXReader */ private static function getXLSXReader() { $optionsManager = new XLSXOptionsManager(); $helperFactory = new XLSXHelperFactory(); $managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory()); $entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory); } /** * @return ODSReader */ private static function getODSReader() { $optionsManager = new ODSOptionsManager(); $helperFactory = new ODSHelperFactory(); $managerFactory = new ODSManagerFactory(); $entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory); $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory); } }