Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
}