PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Cell / CellAddress.php

CellAddress.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Cell/CellAddress.php

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