Passed
Pull Request — master (#3302)
by Mark
12:42
created

Column::getRowIterator()   A

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