Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Cell;
4
 
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
7
use Stringable;
8
 
9
class CellAddress implements Stringable
10
{
11
    protected ?Worksheet $worksheet;
12
 
13
    protected string $cellAddress;
14
 
15
    protected string $columnName = '';
16
 
17
    protected int $columnId;
18
 
19
    protected int $rowId;
20
 
21
    public function __construct(string $cellAddress, ?Worksheet $worksheet = null)
22
    {
23
        $this->cellAddress = str_replace('$', '', $cellAddress);
24
        [$this->columnId, $this->rowId, $this->columnName] = Coordinate::indexesFromString($this->cellAddress);
25
        $this->worksheet = $worksheet;
26
    }
27
 
28
    public function __destruct()
29
    {
30
        unset($this->worksheet);
31
    }
32
 
33
    /**
34
     * @phpstan-assert int|numeric-string $columnId
35
     * @phpstan-assert int|numeric-string $rowId
36
     */
37
    private static function validateColumnAndRow(int|string $columnId, int|string $rowId): void
38
    {
39
        if (!is_numeric($columnId) || $columnId <= 0 || !is_numeric($rowId) || $rowId <= 0) {
40
            throw new Exception('Row and Column Ids must be positive integer values');
41
        }
42
    }
43
 
44
    public static function fromColumnAndRow(int|string $columnId, int|string $rowId, ?Worksheet $worksheet = null): self
45
    {
46
        self::validateColumnAndRow($columnId, $rowId);
47
 
48
        return new self(Coordinate::stringFromColumnIndex($columnId) . $rowId, $worksheet);
49
    }
50
 
51
    public static function fromColumnRowArray(array $array, ?Worksheet $worksheet = null): self
52
    {
53
        [$columnId, $rowId] = $array;
54
 
55
        return self::fromColumnAndRow($columnId, $rowId, $worksheet);
56
    }
57
 
58
    public static function fromCellAddress(string $cellAddress, ?Worksheet $worksheet = null): self
59
    {
60
        return new self($cellAddress, $worksheet);
61
    }
62
 
63
    /**
64
     * The returned address string will contain the worksheet name as well, if available,
65
     *     (ie. if a Worksheet was provided to the constructor).
66
     *     e.g. "'Mark''s Worksheet'!C5".
67
     */
68
    public function fullCellAddress(): string
69
    {
70
        if ($this->worksheet !== null) {
71
            $title = str_replace("'", "''", $this->worksheet->getTitle());
72
 
73
            return "'{$title}'!{$this->cellAddress}";
74
        }
75
 
76
        return $this->cellAddress;
77
    }
78
 
79
    public function worksheet(): ?Worksheet
80
    {
81
        return $this->worksheet;
82
    }
83
 
84
    /**
85
     * The returned address string will contain just the column/row address,
86
     *     (even if a Worksheet was provided to the constructor).
87
     *     e.g. "C5".
88
     */
89
    public function cellAddress(): string
90
    {
91
        return $this->cellAddress;
92
    }
93
 
94
    public function rowId(): int
95
    {
96
        return $this->rowId;
97
    }
98
 
99
    public function columnId(): int
100
    {
101
        return $this->columnId;
102
    }
103
 
104
    public function columnName(): string
105
    {
106
        return $this->columnName;
107
    }
108
 
109
    public function nextRow(int $offset = 1): self
110
    {
111
        $newRowId = $this->rowId + $offset;
112
        if ($newRowId < 1) {
113
            $newRowId = 1;
114
        }
115
 
116
        return self::fromColumnAndRow($this->columnId, $newRowId);
117
    }
118
 
119
    public function previousRow(int $offset = 1): self
120
    {
121
        return $this->nextRow(0 - $offset);
122
    }
123
 
124
    public function nextColumn(int $offset = 1): self
125
    {
126
        $newColumnId = $this->columnId + $offset;
127
        if ($newColumnId < 1) {
128
            $newColumnId = 1;
129
        }
130
 
131
        return self::fromColumnAndRow($newColumnId, $this->rowId);
132
    }
133
 
134
    public function previousColumn(int $offset = 1): self
135
    {
136
        return $this->nextColumn(0 - $offset);
137
    }
138
 
139
    /**
140
     * The returned address string will contain the worksheet name as well, if available,
141
     *     (ie. if a Worksheet was provided to the constructor).
142
     *     e.g. "'Mark''s Worksheet'!C5".
143
     */
144
    public function __toString(): string
145
    {
146
        return $this->fullCellAddress();
147
    }
148
}