Skip to content

Commit d83a99f

Browse files
committed
Fix DateTimeColumn behavior with default/null values
1 parent 43873b1 commit d83a99f

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
All notable changes to `omines\datatables-bundle` will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [Unreleased]
6-
Nothing yet.
5+
## [0.1.5] - 2018-01-25
6+
### Fixed
7+
- Fixed inconsistency in DateTimeColumn with default/null values
78

89
## [0.1.4] - 2018-01-21
910
### Added

docs/source/index.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ result. If data of other types is encountered automatic conversion is attempted
373373
Option | Type | Description
374374
------ | ---- | -----------
375375
format | string | A date format string as accepted by the [`date()`](http://php.net/manual/en/function.date.php) function. Default `'c'`.
376+
nullValue | string | Raw string to display for null values. Defaults to the empty string.
376377

377378
## TwigColumn
378379

src/Column/DateTimeColumn.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DateTimeColumn extends AbstractColumn
2727
public function normalize($value)
2828
{
2929
if (null === $value) {
30-
return $this->getData();
30+
return $this->options['nullValue'];
3131
} elseif (!$value instanceof \DateTimeInterface) {
3232
$value = new \DateTime($value);
3333
}
@@ -45,8 +45,10 @@ protected function configureOptions(OptionsResolver $resolver)
4545
$resolver
4646
->setDefaults([
4747
'format' => 'c',
48+
'nullValue' => '',
4849
])
4950
->setAllowedTypes('format', 'string')
51+
->setAllowedTypes('nullValue', 'string')
5052
;
5153

5254
return $this;

tests/Functional/FunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testPlainDataTable()
6262
$sample = $json->data[5];
6363
$this->assertSame('FirstName94', $sample->firstName);
6464
$this->assertSame('LastName94', $sample->lastName);
65-
$this->assertNull($sample->employedSince);
65+
$this->assertEmpty($sample->employedSince);
6666
$this->assertSame('FirstName94 <img src="https://symfony.com/images/v5/logos/sf-positive.svg"> LastName94', $sample->fullName);
6767
$this->assertRegExp('#href="/employee/[0-9]+"#', $sample->buttons);
6868

tests/Unit/ColumnTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Tests\Unit;
1414

1515
use Omines\DataTablesBundle\Column\BoolColumn;
16+
use Omines\DataTablesBundle\Column\DateTimeColumn;
1617
use Omines\DataTablesBundle\Column\TextColumn;
1718
use Omines\DataTablesBundle\Column\TwigColumn;
1819
use Omines\DataTablesBundle\DataTable;
@@ -25,6 +26,18 @@
2526
*/
2627
class ColumnTest extends TestCase
2728
{
29+
public function testDateTimeColumn()
30+
{
31+
$column = new DateTimeColumn();
32+
$column->initialize('test', 1, [
33+
'nullValue' => 'foo',
34+
'format' => 'd-m-Y',
35+
], (new DataTable())->setName('foo'));
36+
37+
$this->assertSame('03-04-2015', $column->transform('2015-04-03'));
38+
$this->assertSame('foo', $column->transform(null));
39+
}
40+
2841
public function testTextColumn()
2942
{
3043
$column = new TextColumn();

0 commit comments

Comments
 (0)