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\Manager;
   4  
   5  use Box\Spout\Common\Entity\Row;
   6  use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface;
   7  
   8  /**
   9   * Class RowManager
  10   */
  11  class RowManager
  12  {
  13      /** @var InternalEntityFactoryInterface Factory to create entities */
  14      private $entityFactory;
  15  
  16      /**
  17       * @param InternalEntityFactoryInterface $entityFactory Factory to create entities
  18       */
  19      public function __construct(InternalEntityFactoryInterface $entityFactory)
  20      {
  21          $this->entityFactory = $entityFactory;
  22      }
  23  
  24      /**
  25       * Detect whether a row is considered empty.
  26       * An empty row has all of its cells empty.
  27       *
  28       * @param Row $row
  29       * @return bool
  30       */
  31      public function isEmpty(Row $row)
  32      {
  33          foreach ($row->getCells() as $cell) {
  34              if (!$cell->isEmpty()) {
  35                  return false;
  36              }
  37          }
  38  
  39          return true;
  40      }
  41  
  42      /**
  43       * Fills the missing indexes of a row with empty cells.
  44       *
  45       * @param Row $row
  46       * @return Row
  47       */
  48      public function fillMissingIndexesWithEmptyCells(Row $row)
  49      {
  50          $numCells = $row->getNumCells();
  51  
  52          if ($numCells === 0) {
  53              return $row;
  54          }
  55  
  56          $rowCells = $row->getCells();
  57          $maxCellIndex = $numCells;
  58  
  59          // If the row has empty cells, calling "setCellAtIndex" will add the cell
  60          // but in the wrong place (the new cell is added at the end of the array).
  61          // Therefore, we need to sort the array using keys to have proper order.
  62          // @see https://github.com/box/spout/issues/740
  63          $needsSorting = false;
  64  
  65          for ($cellIndex = 0; $cellIndex < $maxCellIndex; $cellIndex++) {
  66              if (!isset($rowCells[$cellIndex])) {
  67                  $row->setCellAtIndex($this->entityFactory->createCell(''), $cellIndex);
  68                  $needsSorting = true;
  69              }
  70          }
  71  
  72          if ($needsSorting) {
  73              $rowCells = $row->getCells();
  74              ksort($rowCells);
  75              $row->setCells($rowCells);
  76          }
  77  
  78          return $row;
  79      }
  80  }