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\Reader\Common\Manager;
   6  
   7  use OpenSpout\Common\Entity\Cell;
   8  use OpenSpout\Common\Entity\Row;
   9  
  10  /**
  11   * @internal
  12   */
  13  final class RowManager
  14  {
  15      /**
  16       * Fills the missing indexes of a row with empty cells.
  17       */
  18      public function fillMissingIndexesWithEmptyCells(Row $row): void
  19      {
  20          $numCells = $row->getNumCells();
  21  
  22          if (0 === $numCells) {
  23              return;
  24          }
  25  
  26          $rowCells = $row->getCells();
  27          $maxCellIndex = $numCells;
  28  
  29          /**
  30           * If the row has empty cells, calling "setCellAtIndex" will add the cell
  31           * but in the wrong place (the new cell is added at the end of the array).
  32           * Therefore, we need to sort the array using keys to have proper order.
  33           *
  34           * @see https://github.com/box/spout/issues/740
  35           */
  36          $needsSorting = false;
  37  
  38          for ($cellIndex = 0; $cellIndex < $maxCellIndex; ++$cellIndex) {
  39              if (!isset($rowCells[$cellIndex])) {
  40                  $row->setCellAtIndex(Cell::fromValue(''), $cellIndex);
  41                  $needsSorting = true;
  42              }
  43          }
  44  
  45          if ($needsSorting) {
  46              $rowCells = $row->getCells();
  47              ksort($rowCells);
  48              $row->setCells($rowCells);
  49          }
  50      }
  51  }