From 446e6fbfb30a9484bb86258ae490c85940774e8e Mon Sep 17 00:00:00 2001 From: Daniel Winter Date: Thu, 22 Aug 2024 13:19:44 +0200 Subject: [PATCH] PHP 8.2 Update (#34) * PHP 8.2/8.3 Update * Removed Port/Spreadsheet Co-authored-by: FBGER\dwinter --- .github/workflows/phpcs.yaml | 4 + .github/workflows/phpunit.yaml | 6 +- .github/workflows/psalm.yaml | 4 + .psalm/baseline.xml | 22 ----- composer.json | 14 +-- src/Console/Command/ImportCommand.php | 33 ++----- src/Reader/Factory/ApiReaderFactory.php | 2 +- src/Reader/XlsxReader.php | 126 +++++++++++++++++++++++- 8 files changed, 151 insertions(+), 60 deletions(-) diff --git a/.github/workflows/phpcs.yaml b/.github/workflows/phpcs.yaml index a3548ce..5cd799c 100644 --- a/.github/workflows/phpcs.yaml +++ b/.github/workflows/phpcs.yaml @@ -16,6 +16,10 @@ jobs: phpcs: runs-on: ubuntu-latest steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' - uses: actions/checkout@v2 with: fetch-depth: 0 # important! diff --git a/.github/workflows/phpunit.yaml b/.github/workflows/phpunit.yaml index c1ac414..4ff426d 100644 --- a/.github/workflows/phpunit.yaml +++ b/.github/workflows/phpunit.yaml @@ -20,12 +20,10 @@ jobs: strategy: matrix: php-version: - - "7.4" - - "8.0" - - "8.1" - "8.2" + - "8.3" include: - - php-versions: '8.1' + - php-versions: '8.3' coverage: xdebug composer-prefer: '--prefer-lowest --prefer-stable' phpunit-flags: '--coverage-clover coverage.xml' diff --git a/.github/workflows/psalm.yaml b/.github/workflows/psalm.yaml index 7562804..d2fc2bb 100644 --- a/.github/workflows/psalm.yaml +++ b/.github/workflows/psalm.yaml @@ -19,6 +19,10 @@ jobs: name: Psalm runs-on: ubuntu-latest steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' - uses: actions/checkout@master - name: Psalm diff --git a/.psalm/baseline.xml b/.psalm/baseline.xml index 9589235..2096e31 100644 --- a/.psalm/baseline.xml +++ b/.psalm/baseline.xml @@ -4,17 +4,6 @@ $limit - - $bar - - - advance - finish - setRedrawFrequency - - - ProgressBar - @@ -29,14 +18,6 @@ createTable - - - $count - - - $count - - scalarNode @@ -52,9 +33,6 @@ $value - - $loader->load('services.yaml') - diff --git a/composer.json b/composer.json index ab32d58..46bcb01 100644 --- a/composer.json +++ b/composer.json @@ -20,17 +20,17 @@ } ], "require": { - "php": "^7.4|^8.0|^8.1", + "php": ">=8.2", "ext-zlib": "*", "doctrine/persistence": "^2.4|^3.0", "guzzlehttp/guzzle": "^7.5", "portphp/csv": "^2.0", - "portphp/spreadsheet": "^1.0", - "symfony/config": "^4.4|^5.0|^6.0", - "symfony/filesystem": "^4.4|^5.0|^6.0", - "symfony/framework-bundle": "^4.4|^5.0|^6.0", - "symfony/string": "^4.4|^5.0|^6.0", - "webmozart/assert" : "^1.11" + "symfony/config": "^6.0", + "symfony/filesystem": "^6.0", + "symfony/framework-bundle": "^6.0", + "symfony/string": "^6.0", + "webmozart/assert" : "^1.11", + "phpoffice/phpspreadsheet": "^2.1" }, "require-dev": { "fastbolt/test-helpers": "^0.1.1", diff --git a/src/Console/Command/ImportCommand.php b/src/Console/Command/ImportCommand.php index ef78946..8445d05 100644 --- a/src/Console/Command/ImportCommand.php +++ b/src/Console/Command/ImportCommand.php @@ -12,6 +12,8 @@ use Fastbolt\EntityImporter\Console\ImportCommandResultRenderer; use Fastbolt\EntityImporter\EntityImporterManager; use Fastbolt\EntityImporter\Exceptions\ImporterDefinitionNotFoundException; +use Fastbolt\EntityImporter\Exceptions\InvalidInputFormatException; +use Fastbolt\EntityImporter\Exceptions\SourceUnavailableException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; @@ -24,33 +26,16 @@ class ImportCommand extends Command { /** - * @var EntityImporterManager - */ - private EntityImporterManager $entityImporterManager; - - /** - * @var ImportCommandHelpRenderer - */ - private ImportCommandHelpRenderer $helpRenderer; - - /** - * @var ImportCommandResultRenderer - */ - private ImportCommandResultRenderer $resultRenderer; - - /** - * @param EntityImporterManager $entityImporterManager + * @param EntityImporterManager $entityImporterManager + * @param ImportCommandHelpRenderer $helpRenderer + * @param ImportCommandResultRenderer $resultRenderer */ public function __construct( - EntityImporterManager $entityImporterManager, - ImportCommandHelpRenderer $helpRenderer, - ImportCommandResultRenderer $resultRenderer + private readonly EntityImporterManager $entityImporterManager, + private readonly ImportCommandHelpRenderer $helpRenderer, + private readonly ImportCommandResultRenderer $resultRenderer ) { parent::__construct(); - - $this->entityImporterManager = $entityImporterManager; - $this->helpRenderer = $helpRenderer; - $this->resultRenderer = $resultRenderer; } /** @@ -69,6 +54,8 @@ protected function configure(): void * @param OutputInterface $output * * @return int + * @throws InvalidInputFormatException + * @throws SourceUnavailableException */ protected function execute(InputInterface $input, OutputInterface $output): int { diff --git a/src/Reader/Factory/ApiReaderFactory.php b/src/Reader/Factory/ApiReaderFactory.php index fa8e31a..51aad8c 100644 --- a/src/Reader/Factory/ApiReaderFactory.php +++ b/src/Reader/Factory/ApiReaderFactory.php @@ -24,7 +24,7 @@ class ApiReaderFactory implements ReaderFactoryInterface */ public function __construct(?callable $clientFactory = null) { - if (!$clientFactory) { + if ($clientFactory === null) { $clientFactory = static function (): Client { return new Client(['verify' => false]); }; diff --git a/src/Reader/XlsxReader.php b/src/Reader/XlsxReader.php index 90a6460..bb0fe35 100644 --- a/src/Reader/XlsxReader.php +++ b/src/Reader/XlsxReader.php @@ -8,13 +8,13 @@ namespace Fastbolt\EntityImporter\Reader; -use Port\Spreadsheet\SpreadsheetReader; +use PhpOffice\PhpSpreadsheet\IOFactory; use SplFileObject; /** * @psalm-suppress PropertyNotSetInConstructor */ -class XlsxReader extends SpreadsheetReader implements ReaderInterface +class XlsxReader implements ReaderInterface { /** * Faulty rows @@ -23,6 +23,33 @@ class XlsxReader extends SpreadsheetReader implements ReaderInterface */ protected ?array $errors = null; + /** + * @var array + */ + protected array $columnHeaders; + + /** + * Total number of rows + * + * @var int + */ + protected int $count; + + /** + * @var int|null + */ + protected ?int $headerRowNumber = null; + + /** + * @var int + */ + protected int $pointer = 0; + + /** + * @var array + */ + protected array $worksheet; + /** * @param SplFileObject $file * @param array $columnHeaders @@ -30,7 +57,12 @@ class XlsxReader extends SpreadsheetReader implements ReaderInterface */ public function __construct(SplFileObject $file, array $columnHeaders, ?int $headerRowNumber) { - parent::__construct($file, $headerRowNumber); + $reader = IOFactory::createReaderForFile($file->getPathName()); + $reader->setReadDataOnly(true); + + $spreadsheet = $reader->load($file->getPathname()); + + $this->worksheet = $spreadsheet->getActiveSheet()->toArray(); if (null !== $headerRowNumber) { $this->setHeaderRowNumber($headerRowNumber); @@ -38,6 +70,16 @@ public function __construct(SplFileObject $file, array $columnHeaders, ?int $hea $this->setColumnHeaders($columnHeaders); } + /** + * @param array $columnHeaders + * + * @return void + */ + public function setColumnHeaders(array $columnHeaders): void + { + $this->columnHeaders = $columnHeaders; + } + /** * @inheritDoc */ @@ -61,4 +103,82 @@ public function getErrors(): array /** @psalm-var array> */ return $this->errors; } + + /** + * @return mixed + */ + public function current(): mixed + { + /** @var array $row */ + $row = $this->worksheet[$this->pointer]; + + // If the spreadsheet file has column headers, use them to construct an associative + // array for the columns in this line + if (!empty($this->columnHeaders) && count($this->columnHeaders) === count($row)) { + /** @var array $headers */ + $headers = array_values($this->columnHeaders); + return array_combine($headers, $row); + } + + // Else just return the column values + return $row; + } + + /** + * @param int $rowNumber + * + * @return void + */ + public function setHeaderRowNumber(int $rowNumber): void + { + $this->headerRowNumber = $rowNumber; + /** + * @psalm-suppress MixedAssignment + */ + $this->columnHeaders = $this->worksheet[$rowNumber]; + } + + /** + * @return array + */ + public function getColumnHeaders(): array + { + return $this->columnHeaders; + } + + /** + * @return void + */ + public function next(): void + { + $this->pointer++; + } + + /** + * @return int + */ + public function key(): int + { + return $this->pointer; + } + + /** + * @return bool + */ + public function valid(): bool + { + return isset($this->worksheet[$this->pointer]); + } + + /** + * @return void + */ + public function rewind(): void + { + if (null === $this->headerRowNumber) { + $this->pointer = 0; + } else { + $this->pointer = $this->headerRowNumber + 1; + } + } }