Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Feb 19, 2025
1 parent 101fb07 commit cfa0547
Show file tree
Hide file tree
Showing 26 changed files with 322 additions and 281 deletions.
26 changes: 0 additions & 26 deletions src/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Column implements ColumnInterface
private ?DataTableInterface $dataTable = null;
private ?PropertyPathInterface $propertyPath = null;
private ?PropertyPathInterface $sortPropertyPath = null;
private int $priority = 0;
private bool $visible = true;

public function __construct(
private readonly ColumnConfigInterface $config,
Expand Down Expand Up @@ -107,28 +105,4 @@ public function createExportValueView(?ValueRowView $parent = null): ColumnValue

return $view;
}

public function getPriority(): int
{
return $this->priority;
}

public function setPriority(int $priority): static
{
$this->priority = $priority;

return $this;
}

public function isVisible(): bool
{
return $this->visible;
}

public function setVisible(bool $visible): static
{
$this->visible = $visible;

return $this;
}
}
48 changes: 1 addition & 47 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,13 @@

class ColumnBuilder extends ColumnConfigBuilder implements ColumnBuilderInterface
{
private int $priority = 0;
private bool $visible = true;

public function getPriority(): int
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

return $this->priority;
}

public function setPriority(int $priority): static
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

$this->priority = $priority;

return $this;
}

public function isVisible(): bool
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

return $this->visible;
}

public function setVisible(bool $visible): static
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

$this->visible = $visible;

return $this;
}

public function getColumn(): ColumnInterface
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

return (new Column($this->getColumnConfig()))
->setPriority($this->getPriority())
->setVisible($this->isVisible())
;
return new Column($this->getColumnConfig());
}

private function createBuilderLockedException(): BadMethodCallException
Expand Down
8 changes: 0 additions & 8 deletions src/Column/ColumnBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,5 @@

interface ColumnBuilderInterface extends ColumnConfigBuilderInterface
{
public function getPriority(): int;

public function setPriority(int $priority): static;

public function isVisible(): bool;

public function setVisible(bool $visible): static;

public function getColumn(): ColumnInterface;
}
41 changes: 41 additions & 0 deletions src/Column/ColumnConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ColumnConfigBuilder implements ColumnConfigBuilderInterface
private bool $sortable = false;
private bool $exportable = false;
private bool $personalizable = true;
private int $priority = 0;
private bool $visible = true;
private ColumnFactoryInterface $columnFactory;

public function __construct(
Expand Down Expand Up @@ -221,6 +223,45 @@ public function setPersonalizable(bool $personalizable): static

return $this;
}
public function getPriority(): int
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

return $this->priority;
}

public function setPriority(int $priority): static
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

$this->priority = $priority;

return $this;
}

public function isVisible(): bool
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

return $this->visible;
}

public function setVisible(bool $visible): static
{
if ($this->locked) {
throw $this->createBuilderLockedException();
}

$this->visible = $visible;

return $this;
}

public function getColumnFactory(): ColumnFactoryInterface
{
Expand Down
19 changes: 4 additions & 15 deletions src/Column/ColumnConfigBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,8 @@

interface ColumnConfigBuilderInterface extends ColumnConfigInterface
{
/**
* @deprecated since 0.14.0, provide the name using the factory {@see ColumnFactoryInterface} "named" methods instead
*/
public function setName(string $name): static;

public function setType(ResolvedColumnTypeInterface $type): static;

/**
* @deprecated since 0.14.0, modifying the options dynamically will be removed as it creates unexpected behaviors
*/
public function setOptions(array $options): static;

/**
* @deprecated since 0.14.0, modifying the options dynamically will be removed as it creates unexpected behaviors
*/
public function setOption(string $name, mixed $value): static;

public function setAttributes(array $attributes): static;

public function setAttribute(string $name, mixed $value): static;
Expand All @@ -40,6 +25,10 @@ public function setExportable(bool $exportable): static;

public function setPersonalizable(bool $personalizable): static;

public function setPriority(int $priority): static;

public function setVisible(bool $visible): static;

public function setColumnFactory(ColumnFactoryInterface $columnFactory): static;

public function getColumnConfig(): ColumnConfigInterface;
Expand Down
4 changes: 4 additions & 0 deletions src/Column/ColumnConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ public function isExportable(): bool;

public function isPersonalizable(): bool;

public function getPriority(): int;

public function isVisible(): bool;

public function getColumnFactory(): ColumnFactoryInterface;
}
8 changes: 0 additions & 8 deletions src/Column/ColumnInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,4 @@ public function createValueView(?ValueRowView $parent = null): ColumnValueView;
public function createExportHeaderView(?HeaderRowView $parent = null): ColumnHeaderView;

public function createExportValueView(?ValueRowView $parent = null): ColumnValueView;

public function getPriority(): int;

public function setPriority(int $priority): static;

public function isVisible(): bool;

public function setVisible(bool $visible): static;
}
55 changes: 55 additions & 0 deletions src/Column/Type/AbstractDateTimeColumnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

abstract class AbstractDateTimeColumnType extends AbstractColumnType
{
public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void
{
$view->vars = array_replace($view->vars, [
'format' => $options['format'],
'timezone' => $options['timezone'],
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->define('format')
->allowedTypes('string')
->info('A date time string format, supported by the PHP date() function - null to use default.')
;

$resolver->define('timezone')
->default(null)
->allowedTypes('null', 'bool', 'string', \DateTimeZone::class)
->info('Target timezone - null to use the default, false to leave unchanged.')
;

$resolver->setNormalizer('export', function (Options $options, $value) {
if (true === $value) {
$value = [];
}

if (is_array($value)) {
$value += [
'formatter' => static function (mixed $value, mixed $data, ColumnInterface $column): string {
if ($value instanceof \DateTimeInterface) {
return $value->format($column->getConfig()->getOption('format'));
}

return '';
},
];
}

return $value;
});
}
}
30 changes: 18 additions & 12 deletions src/Column/Type/ActionsColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,27 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a

foreach ($options['actions'] as $name => $action) {
$action = $this->resolveAction($name, $action, $view);
$action?->setDataTable($column->getDataTable());

$actions[$name] = $action?->createView($view);
if (null === $action) {
continue;
}

$action->setDataTable($column->getDataTable());

$actions[$name] = $action->createView($view);
}

$view->vars['actions'] = array_filter($actions);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'label' => 'Actions',
'export' => false,
'property_path' => false,
'actions' => [],
])
->setNormalizer('actions', function (Options $options, mixed $value) {
/* @see https://data-table-bundle.swroblewski.pl/reference/types/column/actions#actions */
$resolver->define('actions')
->default([])
->allowedTypes('actions', 'array[]', ActionBuilderInterface::class.'[]', ActionInterface::class.'[]')
->info('An array of actions to render in the column.')
->normalize(function (Options $options, mixed $value) {
($resolver = new OptionsResolver())
->setRequired([
'type',
Expand All @@ -68,9 +71,12 @@ public function configureOptions(OptionsResolver $resolver): void

return $value;
})
->setAllowedTypes('actions', ['array[]', ActionBuilderInterface::class.'[]', ActionInterface::class.'[]'])
->setInfo('actions', 'An array of actions configuration, which contains of their type and options.')
;

$resolver->setDefaults([
'export' => false,
'property_path' => false,
]);
}

private function resolveAction(
Expand Down
24 changes: 14 additions & 10 deletions src/Column/Type/BooleanColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'label_true' => 'Yes',
'label_false' => 'No',
'value_translation_domain' => 'KreyuDataTable',
])
->setAllowedTypes('label_true', ['string', TranslatableInterface::class])
->setAllowedTypes('label_false', ['string', TranslatableInterface::class])
->setInfo('label_true', 'Label displayed when the value equals true.')
->setInfo('label_false', 'Label displayed when the value equals false.')
/* @see https://data-table-bundle.swroblewski.pl/reference/types/column/boolean#label_true */
$resolver->define('label_true')
->default('Yes')
->allowedTypes('string', TranslatableInterface::class)
->info('Label displayed when the value is truthy.')
;

/* @see https://data-table-bundle.swroblewski.pl/reference/types/column/boolean#label_false */
$resolver->define('label_false')
->default('No')
->allowedTypes('string', TranslatableInterface::class)
->info('Label displayed when the value is falsy.')
;

$resolver->setDefault('value_translation_domain', 'KreyuDataTable');
}
}
8 changes: 7 additions & 1 deletion src/Column/Type/CheckboxColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a

public function configureOptions(OptionsResolver $resolver): void
{
/* @see https://data-table-bundle.swroblewski.pl/reference/types/column/checkbox#identifier_name */
$resolver->define('identifier_name')
->default('id')
->allowedTypes('string')
->info('The name of the identifier property.')
;

$resolver->setDefaults([
'label' => '',
'property_path' => 'id',
'identifier_name' => 'id',
]);
}
}
Loading

0 comments on commit cfa0547

Please sign in to comment.