Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]

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