Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   1  <?php
   2  
   3  namespace Box\Spout\Reader\Common\Creator;
   4  
   5  use Box\Spout\Common\Creator\HelperFactory;
   6  use Box\Spout\Common\Exception\UnsupportedTypeException;
   7  use Box\Spout\Common\Type;
   8  use Box\Spout\Reader\CSV\Creator\InternalEntityFactory as CSVInternalEntityFactory;
   9  use Box\Spout\Reader\CSV\Manager\OptionsManager as CSVOptionsManager;
  10  use Box\Spout\Reader\CSV\Reader as CSVReader;
  11  use Box\Spout\Reader\ODS\Creator\HelperFactory as ODSHelperFactory;
  12  use Box\Spout\Reader\ODS\Creator\InternalEntityFactory as ODSInternalEntityFactory;
  13  use Box\Spout\Reader\ODS\Creator\ManagerFactory as ODSManagerFactory;
  14  use Box\Spout\Reader\ODS\Manager\OptionsManager as ODSOptionsManager;
  15  use Box\Spout\Reader\ODS\Reader as ODSReader;
  16  use Box\Spout\Reader\ReaderInterface;
  17  use Box\Spout\Reader\XLSX\Creator\HelperFactory as XLSXHelperFactory;
  18  use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory as XLSXInternalEntityFactory;
  19  use Box\Spout\Reader\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
  20  use Box\Spout\Reader\XLSX\Manager\OptionsManager as XLSXOptionsManager;
  21  use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
  22  use Box\Spout\Reader\XLSX\Reader as XLSXReader;
  23  
  24  /**
  25   * Class ReaderFactory
  26   * This factory is used to create readers, based on the type of the file to be read.
  27   * It supports CSV, XLSX and ODS formats.
  28   */
  29  class ReaderFactory
  30  {
  31      /**
  32       * Creates a reader by file extension
  33       *
  34       * @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
  35       * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
  36       * @return ReaderInterface
  37       */
  38      public static function createFromFile(string $path)
  39      {
  40          $extension = \strtolower(\pathinfo($path, PATHINFO_EXTENSION));
  41  
  42          return self::createFromType($extension);
  43      }
  44  
  45      /**
  46       * This creates an instance of the appropriate reader, given the type of the file to be read
  47       *
  48       * @param  string $readerType Type of the reader to instantiate
  49       * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
  50       * @return ReaderInterface
  51       */
  52      public static function createFromType($readerType)
  53      {
  54          switch ($readerType) {
  55              case Type::CSV: return self::createCSVReader();
  56              case Type::XLSX: return self::createXLSXReader();
  57              case Type::ODS: return self::createODSReader();
  58              default:
  59                  throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
  60          }
  61      }
  62  
  63      /**
  64       * @return CSVReader
  65       */
  66      private static function createCSVReader()
  67      {
  68          $optionsManager = new CSVOptionsManager();
  69          $helperFactory = new HelperFactory();
  70          $entityFactory = new CSVInternalEntityFactory($helperFactory);
  71          $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
  72  
  73          return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory);
  74      }
  75  
  76      /**
  77       * @return XLSXReader
  78       */
  79      private static function createXLSXReader()
  80      {
  81          $optionsManager = new XLSXOptionsManager();
  82          $helperFactory = new XLSXHelperFactory();
  83          $managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory());
  84          $entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory);
  85          $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
  86  
  87          return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
  88      }
  89  
  90      /**
  91       * @return ODSReader
  92       */
  93      private static function createODSReader()
  94      {
  95          $optionsManager = new ODSOptionsManager();
  96          $helperFactory = new ODSHelperFactory();
  97          $managerFactory = new ODSManagerFactory();
  98          $entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory);
  99          $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
 100  
 101          return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory);
 102      }
 103  }