From 155203cf9c85ed1c97db061a789a0680a1fe322f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Fri, 28 Jun 2024 10:41:53 +0200 Subject: [PATCH 1/2] Add create methods to data table builder --- src/DataTableBuilder.php | 60 +++++++++++++++++++++++ src/DataTableBuilderInterface.php | 30 ++++++++++++ tests/Unit/DataTableBuilderTest.php | 76 +++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) diff --git a/src/DataTableBuilder.php b/src/DataTableBuilder.php index 7b4db99d..de5717b8 100755 --- a/src/DataTableBuilder.php +++ b/src/DataTableBuilder.php @@ -195,6 +195,15 @@ public function getColumn(string $name): ColumnBuilderInterface throw new InvalidArgumentException(sprintf('The column with the name "%s" does not exist.', $name)); } + public function createColumn(string $name, ?string $type = null, array $options = []): ColumnBuilderInterface + { + if ($this->locked) { + throw $this->createBuilderLockedException(); + } + + return $this->getColumnFactory()->createNamedBuilder($name, $type ?? TextColumnType::class, $options); + } + public function addColumn(ColumnBuilderInterface|string $column, ?string $type = null, array $options = []): static { if ($this->locked) { @@ -263,6 +272,15 @@ public function getFilter(string $name): FilterBuilderInterface throw new InvalidArgumentException(sprintf('The filter with the name "%s" does not exist.', $name)); } + public function createFilter(string $name, ?string $type = null, array $options = []): FilterBuilderInterface + { + if ($this->locked) { + throw $this->createBuilderLockedException(); + } + + return $this->getFilterFactory()->createNamedBuilder($name, $type ?? FilterType::class, $options); + } + public function addFilter(string|FilterBuilderInterface $filter, ?string $type = null, array $options = []): static { if ($this->locked) { @@ -371,6 +389,15 @@ public function getAction(string $name): ActionBuilderInterface throw new InvalidArgumentException(sprintf('The action with the name "%s" does not exist.', $name)); } + public function createAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface + { + if ($this->locked) { + throw $this->createBuilderLockedException(); + } + + return $this->getActionFactory()->createNamedBuilder($name, $type ?? ButtonActionType::class, $options); + } + public function addAction(string|ActionBuilderInterface $action, ?string $type = null, array $options = []): static { if ($this->locked) { @@ -448,6 +475,18 @@ public function hasBatchAction(string $name): bool return isset($this->batchActions[$name]) || isset($this->unresolvedBatchActions[$name]); } + public function createBatchAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface + { + if ($this->locked) { + throw $this->createBuilderLockedException(); + } + + return $this->getActionFactory() + ->createNamedBuilder($name, $type ?? ButtonActionType::class, $options) + ->setContext(ActionContext::Batch) + ; + } + public function addBatchAction(string|ActionBuilderInterface $action, ?string $type = null, array $options = []): static { if ($this->locked) { @@ -536,6 +575,18 @@ public function hasRowAction(string $name): bool return isset($this->rowActions[$name]) || isset($this->unresolvedRowActions[$name]); } + public function createRowAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface + { + if ($this->locked) { + throw $this->createBuilderLockedException(); + } + + return $this->getActionFactory() + ->createNamedBuilder($name, $type ?? ButtonActionType::class, $options) + ->setContext(ActionContext::Row) + ; + } + public function addRowAction(string|ActionBuilderInterface $action, ?string $type = null, array $options = []): static { if ($this->locked) { @@ -615,6 +666,15 @@ public function getExporter(string $name): ExporterBuilderInterface throw new InvalidArgumentException(sprintf('The exporter with the name "%s" does not exist.', $name)); } + public function createExporter(string $name, ?string $type = null, array $options = []): ExporterBuilderInterface + { + if ($this->locked) { + throw $this->createBuilderLockedException(); + } + + return $this->getExporterFactory()->createNamedBuilder($name, $type ?? ExporterType::class, $options); + } + public function addExporter(string|ExporterBuilderInterface $exporter, ?string $type = null, array $options = []): static { if ($this->locked) { diff --git a/src/DataTableBuilderInterface.php b/src/DataTableBuilderInterface.php index a4f6a6f3..65769139 100755 --- a/src/DataTableBuilderInterface.php +++ b/src/DataTableBuilderInterface.php @@ -39,6 +39,11 @@ public function getColumn(string $name): ColumnBuilderInterface; public function hasColumn(string $name): bool; + /** + * @param class-string|null $type + */ + public function createColumn(string $name, ?string $type = null, array $options = []): ColumnBuilderInterface; + /** * @param class-string|null $type */ @@ -58,6 +63,11 @@ public function getFilter(string $name): FilterBuilderInterface; public function hasFilter(string $name): bool; + /** + * @param class-string|null $type + */ + public function createFilter(string $name, ?string $type = null, array $options = []): FilterBuilderInterface; + /** * @param class-string|null $type */ @@ -85,6 +95,11 @@ public function getAction(string $name): ActionBuilderInterface; public function hasAction(string $name): bool; + /** + * @param class-string|null $type + */ + public function createAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface; + /** * @param class-string|null $type */ @@ -104,6 +119,11 @@ public function getBatchAction(string $name): ActionBuilderInterface; public function hasBatchAction(string $name): bool; + /** + * @param class-string|null $type + */ + public function createBatchAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface; + /** * @param class-string|null $type */ @@ -127,6 +147,11 @@ public function getRowAction(string $name): ActionBuilderInterface; public function hasRowAction(string $name): bool; + /** + * @param class-string|null $type + */ + public function createRowAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface; + /** * @param class-string|null $type */ @@ -150,6 +175,11 @@ public function getExporter(string $name): ExporterBuilderInterface; public function hasExporter(string $name): bool; + /** + * @param class-string|null $type + */ + public function createExporter(string $name, ?string $type = null, array $options = []): ExporterBuilderInterface; + /** * @param class-string|null $type */ diff --git a/tests/Unit/DataTableBuilderTest.php b/tests/Unit/DataTableBuilderTest.php index 57807363..f18ea2ba 100644 --- a/tests/Unit/DataTableBuilderTest.php +++ b/tests/Unit/DataTableBuilderTest.php @@ -5,6 +5,7 @@ namespace Kreyu\Bundle\DataTableBundle\Tests\Unit; use Kreyu\Bundle\DataTableBundle\Action\ActionBuilderInterface; +use Kreyu\Bundle\DataTableBundle\Action\ActionContext; use Kreyu\Bundle\DataTableBundle\Action\ActionFactory; use Kreyu\Bundle\DataTableBundle\Action\ActionRegistry; use Kreyu\Bundle\DataTableBundle\Action\Type\ActionType; @@ -107,6 +108,18 @@ public function testGetColumn() $this->assertInstanceOf(TextColumnType::class, $column->getColumnConfig()->getType()->getInnerType()); } + public function testCreateColumn() + { + $builder = $this->createBuilder(); + $builder->setColumnFactory($this->createColumnFactory()); + + $column = $builder->createColumn('foo'); + + $this->assertInstanceOf(ColumnBuilderInterface::class, $column); + $this->assertSame('foo', $column->getName()); + $this->assertSame(TextColumnType::class, $column->getType()->getInnerType()::class); + } + public function testAddColumnWithColumnBuilder() { $columnFactory = $this->createColumnFactory(); @@ -183,6 +196,18 @@ public function testGetFilter() $this->assertInstanceOf(FilterType::class, $filter->getFilterConfig()->getType()->getInnerType()); } + public function testCreateFilter() + { + $builder = $this->createBuilder(); + $builder->setFilterFactory($this->createFilterFactory()); + + $filter = $builder->createFilter('foo'); + + $this->assertInstanceOf(FilterBuilderInterface::class, $filter); + $this->assertSame('foo', $filter->getName()); + $this->assertSame(FilterType::class, $filter->getType()->getInnerType()::class); + } + public function testAddFilterWithFilterBuilder() { $filterFactory = $this->createFilterFactory(); @@ -306,6 +331,19 @@ public function testGetAction() $this->assertInstanceOf(ButtonActionType::class, $action->getActionConfig()->getType()->getInnerType()); } + public function testCreateAction() + { + $builder = $this->createBuilder(); + $builder->setActionFactory($this->createActionFactory()); + + $action = $builder->createAction('foo'); + + $this->assertInstanceOf(ActionBuilderInterface::class, $action); + $this->assertSame('foo', $action->getName()); + $this->assertSame(ButtonActionType::class, $action->getType()->getInnerType()::class); + $this->assertSame(ActionContext::Global, $action->getContext()); + } + public function testAddActionWithActionBuilder() { $actionFactory = $this->createActionFactory(); @@ -354,6 +392,19 @@ public function testGetBatchAction() $this->assertInstanceOf(ButtonActionType::class, $action->getActionConfig()->getType()->getInnerType()); } + public function testCreateBatchAction() + { + $builder = $this->createBuilder(); + $builder->setActionFactory($this->createActionFactory()); + + $action = $builder->createBatchAction('foo'); + + $this->assertInstanceOf(ActionBuilderInterface::class, $action); + $this->assertSame('foo', $action->getName()); + $this->assertSame(ButtonActionType::class, $action->getType()->getInnerType()::class); + $this->assertSame(ActionContext::Batch, $action->getContext()); + } + public function testAddBatchActionWithBatchActionBuilder() { $actionFactory = $this->createActionFactory(); @@ -441,6 +492,19 @@ public function testGetRowAction() $this->assertInstanceOf(ButtonActionType::class, $action->getActionConfig()->getType()->getInnerType()); } + public function testCreateRowAction() + { + $builder = $this->createBuilder(); + $builder->setActionFactory($this->createActionFactory()); + + $action = $builder->createRowAction('foo'); + + $this->assertInstanceOf(ActionBuilderInterface::class, $action); + $this->assertSame('foo', $action->getName()); + $this->assertSame(ButtonActionType::class, $action->getType()->getInnerType()::class); + $this->assertSame(ActionContext::Row, $action->getContext()); + } + public function testAddRowActionWithRowActionBuilder() { $actionFactory = $this->createActionFactory(); @@ -558,6 +622,18 @@ public function testGetExporter() $this->assertInstanceOf(ExporterType::class, $action->getExporterConfig()->getType()->getInnerType()); } + public function testCreateExporterAction() + { + $builder = $this->createBuilder(); + $builder->setExporterFactory($this->createExporterFactory()); + + $exporter = $builder->createExporter('foo'); + + $this->assertInstanceOf(ExporterBuilderInterface::class, $exporter); + $this->assertSame('foo', $exporter->getName()); + $this->assertSame(ExporterType::class, $exporter->getType()->getInnerType()::class); + } + public function testAddExporterWithExporterBuilder() { $exporterFactory = $this->createExporterFactory(); From 78d829fd3451d018a64804e0817242ce7479c17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Fri, 28 Jun 2024 10:44:01 +0200 Subject: [PATCH 2/2] Fix php-cs-fixer errors --- src/Column/Type/EnumColumnType.php | 5 +++-- src/Resources/config/columns.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Column/Type/EnumColumnType.php b/src/Column/Type/EnumColumnType.php index 55f29186..5ae1d8cc 100644 --- a/src/Column/Type/EnumColumnType.php +++ b/src/Column/Type/EnumColumnType.php @@ -10,8 +10,9 @@ final class EnumColumnType extends AbstractColumnType { - public function __construct(private ?TranslatorInterface $translator) - { + public function __construct( + private ?TranslatorInterface $translator, + ) { } public function configureOptions(OptionsResolver $resolver): void diff --git a/src/Resources/config/columns.php b/src/Resources/config/columns.php index 479be87e..1fe0dd4d 100755 --- a/src/Resources/config/columns.php +++ b/src/Resources/config/columns.php @@ -16,10 +16,10 @@ use Kreyu\Bundle\DataTableBundle\Column\Type\DateColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\DatePeriodColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\DateTimeColumnType; +use Kreyu\Bundle\DataTableBundle\Column\Type\EnumColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\FormColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\LinkColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\MoneyColumnType; -use Kreyu\Bundle\DataTableBundle\Column\Type\EnumColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\NumberColumnType; use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeFactory; use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeFactoryInterface;