Completed
Push — master ( 80e270...da3bcc )
by Mark
38s queued 38s
created

Row::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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