Skip to content

Commit

Permalink
Add default timezone in DatetimeFieldType based on timezone param
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Apr 8, 2024
1 parent e688acb commit 2d76e8e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services/field_types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<service id="Sylius\Component\Grid\FieldTypes\DatetimeFieldType">
<argument type="service" id="sylius.grid.data_extractor" />
<argument on-invalid="null">%timezone%</argument>
<tag name="sylius.grid_field" type="datetime" />
</service>
<service id="sylius.grid_field.datetime" alias="Sylius\Component\Grid\FieldTypes\DatetimeFieldType" />
Expand Down
8 changes: 5 additions & 3 deletions src/Component/FieldTypes/DatetimeFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ final class DatetimeFieldType implements FieldTypeInterface
{
private DataExtractorInterface $dataExtractor;

public function __construct(DataExtractorInterface $dataExtractor)
{
public function __construct(
DataExtractorInterface $dataExtractor,
private ?string $timezone = null,
) {
$this->dataExtractor = $dataExtractor;
}

Expand Down Expand Up @@ -51,7 +53,7 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('format', 'Y-m-d H:i:s');
$resolver->setAllowedTypes('format', 'string');
$resolver->setDefault('timezone', null);
$resolver->setDefault('timezone', $this->timezone);
$resolver->setAllowedTypes('timezone', ['null', 'string']);
}
}
15 changes: 15 additions & 0 deletions src/Component/spec/FieldTypes/DatetimeFieldTypeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Component\Grid\DataExtractor\DataExtractorInterface;
use Sylius\Component\Grid\Definition\Field;
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class DatetimeFieldTypeSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -73,6 +74,20 @@ function it_returns_null_if_property_accessor_returns_null(DataExtractorInterfac
])->shouldReturn('');
}

function it_uses_timezone_parameter_as_default_timezone_option(
DataExtractorInterface $dataExtractor,
OptionsResolver $resolver,
): void {
$this->beConstructedWith($dataExtractor, 'Europe/Warsaw');

$resolver->setDefault('format', 'Y-m-d H:i:s')->willReturn($resolver)->shouldBeCalled();
$resolver->setAllowedTypes('format', 'string')->willReturn($resolver)->shouldBeCalled();
$resolver->setDefault('timezone', 'Europe/Warsaw')->willReturn($resolver)->shouldBeCalled();
$resolver->setAllowedTypes('timezone', ['null', 'string'])->willReturn($resolver)->shouldBeCalled();

$this->configureOptions($resolver);
}

function it_throws_exception_if_returned_value_is_not_datetime(DataExtractorInterface $dataExtractor, Field $field): void
{
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('badObject');
Expand Down

0 comments on commit 2d76e8e

Please sign in to comment.