Typed coordinate and range parameters

This commit is contained in:
Adrien Crivelli
2024-01-22 23:26:04 +08:00
parent ad9fe0a13b
commit 3dce900eff
5 changed files with 36 additions and 34 deletions
+2 -2
View File
@@ -53,7 +53,7 @@ class AutoFilter implements Stringable
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
*/
public function __construct($range = '', ?Worksheet $worksheet = null)
public function __construct(AddressRange|string|array $range = '', ?Worksheet $worksheet = null)
{
if ($range !== '') {
[, $range] = Worksheet::extractSheetTitle(Validations::validateCellRange($range), true);
@@ -105,7 +105,7 @@ class AutoFilter implements Stringable
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
*/
public function setRange($range = ''): self
public function setRange(AddressRange|string|array $range = ''): self
{
$this->evaluated = false;
// extract coordinate
+4 -2
View File
@@ -14,8 +14,10 @@ class PageBreak
private int $maxColOrRow;
/** @param array|CellAddress|string $coordinate */
public function __construct(int $breakType, $coordinate, int $maxColOrRow = -1)
/**
* @param array{0: int, 1: int}|CellAddress|string $coordinate
*/
public function __construct(int $breakType, CellAddress|string|array $coordinate, int $maxColOrRow = -1)
{
$coordinate = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
$this->breakType = $breakType;
+2 -2
View File
@@ -69,7 +69,7 @@ class Table implements Stringable
* or an AddressRange object.
* @param string $name (e.g. Table1)
*/
public function __construct($range = '', string $name = '')
public function __construct(AddressRange|string|array $range = '', string $name = '')
{
$this->style = new TableStyle();
$this->autoFilter = new AutoFilter($range);
@@ -272,7 +272,7 @@ class Table implements Stringable
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or an AddressRange object.
*/
public function setRange($range = ''): self
public function setRange(AddressRange|string|array $range = ''): self
{
// extract coordinate
if ($range !== '') {
+4 -4
View File
@@ -12,10 +12,10 @@ class Validations
/**
* Validate a cell address.
*
* @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
* @param null|array{0: int, 1: int}|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*/
public static function validateCellAddress($cellAddress): string
public static function validateCellAddress(null|CellAddress|string|array $cellAddress): string
{
if (is_string($cellAddress)) {
[$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true);
@@ -40,7 +40,7 @@ class Validations
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
* or as a CellAddress or AddressRange object.
*/
public static function validateCellOrCellRange($cellRange): string
public static function validateCellOrCellRange(AddressRange|CellAddress|int|string|array $cellRange): string
{
if (is_string($cellRange) || is_numeric($cellRange)) {
// Convert a single column reference like 'A' to 'A:A',
@@ -63,7 +63,7 @@ class Validations
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
* or as an AddressRange object.
*/
public static function validateCellRange(AddressRange|array|string $cellRange): string
public static function validateCellRange(AddressRange|string|array $cellRange): string
{
if (is_string($cellRange)) {
[$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);
+24 -24
View File
@@ -1103,14 +1103,14 @@ class Worksheet implements IComparable
/**
* Set a cell value.
*
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
* @param mixed $value Value for the cell
* @param null|IValueBinder $binder Value Binder to override the currently set Value Binder
*
* @return $this
*/
public function setCellValue($coordinate, mixed $value, ?IValueBinder $binder = null): static
public function setCellValue(CellAddress|string|array $coordinate, mixed $value, ?IValueBinder $binder = null): static
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
$this->getCell($cellAddress)->setValue($value, $binder);
@@ -1121,7 +1121,7 @@ class Worksheet implements IComparable
/**
* Set a cell value.
*
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
* @param mixed $value Value of the cell
* @param string $dataType Explicit data type, see DataType::TYPE_*
@@ -1135,7 +1135,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function setCellValueExplicit($coordinate, mixed $value, string $dataType): static
public function setCellValueExplicit(CellAddress|string|array $coordinate, mixed $value, string $dataType): static
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
$this->getCell($cellAddress)->setValueExplicit($value, $dataType);
@@ -1146,7 +1146,7 @@ class Worksheet implements IComparable
/**
* Get cell at a specific coordinate.
*
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*
* @return Cell Cell that was found or created
@@ -1156,7 +1156,7 @@ class Worksheet implements IComparable
* the "active" cell, and any previous assignment becomes a disconnected reference because
* the active cell has changed.
*/
public function getCell($coordinate): Cell
public function getCell(CellAddress|string|array $coordinate): Cell
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
@@ -1299,10 +1299,10 @@ class Worksheet implements IComparable
/**
* Does the cell at a specific coordinate exist?
*
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*/
public function cellExists($coordinate): bool
public function cellExists(CellAddress|string|array $coordinate): bool
{
$cellAddress = Validations::validateCellAddress($coordinate);
[$sheet, $finalCoordinate] = $this->getWorksheetAndCoordinate($cellAddress);
@@ -1388,7 +1388,7 @@ class Worksheet implements IComparable
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or a CellAddress or AddressRange object.
*/
public function getStyle($cellCoordinate): Style
public function getStyle(AddressRange|CellAddress|int|string|array $cellCoordinate): Style
{
$cellCoordinate = Validations::validateCellOrCellRange($cellCoordinate);
@@ -1589,13 +1589,13 @@ class Worksheet implements IComparable
/**
* Set break on a cell.
*
* @param array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
* @param int $break Break type (type of Worksheet::BREAK_*)
*
* @return $this
*/
public function setBreak($coordinate, int $break, int $max = -1): static
public function setBreak(CellAddress|string|array $coordinate, int $break, int $max = -1): static
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate));
@@ -1692,7 +1692,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function mergeCells($range, string $behaviour = self::MERGE_CELL_CONTENT_EMPTY): static
public function mergeCells(AddressRange|string|array $range, string $behaviour = self::MERGE_CELL_CONTENT_EMPTY): static
{
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
@@ -1812,7 +1812,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function unmergeCells($range): static
public function unmergeCells(AddressRange|string|array $range): static
{
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
@@ -1865,7 +1865,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function protectCells($range, string $password, bool $alreadyHashed = false): static
public function protectCells(AddressRange|CellAddress|int|string|array $range, string $password, bool $alreadyHashed = false): static
{
$range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range));
@@ -1886,7 +1886,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function unprotectCells($range): static
public function unprotectCells(AddressRange|CellAddress|int|string|array $range): static
{
$range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range));
@@ -1927,7 +1927,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function setAutoFilter($autoFilterOrRange): static
public function setAutoFilter(AddressRange|string|array|AutoFilter $autoFilterOrRange): static
{
if (is_object($autoFilterOrRange) && ($autoFilterOrRange instanceof AutoFilter)) {
$this->autoFilter = $autoFilterOrRange;
@@ -2063,16 +2063,16 @@ class Worksheet implements IComparable
* - B1 will freeze the columns to the left of cell B1 (i.e column A)
* - B2 will freeze the rows above and to the left of cell B2 (i.e row 1 and column A)
*
* @param null|array<int>|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* @param null|array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
* Passing a null value for this argument will clear any existing freeze pane for this worksheet.
* @param null|array<int>|CellAddress|string $topLeftCell default position of the right bottom pane
* @param null|array{0: int, 1: int}|CellAddress|string $topLeftCell default position of the right bottom pane
* Coordinate of the cell as a string, eg: 'C5'; or as an array of [$columnIndex, $row] (e.g. [3, 5]),
* or a CellAddress object.
*
* @return $this
*/
public function freezePane($coordinate, $topLeftCell = null, bool $frozenSplit = false): static
public function freezePane(null|CellAddress|string|array $coordinate, null|CellAddress|string|array $topLeftCell = null, bool $frozenSplit = false): static
{
$this->panes = [
'bottomRight' => null,
@@ -2577,12 +2577,12 @@ class Worksheet implements IComparable
/**
* Remove comment from cell.
*
* @param array<int>|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*
* @return $this
*/
public function removeComment($cellCoordinate): self
public function removeComment(CellAddress|string|array $cellCoordinate): self
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
@@ -2604,10 +2604,10 @@ class Worksheet implements IComparable
/**
* Get comment for cell.
*
* @param array<int>|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*/
public function getComment($cellCoordinate): Comment
public function getComment(CellAddress|string|array $cellCoordinate): Comment
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
@@ -2670,7 +2670,7 @@ class Worksheet implements IComparable
*
* @return $this
*/
public function setSelectedCells($coordinate): static
public function setSelectedCells(AddressRange|CellAddress|int|string|array $coordinate): static
{
if (is_string($coordinate)) {
$coordinate = Validations::definedNameToCoordinate($coordinate, $this);