Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
47 changes: 17 additions & 30 deletions src/SpreadsheetReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand All @@ -122,7 +120,7 @@ public function count()
*
* @author Derek Chafin <infomaniac50@gmail.com>
*/
public function current()
public function current(): mixed
{
$row = $this->worksheet[$this->pointer];

Expand All @@ -141,7 +139,7 @@ public function current()
*
* @return array
*/
public function getColumnHeaders()
public function getColumnHeaders(): array
{
return $this->columnHeaders;
}
Expand All @@ -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);

Expand All @@ -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++;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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];
Expand All @@ -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]);
}
Expand Down
13 changes: 7 additions & 6 deletions src/SpreadsheetReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
namespace Port\Spreadsheet;

use Port\Reader;
use Port\Reader\ReaderFactory;

/**
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
23 changes: 10 additions & 13 deletions src/SpreadsheetWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
namespace Port\Spreadsheet;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Port\Writer;
Expand Down Expand Up @@ -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;
Expand All @@ -81,21 +82,17 @@ 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);
}

/**
* 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)) {
Expand All @@ -116,26 +113,26 @@ 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);

if ($this->prependHeaderRow && 1 === $this->row) {
$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++;
}

$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++;
Expand Down