diff --git a/composer.json b/composer.json index 29eba03..85b6f3f 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,9 @@ "docs": "http://docs.portphp.org" }, "require": { - "php": ">=5.6.0", + "php": ">=8.1", "portphp/portphp": "^1.3", - "phpoffice/phpspreadsheet": "^1.3" + "phpoffice/phpspreadsheet": "^1.3 || ^2.0 || ^3.0 || ^4.0 || ^5.0" }, "autoload": { "psr-4": { diff --git a/src/SpreadsheetReader.php b/src/SpreadsheetReader.php index 1a42b96..1074102 100644 --- a/src/SpreadsheetReader.php +++ b/src/SpreadsheetReader.php @@ -65,17 +65,15 @@ class SpreadsheetReader implements CountableReader, \SeekableIterator */ protected $worksheet; - // phpcs:disable Generic.Files.LineLength.MaxExceeded /** * @param \SplFileObject $file Spreadsheet file - * @param int $headerRowNumber Optional number of header row - * @param int $activeSheet Index of active sheet to read from + * @param int|null $headerRowNumber Optional number of header row + * @param int|null $activeSheet Index of active sheet to read from * @param bool $readOnly If set to false, the reader take care of the spreadsheet formatting (slow) - * @param int $maxRows Maximum number of rows to read + * @param int|null $maxRows Maximum number of rows to read */ - public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null) + public function __construct(\SplFileObject $file, ?int $headerRowNumber = null, ?int $activeSheet = null, bool $readOnly = true, ?int $maxRows = null) { - // phpcs:enable Generic.Files.LineLength.MaxExceeded $reader = IOFactory::createReaderForFile($file->getPathName()); $reader->setReadDataOnly($readOnly); /** @var Spreadsheet $spreadsheet */ @@ -103,7 +101,7 @@ public function __construct(\SplFileObject $file, $headerRowNumber = null, $acti /** * @return int */ - public function count() + public function count(): int { $count = count($this->worksheet); if (null !== $this->headerRowNumber) { @@ -122,7 +120,7 @@ public function count() * * @author Derek Chafin */ - public function current() + public function current(): mixed { $row = $this->worksheet[$this->pointer]; @@ -141,7 +139,7 @@ public function current() * * @return array */ - public function getColumnHeaders() + public function getColumnHeaders(): array { return $this->columnHeaders; } @@ -151,9 +149,9 @@ public function getColumnHeaders() * * @param int $number * - * @return array + * @return array|null */ - public function getRow($number) + public function getRow(int $number): mixed { $this->seek($number); @@ -165,17 +163,15 @@ public function getRow($number) * * @return int */ - public function key() + public function key(): mixed { return $this->pointer; } /** * Move forward to next element - * - * @return void Any returned value is ignored. */ - public function next() + public function next(): void { $this->pointer++; } @@ -186,10 +182,8 @@ public function next() * If a header row has been set, the pointer is set just below the header * row. That way, when you iterate over the rows, that header row is * skipped. - * - * @return void Any returned value is ignored. */ - public function rewind() + public function rewind(): void { if (null === $this->headerRowNumber) { $this->pointer = 0; @@ -204,10 +198,8 @@ public function rewind() * @link http://php.net/manual/en/seekableiterator.seek.php * * @param int $pointer The position to seek to. - * - * @return void Any returned value is ignored. */ - public function seek($pointer) + public function seek(int $pointer): void { $this->pointer = $pointer; } @@ -216,10 +208,8 @@ public function seek($pointer) * Set column headers * * @param array $columnHeaders - * - * @return void Any returned value is ignored. */ - public function setColumnHeaders(array $columnHeaders) + public function setColumnHeaders(array $columnHeaders): void { $this->columnHeaders = $columnHeaders; } @@ -228,10 +218,8 @@ public function setColumnHeaders(array $columnHeaders) * Set header row number * * @param int $rowNumber Number of the row that contains column header names - * - * @return void Any returned value is ignored. */ - public function setHeaderRowNumber($rowNumber) + public function setHeaderRowNumber(int $rowNumber): void { $this->headerRowNumber = $rowNumber; $this->columnHeaders = $this->worksheet[$rowNumber]; @@ -240,10 +228,9 @@ public function setHeaderRowNumber($rowNumber) /** * Checks if current position is valid * - * @return bool The return value will be casted to boolean and then evaluated. - * Returns true on success or false on failure. + * @return bool */ - public function valid() + public function valid(): bool { return isset($this->worksheet[$this->pointer]); } diff --git a/src/SpreadsheetReaderFactory.php b/src/SpreadsheetReaderFactory.php index 086aa97..4e7f8cd 100644 --- a/src/SpreadsheetReaderFactory.php +++ b/src/SpreadsheetReaderFactory.php @@ -24,6 +24,7 @@ */ namespace Port\Spreadsheet; +use Port\Reader; use Port\Reader\ReaderFactory; /** @@ -34,20 +35,20 @@ class SpreadsheetReaderFactory implements ReaderFactory { /** - * @var int + * @var int|null */ protected $activeSheet; /** - * @var int + * @var int|null */ protected $headerRowNumber; /** - * @param int $headerRowNumber - * @param int $activeSheet + * @param int|null $headerRowNumber + * @param int|null $activeSheet */ - public function __construct($headerRowNumber = null, $activeSheet = null) + public function __construct(?int $headerRowNumber = null, ?int $activeSheet = null) { $this->headerRowNumber = $headerRowNumber; $this->activeSheet = $activeSheet; @@ -58,7 +59,7 @@ public function __construct($headerRowNumber = null, $activeSheet = null) * * @return SpreadsheetReader */ - public function getReader(\SplFileObject $file) + public function getReader(\SplFileObject $file): Reader { return new SpreadsheetReader($file, $this->headerRowNumber, $this->activeSheet); } diff --git a/src/SpreadsheetWriter.php b/src/SpreadsheetWriter.php index 05d471e..ab3b35d 100644 --- a/src/SpreadsheetWriter.php +++ b/src/SpreadsheetWriter.php @@ -24,6 +24,7 @@ */ namespace Port\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use Port\Writer; @@ -67,11 +68,11 @@ class SpreadsheetWriter implements Writer /** * @param \SplFileObject $file File - * @param string $sheet Sheet title (optional) + * @param string|null $sheet Sheet title (optional) * @param string $type Spreadsheet file type (defaults to Xlsx) * @param bool $prependHeaderRow */ - public function __construct(\SplFileObject $file, $sheet = null, $type = 'Xlsx', $prependHeaderRow = false) + public function __construct(\SplFileObject $file, ?string $sheet = null, string $type = 'Xlsx', bool $prependHeaderRow = false) { $this->filename = $file->getPathname(); $this->sheet = $sheet; @@ -81,10 +82,8 @@ public function __construct(\SplFileObject $file, $sheet = null, $type = 'Xlsx', /** * Wrap up the writer after all items have been written - * - * @return void Any returned value is ignored. */ - public function finish() + public function finish(): void { $writer = IOFactory::createWriter($this->spreadsheet, $this->type); $writer->save($this->filename); @@ -92,10 +91,8 @@ public function finish() /** * Prepare the writer before writing the items - * - * @return void Any returned value is ignored. */ - public function prepare() + public function prepare(): void { $reader = IOFactory::createReader($this->type); if ($reader->canRead($this->filename)) { @@ -116,10 +113,8 @@ public function prepare() * Write one data item * * @param array $item The data item with converted values - * - * @return void Any returned value is ignored. */ - public function writeItem(array $item) + public function writeItem(array $item): void { $count = count($item); @@ -127,7 +122,8 @@ public function writeItem(array $item) $headers = array_keys($item); for ($i = 0; $i < $count; $i++) { - $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $headers[$i]); + $col = Coordinate::stringFromColumnIndex($i + 1); + $this->spreadsheet->getActiveSheet()->setCellValue($col . $this->row, $headers[$i]); } $this->row++; } @@ -135,7 +131,8 @@ public function writeItem(array $item) $values = array_values($item); for ($i = 0; $i < $count; $i++) { - $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $values[$i]); + $col = Coordinate::stringFromColumnIndex($i + 1); + $this->spreadsheet->getActiveSheet()->setCellValue($col . $this->row, $values[$i]); } $this->row++;