| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Cell; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 6 |
|
| 7 |
class RowRange implements AddressRange |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var ?Worksheet |
| 11 |
*/ |
| 12 |
protected $worksheet; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var int |
| 16 |
*/ |
| 17 |
protected $from; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $to; |
| 23 |
|
| 24 |
public function __construct(int $from, ?int $to = null, ?Worksheet $worksheet = null) |
| 25 |
{ |
| 26 |
$this->validateFromTo($from, $to ?? $from); |
| 27 |
$this->worksheet = $worksheet; |
| 28 |
} |
| 29 |
|
| 30 |
public static function fromArray(array $array, ?Worksheet $worksheet = null): self |
| 31 |
{ |
| 32 |
[$from, $to] = $array; |
| 33 |
|
| 34 |
return new self($from, $to, $worksheet); |
| 35 |
} |
| 36 |
|
| 37 |
private function validateFromTo(int $from, int $to): void |
| 38 |
{ |
| 39 |
// Identify actual top and bottom values (in case we've been given bottom and top) |
| 40 |
$this->from = min($from, $to); |
| 41 |
$this->to = max($from, $to); |
| 42 |
} |
| 43 |
|
| 44 |
public function from(): int |
| 45 |
{ |
| 46 |
return $this->from; |
| 47 |
} |
| 48 |
|
| 49 |
public function to(): int |
| 50 |
{ |
| 51 |
return $this->to; |
| 52 |
} |
| 53 |
|
| 54 |
public function rowCount(): int |
| 55 |
{ |
| 56 |
return $this->to - $this->from + 1; |
| 57 |
} |
| 58 |
|
| 59 |
public function shiftRight(int $offset = 1): self |
| 60 |
{ |
| 61 |
$newFrom = $this->from + $offset; |
| 62 |
$newFrom = ($newFrom < 1) ? 1 : $newFrom; |
| 63 |
|
| 64 |
$newTo = $this->to + $offset; |
| 65 |
$newTo = ($newTo < 1) ? 1 : $newTo; |
| 66 |
|
| 67 |
return new self($newFrom, $newTo, $this->worksheet); |
| 68 |
} |
| 69 |
|
| 70 |
public function shiftLeft(int $offset = 1): self |
| 71 |
{ |
| 72 |
return $this->shiftRight(0 - $offset); |
| 73 |
} |
| 74 |
|
| 75 |
public function toCellRange(): CellRange |
| 76 |
{ |
| 77 |
return new CellRange( |
| 78 |
CellAddress::fromColumnAndRow(Coordinate::columnIndexFromString('A'), $this->from, $this->worksheet), |
| 79 |
CellAddress::fromColumnAndRow(Coordinate::columnIndexFromString(AddressRange::MAX_COLUMN), $this->to) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
public function __toString(): string |
| 84 |
{ |
| 85 |
if ($this->worksheet !== null) { |
| 86 |
$title = str_replace("'", "''", $this->worksheet->getTitle()); |
| 87 |
|
| 88 |
return "'{$title}'!{$this->from}:{$this->to}"; |
| 89 |
} |
| 90 |
|
| 91 |
return "{$this->from}:{$this->to}"; |
| 92 |
} |
| 93 |
} |
| 94 |
|