Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Writer\CSV;
   6  
   7  use OpenSpout\Common\Entity\Cell;
   8  use OpenSpout\Common\Entity\Row;
   9  use OpenSpout\Common\Exception\IOException;
  10  use OpenSpout\Common\Helper\EncodingHelper;
  11  use OpenSpout\Writer\AbstractWriter;
  12  
  13  final class Writer extends AbstractWriter
  14  {
  15      /** @var string Content-Type value for the header */
  16      protected static string $headerContentType = 'text/csv; charset=UTF-8';
  17  
  18      private Options $options;
  19  
  20      private int $lastWrittenRowIndex = 0;
  21  
  22      public function __construct(?Options $options = null)
  23      {
  24          $this->options = $options ?? new Options();
  25      }
  26  
  27      public function getOptions(): Options
  28      {
  29          return $this->options;
  30      }
  31  
  32      /**
  33       * Opens the CSV streamer and makes it ready to accept data.
  34       */
  35      protected function openWriter(): void
  36      {
  37          if ($this->options->SHOULD_ADD_BOM) {
  38              // Adds UTF-8 BOM for Unicode compatibility
  39              fwrite($this->filePointer, EncodingHelper::BOM_UTF8);
  40          }
  41      }
  42  
  43      /**
  44       * Adds a row to the currently opened writer.
  45       *
  46       * @param Row $row The row containing cells and styles
  47       *
  48       * @throws IOException If unable to write data
  49       */
  50      protected function addRowToWriter(Row $row): void
  51      {
  52          $cells = array_map(static function (Cell\BooleanCell|Cell\EmptyCell|Cell\NumericCell|Cell\StringCell|Cell\FormulaCell $value): string {
  53              if ($value instanceof Cell\BooleanCell) {
  54                  return (string) (int) $value->getValue();
  55              }
  56  
  57              return (string) $value->getValue();
  58          }, $row->getCells());
  59  
  60          $wasWriteSuccessful = fputcsv(
  61              $this->filePointer,
  62              $cells,
  63              $this->options->FIELD_DELIMITER,
  64              $this->options->FIELD_ENCLOSURE,
  65              ''
  66          );
  67          if (false === $wasWriteSuccessful) {
  68              throw new IOException('Unable to write data'); // @codeCoverageIgnore
  69          }
  70  
  71          ++$this->lastWrittenRowIndex;
  72          if (0 === $this->lastWrittenRowIndex % $this->options->FLUSH_THRESHOLD) {
  73              fflush($this->filePointer);
  74          }
  75      }
  76  
  77      /**
  78       * Closes the CSV streamer, preventing any additional writing.
  79       * If set, sets the headers and redirects output to the browser.
  80       */
  81      protected function closeWriter(): void
  82      {
  83          $this->lastWrittenRowIndex = 0;
  84      }
  85  }