Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
 
5
class Row
6
{
7
    private Worksheet $worksheet;
8
 
9
    /**
10
     * Row index.
11
     */
12
    private int $rowIndex;
13
 
14
    /**
15
     * Create a new row.
16
     */
17
    public function __construct(Worksheet $worksheet, int $rowIndex = 1)
18
    {
19
        // Set parent and row index
20
        $this->worksheet = $worksheet;
21
        $this->rowIndex = $rowIndex;
22
    }
23
 
24
    /**
25
     * Destructor.
26
     */
27
    public function __destruct()
28
    {
29
        unset($this->worksheet);
30
    }
31
 
32
    /**
33
     * Get row index.
34
     */
35
    public function getRowIndex(): int
36
    {
37
        return $this->rowIndex;
38
    }
39
 
40
    /**
41
     * Get cell iterator.
42
     *
43
     * @param string $startColumn The column address at which to start iterating
44
     * @param ?string $endColumn Optionally, the column address at which to stop iterating
45
     */
46
    public function getCellIterator(string $startColumn = 'A', ?string $endColumn = null, bool $iterateOnlyExistingCells = false): RowCellIterator
47
    {
48
        return new RowCellIterator($this->worksheet, $this->rowIndex, $startColumn, $endColumn, $iterateOnlyExistingCells);
49
    }
50
 
51
    /**
52
     * Get column iterator. Synonym for getCellIterator().
53
     *
54
     * @param string $startColumn The column address at which to start iterating
55
     * @param ?string $endColumn Optionally, the column address at which to stop iterating
56
     */
57
    public function getColumnIterator(string $startColumn = 'A', ?string $endColumn = null, bool $iterateOnlyExistingCells = false): RowCellIterator
58
    {
59
        return $this->getCellIterator($startColumn, $endColumn, $iterateOnlyExistingCells);
60
    }
61
 
62
    /**
63
     * Returns a boolean true if the row contains no cells. By default, this means that no cell records exist in the
64
     *         collection for this row. false will be returned otherwise.
65
     *     This rule can be modified by passing a $definitionOfEmptyFlags value:
66
     *          1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value
67
     *                  cells, then the row will be considered empty.
68
     *          2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty
69
     *                  string value cells, then the row will be considered empty.
70
     *          3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
71
     *                  If the only cells in the collection are null value or empty string value cells, then the row
72
     *                  will be considered empty.
73
     *
74
     * @param int $definitionOfEmptyFlags
75
     *              Possible Flag Values are:
76
     *                  CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
77
     *                  CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
78
     * @param string $startColumn The column address at which to start checking if cells are empty
79
     * @param ?string $endColumn Optionally, the column address at which to stop checking if cells are empty
80
     */
81
    public function isEmpty(int $definitionOfEmptyFlags = 0, string $startColumn = 'A', ?string $endColumn = null): bool
82
    {
83
        $nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
84
        $emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
85
 
86
        $cellIterator = $this->getCellIterator($startColumn, $endColumn);
87
        $cellIterator->setIterateOnlyExistingCells(true);
88
        foreach ($cellIterator as $cell) {
89
            $value = $cell->getValue();
90
            if ($value === null && $nullValueCellIsEmpty === true) {
91
                continue;
92
            }
93
            if ($value === '' && $emptyStringCellIsEmpty === true) {
94
                continue;
95
            }
96
 
97
            return false;
98
        }
99
 
100
        return true;
101
    }
102
 
103
    /**
104
     * Returns bound worksheet.
105
     */
106
    public function getWorksheet(): Worksheet
107
    {
108
        return $this->worksheet;
109
    }
110
}