Skip to content

Add create methods to data table builder #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024
Merged
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
5 changes: 3 additions & 2 deletions src/Column/Type/EnumColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions src/DataTableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions src/DataTableBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function getColumn(string $name): ColumnBuilderInterface;

public function hasColumn(string $name): bool;

/**
* @param class-string<ColumnTypeInterface>|null $type
*/
public function createColumn(string $name, ?string $type = null, array $options = []): ColumnBuilderInterface;

/**
* @param class-string<ColumnTypeInterface>|null $type
*/
Expand All @@ -58,6 +63,11 @@ public function getFilter(string $name): FilterBuilderInterface;

public function hasFilter(string $name): bool;

/**
* @param class-string<FilterTypeInterface>|null $type
*/
public function createFilter(string $name, ?string $type = null, array $options = []): FilterBuilderInterface;

/**
* @param class-string<FilterTypeInterface>|null $type
*/
Expand Down Expand Up @@ -85,6 +95,11 @@ public function getAction(string $name): ActionBuilderInterface;

public function hasAction(string $name): bool;

/**
* @param class-string<ActionTypeInterface>|null $type
*/
public function createAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface;

/**
* @param class-string<ActionTypeInterface>|null $type
*/
Expand All @@ -104,6 +119,11 @@ public function getBatchAction(string $name): ActionBuilderInterface;

public function hasBatchAction(string $name): bool;

/**
* @param class-string<ActionTypeInterface>|null $type
*/
public function createBatchAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface;

/**
* @param class-string<ActionTypeInterface>|null $type
*/
Expand All @@ -127,6 +147,11 @@ public function getRowAction(string $name): ActionBuilderInterface;

public function hasRowAction(string $name): bool;

/**
* @param class-string<ActionTypeInterface>|null $type
*/
public function createRowAction(string $name, ?string $type = null, array $options = []): ActionBuilderInterface;

/**
* @param class-string<ActionTypeInterface>|null $type
*/
Expand All @@ -150,6 +175,11 @@ public function getExporter(string $name): ExporterBuilderInterface;

public function hasExporter(string $name): bool;

/**
* @param class-string<ExporterTypeInterface>|null $type
*/
public function createExporter(string $name, ?string $type = null, array $options = []): ExporterBuilderInterface;

/**
* @param class-string<ExporterTypeInterface>|null $type
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions tests/Unit/DataTableBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading