Skip to content
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

Remove inheritance of built-in column types, improve readability of column type classes, fix getters that returns columns from data tables #192

Merged
merged 13 commits into from
Feb 23, 2025
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
1 change: 0 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ jobs:

- name: Run PHPUnit
run: vendor/bin/phpunit

1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
;

return (new PhpCsFixer\Config())
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->registerCustomFixers(new PhpCsFixerCustomFixers\Fixers())
->setRules([
'@Symfony' => true,
Expand Down
11 changes: 0 additions & 11 deletions docs/src/docs/composer-require-version.data.js

This file was deleted.

10 changes: 2 additions & 8 deletions docs/src/docs/installation.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<script setup>
import { data as version } from './composer-require-version.data';
</script>

# Installation

This bundle can be installed at any moment during a project’s lifecycle.
Expand All @@ -18,12 +14,10 @@ This bundle can be installed at any moment during a project’s lifecycle.
Use [Composer](https://getcomposer.org/) to install the bundle:

```shell-vue
composer require kreyu/data-table-bundle:"{{ version }}"
composer require kreyu/data-table-bundle
```

::: danger This bundle is not production ready!
It is recommended to lock the minor version, as minor versions can provide breaking changes until the stable release!
:::
> [!DANGER] This bundle is not stable yet. Use with caution.

## Enable the bundle

Expand Down
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;
}
34 changes: 34 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 @@ -222,6 +224,38 @@ public function setPersonalizable(bool $personalizable): static
return $this;
}

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

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

$this->priority = $priority;

return $this;
}

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

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

$this->visible = $visible;

return $this;
}

public function getColumnFactory(): ColumnFactoryInterface
{
if (!isset($this->columnFactory)) {
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;
}
2 changes: 1 addition & 1 deletion src/Column/ColumnSortUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function generate(DataTableView $dataTableView, ColumnHeaderView ...$colu
}

// Clearing the filters should reset the pagination to the first page.
if ($dataTableView->vars['pagination_enabled']) {
if ($dataTableView->vars['pagination_enabled'] ?? false) {
$parameters[$dataTableView->vars['page_parameter_name']] = 1;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Column/ColumnValueView.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function getDataTable(): DataTableView
{
return $this->parent->parent;
}

public function getRowData(): mixed
{
return $this->parent->data;
}
}
72 changes: 72 additions & 0 deletions src/Column/Type/AbstractDateTimeColumnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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.')
;

// When exporting, we ensure the export "formatter" option is present, so the value gets pre-formatted.
$resolver->addNormalizer('export', function (Options $options, mixed $export) {
if (false === $export) {
return false;
}

if (true === $export) {
$export = [];
}

$export['formatter'] ??= $options['formatter'] ?? function (mixed $value) use ($options) {
if (!$value instanceof \DateTimeInterface) {
return '';
}

$timezone = $options['timezone'];

if (null === $timezone) {
$timezone = date_default_timezone_get();
}

if (is_string($timezone)) {
$timezone = new \DateTimeZone($timezone);
}

$dateTime = \DateTime::createFromInterface($value);

if ($timezone instanceof \DateTimeZone) {
$dateTime->setTimezone($timezone);
}

return $dateTime->format($options['format']);
};

return $export;
});
}
}
Loading