| 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 |
public function __construct(Worksheet $worksheet, $rowIndex = 1) |
| 27 |
{ |
| 28 |
// Set parent and row index |
| 29 |
$this->worksheet = $worksheet; |
| 30 |
$this->rowIndex = $rowIndex; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Destructor. |
| 35 |
*/ |
| 36 |
public function __destruct() |
| 37 |
{ |
| 38 |
$this->worksheet = null; // @phpstan-ignore-line |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get row index. |
| 43 |
*/ |
| 44 |
public function getRowIndex(): int |
| 45 |
{ |
| 46 |
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 |
public function getCellIterator($startColumn = 'A', $endColumn = null): RowCellIterator |
| 56 |
{ |
| 57 |
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 |
public function getColumnIterator($startColumn = 'A', $endColumn = null): RowCellIterator |
| 67 |
{ |
| 68 |
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 |
* @param string $startColumn The column address at which to start checking if cells are empty |
| 88 |
* @param string $endColumn Optionally, the column address at which to stop checking if cells are empty |
| 89 |
*/ |
| 90 |
public function isEmpty(int $definitionOfEmptyFlags = 0, $startColumn = 'A', $endColumn = null): bool |
| 91 |
{ |
| 92 |
$nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL); |
| 93 |
$emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL); |
| 94 |
|
| 95 |
$cellIterator = $this->getCellIterator($startColumn, $endColumn); |
| 96 |
$cellIterator->setIterateOnlyExistingCells(true); |
| 97 |
foreach ($cellIterator as $cell) { |
| 98 |
/** @scrutinizer ignore-call */ |
| 99 |
$value = $cell->getValue(); |
| 100 |
if ($value === null && $nullValueCellIsEmpty === true) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
if ($value === '' && $emptyStringCellIsEmpty === true) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns bound worksheet. |
| 115 |
*/ |
| 116 |
public function getWorksheet(): Worksheet |
| 117 |
{ |
| 118 |
return $this->worksheet; |
| 119 |
} |
| 120 |
} |
| 121 |
|