chengkun
2025-09-15 0cc7f61de2b106c9664033fc27d6426d072ea019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
 
namespace PhpOffice\PhpSpreadsheet\Worksheet;
 
use Iterator as NativeIterator;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Collection\Cells;
 
/**
 * @template TKey
 *
 * @implements NativeIterator<TKey, Cell>
 */
abstract class CellIterator implements NativeIterator
{
    public const TREAT_NULL_VALUE_AS_EMPTY_CELL = 1;
 
    public const TREAT_EMPTY_STRING_AS_EMPTY_CELL = 2;
 
    public const IF_NOT_EXISTS_RETURN_NULL = false;
 
    public const IF_NOT_EXISTS_CREATE_NEW = true;
 
    /**
     * Worksheet to iterate.
     */
    protected Worksheet $worksheet;
 
    /**
     * Cell Collection to iterate.
     */
    protected Cells $cellCollection;
 
    /**
     * Iterate only existing cells.
     */
    protected bool $onlyExistingCells = false;
 
    /**
     * If iterating all cells, and a cell doesn't exist, identifies whether a new cell should be created,
     *    or if the iterator should return a null value.
     */
    protected bool $ifNotExists = self::IF_NOT_EXISTS_CREATE_NEW;
 
    /**
     * Destructor.
     */
    public function __destruct()
    {
        unset($this->worksheet, $this->cellCollection);
    }
 
    public function getIfNotExists(): bool
    {
        return $this->ifNotExists;
    }
 
    public function setIfNotExists(bool $ifNotExists = self::IF_NOT_EXISTS_CREATE_NEW): void
    {
        $this->ifNotExists = $ifNotExists;
    }
 
    /**
     * Get loop only existing cells.
     */
    public function getIterateOnlyExistingCells(): bool
    {
        return $this->onlyExistingCells;
    }
 
    /**
     * Validate start/end values for 'IterateOnlyExistingCells' mode, and adjust if necessary.
     */
    abstract protected function adjustForExistingOnlyRange(): void;
 
    /**
     * Set the iterator to loop only existing cells.
     */
    public function setIterateOnlyExistingCells(bool $value): void
    {
        $this->onlyExistingCells = (bool) $value;
 
        $this->adjustForExistingOnlyRange();
    }
}