From e351b386d6b7ac9155e60b8a83d896a0e5196db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Sun, 21 Jan 2024 20:36:39 +0100 Subject: [PATCH] wip --- composer.json | 3 +- phpstan.neon => phpstan.dist.neon | 3 + src/AbstractExtension.php | 4 + src/AbstractRegistry.php | 5 ++ .../Extension/PreloadedActionExtension.php | 4 +- .../Orm/Filter/Type/CallbackFilterType.php | 37 ++++++++ .../Exporter/OpenSpoutExportHandler.php | 85 ------------------- src/Column/ColumnFactoryBuilder.php | 21 ----- .../Extension/PreloadedColumnExtension.php | 4 +- src/Column/Type/ColumnType.php | 2 - src/DataTable.php | 4 +- .../Extension/PreloadedExporterExtension.php | 9 +- src/Extension/PreloadedDataTableExtension.php | 4 +- .../Extension/PreloadedFilterExtension.php | 4 +- src/Filter/Type/SearchFilterType.php | 2 +- src/Type/DataTableType.php | 2 +- src/Type/ResolvedDataTableTypeInterface.php | 7 ++ tests/Unit/Filter/FilterRegistryTest.php | 4 +- 18 files changed, 77 insertions(+), 127 deletions(-) rename phpstan.neon => phpstan.dist.neon (83%) create mode 100644 src/Bridge/Doctrine/Orm/Filter/Type/CallbackFilterType.php delete mode 100644 src/Bridge/OpenSpout/Exporter/OpenSpoutExportHandler.php diff --git a/composer.json b/composer.json index 51ed21ce..2c3bf0eb 100755 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "doctrine/doctrine-bundle": "^2.9", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^10.4", - "dg/bypass-finals": "dev-master" + "dg/bypass-finals": "dev-master", + "openspout/openspout": "^4.23" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.dist.neon similarity index 83% rename from phpstan.neon rename to phpstan.dist.neon index fe607c2e..ab2834f1 100755 --- a/phpstan.neon +++ b/phpstan.dist.neon @@ -1,6 +1,9 @@ parameters: level: 5 checkGenericClassInNonGenericObjectType: false + paths: + - src + - tests excludePaths: - src/Resources/skeleton/* - src/DependencyInjection/Configuration.php \ No newline at end of file diff --git a/src/AbstractExtension.php b/src/AbstractExtension.php index 4a056d1f..e0357834 100644 --- a/src/AbstractExtension.php +++ b/src/AbstractExtension.php @@ -115,6 +115,10 @@ private function initTypeExtensions(): void $typeExtensionClass = $this->getTypeExtensionClass(); foreach ($this->loadTypeExtensions() as $extensions) { + if (!is_array($extensions)) { + $extensions = [$extensions]; + } + foreach ($extensions as $extension) { if (!$extension instanceof $typeExtensionClass) { throw new UnexpectedTypeException($extension, $typeExtensionClass); diff --git a/src/AbstractRegistry.php b/src/AbstractRegistry.php index de92d663..60de0938 100644 --- a/src/AbstractRegistry.php +++ b/src/AbstractRegistry.php @@ -133,6 +133,11 @@ private function resolveType($type) } } + /** + * @return TResolvedType + */ + abstract public function getType(string $name); + /** * @return class-string */ diff --git a/src/Action/Extension/PreloadedActionExtension.php b/src/Action/Extension/PreloadedActionExtension.php index a05dd1b9..fa05d562 100644 --- a/src/Action/Extension/PreloadedActionExtension.php +++ b/src/Action/Extension/PreloadedActionExtension.php @@ -9,8 +9,8 @@ class PreloadedActionExtension extends AbstractActionExtension { /** - * @param array $types - * @param array> $typeExtensions + * @param array $types + * @param array|array> $typeExtensions */ public function __construct( private readonly array $types = [], diff --git a/src/Bridge/Doctrine/Orm/Filter/Type/CallbackFilterType.php b/src/Bridge/Doctrine/Orm/Filter/Type/CallbackFilterType.php new file mode 100644 index 00000000..1b2a0b89 --- /dev/null +++ b/src/Bridge/Doctrine/Orm/Filter/Type/CallbackFilterType.php @@ -0,0 +1,37 @@ +setDefault('supported_operators', Operator::cases()) + ->setRequired('callback') + ->setAllowedTypes('callback', ['callable']) + ; + } +} diff --git a/src/Bridge/OpenSpout/Exporter/OpenSpoutExportHandler.php b/src/Bridge/OpenSpout/Exporter/OpenSpoutExportHandler.php deleted file mode 100644 index 2cfc7fa6..00000000 --- a/src/Bridge/OpenSpout/Exporter/OpenSpoutExportHandler.php +++ /dev/null @@ -1,85 +0,0 @@ -tempnam( - $exporter->getConfig()->getOption('tempnam_dir'), - $exporter->getConfig()->getOption('tempnam_prefix'), - ); - - $this->writer->openToFile($outputFilePath); - - if ($exporter->getConfig()->getOption('use_headers')) { - $this->writer->addRow(new Row( - cells: $this->getHeaderRowCells($view->vars['headerRow'], $exporter), - style: $exporter->getConfig()->getOption('header_row_style', new Style()), - )); - } - - foreach ($view->vars['valueRows'] as $valueRow) { - $this->writer->addRow(new Row( - cells: $this->getValueRowCells($valueRow, $exporter), - style: $exporter->getConfig()->getOption('value_row_style', new Style()), - )); - } - - $this->writer->close(); - - return new ExportFile($outputFilePath, sprintf('%s.%s', $filename, $this->getExtension())); - } - - private function getExtension(): string - { - return match (get_class($this->writer)) { - Writer\CSV\Writer::class => 'csv', - Writer\XLSX\Writer::class => 'xlsx', - Writer\ODS\Writer::class => 'ods', - }; - } - - private function getHeaderRowCells(HeaderRowView $view, ExporterInterface $exporter): array - { - return array_map( - fn (ColumnHeaderView $columnHeaderView) => Cell::fromValue( - value: $view->vars['label'], - style: $exporter->getConfig()->getOption('header_cell_style'), - ), - $view->children, - ); - } - - private function getValueRowCells(ValueRowView $view, ExporterInterface $exporter): array - { - return array_map( - fn (ColumnValueView $columnValueView) => Cell::fromValue( - value: $view->vars['value'], - style: $exporter->getConfig()->getOption('value_cell_style'), - ), - $view->children, - ); - } -} diff --git a/src/Column/ColumnFactoryBuilder.php b/src/Column/ColumnFactoryBuilder.php index 1cd4b72c..c8ca5c41 100644 --- a/src/Column/ColumnFactoryBuilder.php +++ b/src/Column/ColumnFactoryBuilder.php @@ -6,7 +6,6 @@ use Kreyu\Bundle\DataTableBundle\Column\Extension\ColumnExtensionInterface; use Kreyu\Bundle\DataTableBundle\Column\Extension\ColumnTypeExtensionInterface; -use Kreyu\Bundle\DataTableBundle\Column\Extension\CoreColumnExtension; use Kreyu\Bundle\DataTableBundle\Column\Extension\PreloadedColumnExtension; use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface; use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeFactory; @@ -31,11 +30,6 @@ class ColumnFactoryBuilder implements ColumnFactoryBuilderInterface */ private array $typeExtensions = []; - public function __construct( - private readonly bool $forceCoreExtension = false, - ) { - } - public function setResolvedTypeFactory(ResolvedColumnTypeFactoryInterface $resolvedTypeFactory): static { $this->resolvedTypeFactory = $resolvedTypeFactory; @@ -95,21 +89,6 @@ public function getColumnFactory(): ColumnFactoryInterface { $extensions = $this->extensions; - if ($this->forceCoreExtension) { - $hasCoreExtension = false; - - foreach ($extensions as $extension) { - if ($extension instanceof CoreColumnExtension) { - $hasCoreExtension = true; - break; - } - } - - if (!$hasCoreExtension) { - array_unshift($extensions, new CoreColumnExtension()); - } - } - if (\count($this->types) > 0 || \count($this->typeExtensions) > 0) { $extensions[] = new PreloadedColumnExtension($this->types, $this->typeExtensions); } diff --git a/src/Column/Extension/PreloadedColumnExtension.php b/src/Column/Extension/PreloadedColumnExtension.php index f7e44bba..d93a89e2 100644 --- a/src/Column/Extension/PreloadedColumnExtension.php +++ b/src/Column/Extension/PreloadedColumnExtension.php @@ -9,8 +9,8 @@ class PreloadedColumnExtension extends AbstractColumnExtension { /** - * @param array $types - * @param array> $typeExtensions + * @param array $types + * @param array|array> $typeExtensions */ public function __construct( private readonly array $types = [], diff --git a/src/Column/Type/ColumnType.php b/src/Column/Type/ColumnType.php index 9547ed07..48868c69 100755 --- a/src/Column/Type/ColumnType.php +++ b/src/Column/Type/ColumnType.php @@ -127,8 +127,6 @@ public function buildExportHeaderView(ColumnHeaderView $view, ColumnInterface $c public function buildExportValueView(ColumnValueView $view, ColumnInterface $column, array $options): void { - return; - if (false === $options['export']) { return; } diff --git a/src/DataTable.php b/src/DataTable.php index f7e36749..c779f50d 100755 --- a/src/DataTable.php +++ b/src/DataTable.php @@ -170,8 +170,8 @@ public function getColumns(): array { $columns = $this->columns; - uasort($columns, static function (ColumnInterface $columnA, ColumnInterface $columnB) { - return $columnA->getPriority() < $columnB->getPriority(); + uasort($columns, static function (ColumnInterface $columnA, ColumnInterface $columnB): int { + return $columnB->getPriority() <=> $columnA->getPriority(); }); return $columns; diff --git a/src/Exporter/Extension/PreloadedExporterExtension.php b/src/Exporter/Extension/PreloadedExporterExtension.php index f553c335..2170dffe 100644 --- a/src/Exporter/Extension/PreloadedExporterExtension.php +++ b/src/Exporter/Extension/PreloadedExporterExtension.php @@ -4,14 +4,13 @@ namespace Kreyu\Bundle\DataTableBundle\Exporter\Extension; -use Kreyu\Bundle\DataTableBundle\Column\Extension\AbstractColumnExtension; -use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface; +use Kreyu\Bundle\DataTableBundle\Exporter\Type\ExporterTypeInterface; -class PreloadedExporterExtension extends AbstractColumnExtension +class PreloadedExporterExtension extends AbstractExporterExtension { /** - * @param array $types - * @param array> $typeExtensions + * @param array $types + * @param array|array> $typeExtensions */ public function __construct( private readonly array $types = [], diff --git a/src/Extension/PreloadedDataTableExtension.php b/src/Extension/PreloadedDataTableExtension.php index b281506a..11df279e 100644 --- a/src/Extension/PreloadedDataTableExtension.php +++ b/src/Extension/PreloadedDataTableExtension.php @@ -9,8 +9,8 @@ class PreloadedDataTableExtension extends AbstractDataTableExtension { /** - * @param array $types - * @param array> $typeExtensions + * @param array $types + * @param array|array> $typeExtensions */ public function __construct( private readonly array $types = [], diff --git a/src/Filter/Extension/PreloadedFilterExtension.php b/src/Filter/Extension/PreloadedFilterExtension.php index 9abf418b..5093396e 100644 --- a/src/Filter/Extension/PreloadedFilterExtension.php +++ b/src/Filter/Extension/PreloadedFilterExtension.php @@ -9,8 +9,8 @@ class PreloadedFilterExtension extends AbstractFilterExtension { /** - * @param array $types - * @param array> $typeExtensions + * @param array $types + * @param array|array> $typeExtensions */ public function __construct( private readonly array $types = [], diff --git a/src/Filter/Type/SearchFilterType.php b/src/Filter/Type/SearchFilterType.php index 7b1cf916..3eff1041 100755 --- a/src/Filter/Type/SearchFilterType.php +++ b/src/Filter/Type/SearchFilterType.php @@ -39,6 +39,6 @@ public function configureOptions(OptionsResolver $resolver): void public function handle(ProxyQueryInterface $query, FilterData $data, FilterInterface $filter): void { - $filter->getOption('handler')($query, (string) $data->getValue(), $filter); + $filter->getConfig()->getOption('handler')($query, (string) $data->getValue(), $filter); } } diff --git a/src/Type/DataTableType.php b/src/Type/DataTableType.php index d9d4aed7..b56f7ff3 100755 --- a/src/Type/DataTableType.php +++ b/src/Type/DataTableType.php @@ -378,7 +378,7 @@ private function createExportValueRowsViews(DataTableView $view, DataTableInterf $valueRowView = new ValueRowView($view, $index, $data); foreach ($columns as $column) { - $valueRowView->children[$column->getName()] = $column->createValueView($valueRowView); + $valueRowView->children[$column->getName()] = $column->createExportValueView($valueRowView); } yield $valueRowView; diff --git a/src/Type/ResolvedDataTableTypeInterface.php b/src/Type/ResolvedDataTableTypeInterface.php index 79562345..e8b245da 100755 --- a/src/Type/ResolvedDataTableTypeInterface.php +++ b/src/Type/ResolvedDataTableTypeInterface.php @@ -32,6 +32,8 @@ public function createBuilder(DataTableFactoryInterface $factory, string $name, public function createView(DataTableInterface $dataTable): DataTableView; + public function createExportView(DataTableInterface $dataTable): DataTableView; + /** * @param array $options */ @@ -42,5 +44,10 @@ public function buildDataTable(DataTableBuilderInterface $builder, array $option */ public function buildView(DataTableView $view, DataTableInterface $dataTable, array $options): void; + /** + * @param array $options + */ + public function buildExportView(DataTableView $view, DataTableInterface $dataTable, array $options): void; + public function getOptionsResolver(): OptionsResolver; } diff --git a/tests/Unit/Filter/FilterRegistryTest.php b/tests/Unit/Filter/FilterRegistryTest.php index d109d2bb..c386a2d4 100644 --- a/tests/Unit/Filter/FilterRegistryTest.php +++ b/tests/Unit/Filter/FilterRegistryTest.php @@ -26,6 +26,7 @@ public function testCallingGetTypeWithNonExistentClassThrowsException(): void { $this->expectExceptionObject(new InvalidArgumentException('Could not load filter type "App\\InvalidFilterType": class does not exist.')); + // @phpstan-ignore-next-line $this->createRegistry()->getType('App\\InvalidFilterType'); } @@ -33,6 +34,7 @@ public function testCallingGetTypeWithInvalidClassThrowsException(): void { $this->expectExceptionObject(new InvalidArgumentException(sprintf('Could not load filter type "%s": class does not implement "%s".', FilterRegistry::class, FilterTypeInterface::class))); + // @phpstan-ignore-next-line $this->createRegistry()->getType(FilterRegistry::class); } @@ -80,7 +82,7 @@ public function testGetTypeResolvesParentUsingExtension(): void $resolvedFilterTypeFactory ->expects($matcher = $this->exactly(2)) ->method('createResolvedType') - ->willReturnCallback(function ($type, $typeExtensions, $parent) use ($matcher, $filterTypeExtension, $parentFilterType) { + ->willReturnCallback(function ($type, $typeExtensions, $parent) use ($matcher, $filterTypeExtension) { // @phpstan-ignore-next-line match ($matcher->numberOfInvocations()) { 1 => $this->assertInstanceOf(FilterType::class, $type),