Column::getColumnIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
class Column
6
{
7
    private Worksheet $worksheet;
8
9
    /**
10
     * Column index.
11
     */
12
    private string $columnIndex;
13
14
    /**
15
     * Create a new column.
16
     */
17 77
    public function __construct(Worksheet $worksheet, string $columnIndex = 'A')
18
    {
19
        // Set parent and column index
20 77
        $this->worksheet = $worksheet;
21 77
        $this->columnIndex = $columnIndex;
22
    }
23
24
    /**
25
     * Destructor.
26
     */
27 77
    public function __destruct()
28
    {
29 77
        unset($this->worksheet);
30
    }
31
32
    /**
33
     * Get column index as string eg: 'A'.
34
     */
35 2
    public function getColumnIndex(): string
36
    {
37 2
        return $this->columnIndex;
38
    }
39
40
    /**
41
     * Get cell iterator.
42
     *
43
     * @param int $startRow The row number at which to start iterating
44
     * @param ?int $endRow Optionally, the row number at which to stop iterating
45
     */
46 73
    public function getCellIterator(int $startRow = 1, ?int $endRow = null, bool $iterateOnlyExistingCells = false): ColumnCellIterator
47
    {
48 73
        return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow, $iterateOnlyExistingCells);
49
    }
50
51
    /**
52
     * Get row iterator. Synonym for getCellIterator().
53
     *
54
     * @param int $startRow The row number at which to start iterating
55
     * @param ?int $endRow Optionally, the row number at which to stop iterating
56
     */
57 1
    public function getRowIterator(int $startRow = 1, ?int $endRow = null, bool $iterateOnlyExistingCells = false): ColumnCellIterator
58
    {
59 1
        return $this->getCellIterator($startRow, $endRow, $iterateOnlyExistingCells);
60
    }
61
62
    /**
63
     * Returns a boolean true if the column contains no cells. By default, this means that no cell records exist in the
64
     *         collection for this column. 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 column 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 column 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 column
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 int $startRow The row number at which to start checking if cells are empty
79
     * @param ?int $endRow Optionally, the row number at which to stop checking if cells are empty
80
     */
81 45
    public function isEmpty(int $definitionOfEmptyFlags = 0, int $startRow = 1, ?int $endRow = null): bool
82
    {
83 45
        $nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
84 45
        $emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
85
86 45
        $cellIterator = $this->getCellIterator($startRow, $endRow);
87 45
        $cellIterator->setIterateOnlyExistingCells(true);
88 45
        foreach ($cellIterator as $cell) {
89 36
            $value = $cell->getValue();
90 36
            if ($value === null && $nullValueCellIsEmpty === true) {
91 12
                continue;
92
            }
93 33
            if ($value === '' && $emptyStringCellIsEmpty === true) {
94 10
                continue;
95
            }
96
97 28
            return false;
98
        }
99
100 18
        return true;
101
    }
102
103
    /**
104
     * Returns bound worksheet.
105
     */
106
    public function getWorksheet(): Worksheet
107
    {
108
        return $this->worksheet;
109
    }
110
}
111