| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Cell; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 6 |
use Stringable; |
| 7 |
|
| 8 |
/** |
| 9 |
* @implements AddressRange<string> |
| 10 |
*/ |
| 11 |
class ColumnRange implements AddressRange |
| 12 |
{ |
| 13 |
protected ?Worksheet $worksheet; |
| 14 |
|
| 15 |
protected int $from; |
| 16 |
|
| 17 |
protected int $to; |
| 18 |
|
| 19 |
public function __construct(string $from, ?string $to = null, ?Worksheet $worksheet = null) |
| 20 |
{ |
| 21 |
$this->validateFromTo( |
| 22 |
Coordinate::columnIndexFromString($from), |
| 23 |
Coordinate::columnIndexFromString($to ?? $from) |
| 24 |
); |
| 25 |
$this->worksheet = $worksheet; |
| 26 |
} |
| 27 |
|
| 28 |
public function __destruct() |
| 29 |
{ |
| 30 |
$this->worksheet = null; |
| 31 |
} |
| 32 |
|
| 33 |
public static function fromColumnIndexes(int $from, int $to, ?Worksheet $worksheet = null): self |
| 34 |
{ |
| 35 |
return new self(Coordinate::stringFromColumnIndex($from), Coordinate::stringFromColumnIndex($to), $worksheet); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param array<int|string> $array |
| 40 |
*/ |
| 41 |
public static function fromArray(array $array, ?Worksheet $worksheet = null): self |
| 42 |
{ |
| 43 |
array_walk( |
| 44 |
$array, |
| 45 |
function (&$column): void { |
| 46 |
$column = is_numeric($column) ? Coordinate::stringFromColumnIndex((int) $column) : $column; |
| 47 |
} |
| 48 |
); |
| 49 |
/** @var string $from */ |
| 50 |
/** @var string $to */ |
| 51 |
[$from, $to] = $array; |
| 52 |
|
| 53 |
return new self($from, $to, $worksheet); |
| 54 |
} |
| 55 |
|
| 56 |
private function validateFromTo(int $from, int $to): void |
| 57 |
{ |
| 58 |
// Identify actual top and bottom values (in case we've been given bottom and top) |
| 59 |
$this->from = min($from, $to); |
| 60 |
$this->to = max($from, $to); |
| 61 |
} |
| 62 |
|
| 63 |
public function columnCount(): int |
| 64 |
{ |
| 65 |
return $this->to - $this->from + 1; |
| 66 |
} |
| 67 |
|
| 68 |
public function shiftDown(int $offset = 1): self |
| 69 |
{ |
| 70 |
$newFrom = $this->from + $offset; |
| 71 |
$newFrom = ($newFrom < 1) ? 1 : $newFrom; |
| 72 |
|
| 73 |
$newTo = $this->to + $offset; |
| 74 |
$newTo = ($newTo < 1) ? 1 : $newTo; |
| 75 |
|
| 76 |
return self::fromColumnIndexes($newFrom, $newTo, $this->worksheet); |
| 77 |
} |
| 78 |
|
| 79 |
public function shiftUp(int $offset = 1): self |
| 80 |
{ |
| 81 |
return $this->shiftDown(0 - $offset); |
| 82 |
} |
| 83 |
|
| 84 |
public function from(): string |
| 85 |
{ |
| 86 |
return Coordinate::stringFromColumnIndex($this->from); |
| 87 |
} |
| 88 |
|
| 89 |
public function to(): string |
| 90 |
{ |
| 91 |
return Coordinate::stringFromColumnIndex($this->to); |
| 92 |
} |
| 93 |
|
| 94 |
public function fromIndex(): int |
| 95 |
{ |
| 96 |
return $this->from; |
| 97 |
} |
| 98 |
|
| 99 |
public function toIndex(): int |
| 100 |
{ |
| 101 |
return $this->to; |
| 102 |
} |
| 103 |
|
| 104 |
public function toCellRange(): CellRange |
| 105 |
{ |
| 106 |
return new CellRange( |
| 107 |
CellAddress::fromColumnAndRow($this->from, 1, $this->worksheet), |
| 108 |
CellAddress::fromColumnAndRow($this->to, AddressRange::MAX_ROW) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
public function __toString(): string |
| 113 |
{ |
| 114 |
$from = $this->from(); |
| 115 |
$to = $this->to(); |
| 116 |
|
| 117 |
if ($this->worksheet !== null) { |
| 118 |
$title = str_replace("'", "''", $this->worksheet->getTitle()); |
| 119 |
|
| 120 |
return "'{$title}'!{$from}:{$to}"; |
| 121 |
} |
| 122 |
|
| 123 |
return "{$from}:{$to}"; |
| 124 |
} |
| 125 |
} |
| 126 |
|