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\Entity\Cell;
   6  use Box\Spout\Common\Entity\Row;
   7  use Box\Spout\Common\Entity\Style\Style;
   8  use Box\Spout\Common\Exception\UnsupportedTypeException;
   9  use Box\Spout\Common\Type;
  10  use Box\Spout\Writer\WriterInterface;
  11  
  12  /**
  13   * Class WriterEntityFactory
  14   * Factory to create external entities
  15   */
  16  class WriterEntityFactory
  17  {
  18      /**
  19       * This creates an instance of the appropriate writer, given the type of the file to be written
  20       *
  21       * @param  string $writerType Type of the writer to instantiate
  22       * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
  23       * @return WriterInterface
  24       */
  25      public static function createWriter($writerType)
  26      {
  27          return WriterFactory::createFromType($writerType);
  28      }
  29  
  30      /**
  31       * This creates an instance of the appropriate writer, given the extension of the file to be written
  32       *
  33       * @param string $path The path to the spreadsheet file. Supported extensions are .csv, .ods and .xlsx
  34       * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
  35       * @return WriterInterface
  36       */
  37      public static function createWriterFromFile(string $path)
  38      {
  39          return WriterFactory::createFromFile($path);
  40      }
  41  
  42      /**
  43       * This creates an instance of a CSV writer
  44       *
  45       * @return \Box\Spout\Writer\CSV\Writer
  46       */
  47      public static function createCSVWriter()
  48      {
  49          try {
  50              return WriterFactory::createFromType(Type::CSV);
  51          } catch (UnsupportedTypeException $e) {
  52              // should never happen
  53          }
  54      }
  55  
  56      /**
  57       * This creates an instance of a XLSX writer
  58       *
  59       * @return \Box\Spout\Writer\XLSX\Writer
  60       */
  61      public static function createXLSXWriter()
  62      {
  63          try {
  64              return WriterFactory::createFromType(Type::XLSX);
  65          } catch (UnsupportedTypeException $e) {
  66              // should never happen
  67          }
  68      }
  69  
  70      /**
  71       * This creates an instance of a ODS writer
  72       *
  73       * @return \Box\Spout\Writer\ODS\Writer
  74       */
  75      public static function createODSWriter()
  76      {
  77          try {
  78              return WriterFactory::createFromType(Type::ODS);
  79          } catch (UnsupportedTypeException $e) {
  80              // should never happen
  81          }
  82      }
  83  
  84      /**
  85       * @param Cell[] $cells
  86       * @param Style|null $rowStyle
  87       * @return Row
  88       */
  89      public static function createRow(array $cells = [], Style $rowStyle = null)
  90      {
  91          return new Row($cells, $rowStyle);
  92      }
  93  
  94      /**
  95       * @param array $cellValues
  96       * @param Style|null $rowStyle
  97       * @return Row
  98       */
  99      public static function createRowFromArray(array $cellValues = [], Style $rowStyle = null)
 100      {
 101          $cells = array_map(function ($cellValue) {
 102              return new Cell($cellValue);
 103          }, $cellValues);
 104  
 105          return new Row($cells, $rowStyle);
 106      }
 107  
 108      /**
 109       * @param mixed $cellValue
 110       * @param Style|null $cellStyle
 111       * @return Cell
 112       */
 113      public static function createCell($cellValue, Style $cellStyle = null)
 114      {
 115          return new Cell($cellValue, $cellStyle);
 116      }
 117  }