Skip to content

Commit 38449a6

Browse files
committed
Add BoolColumn support
1 parent 3555b6c commit 38449a6

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

CHANGELOG.md

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

55
## [Unreleased]
6-
Nothing yet.
6+
### Added
7+
- Add BoolColumn for handling strict boolean columns
8+
9+
### Changed
10+
- Column values default to 'data' only on NULL instead of any 'emptiness'
711

812
## [0.1.2] - 2017-12-14
913
### Added

src/Column/AbstractColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function transform($value = null, $context = null)
7070
$data = $this->options['data'];
7171
if (is_callable($data)) {
7272
$value = call_user_func($data, $context, $value);
73-
} elseif (empty($value)) {
73+
} elseif (null === $value) {
7474
$value = $data;
7575
}
7676

src/Column/BoolColumn.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* Symfony DataTables Bundle
5+
* (c) Omines Internetbureau B.V. - https://omines.nl/
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace Omines\DataTablesBundle\Column;
14+
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
/**
18+
* BoolColumn.
19+
*
20+
* @author Niels Keurentjes <[email protected]>
21+
*/
22+
class BoolColumn extends AbstractColumn
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function normalize($value): string
28+
{
29+
if (null === $value) {
30+
return $this->getNullValue();
31+
}
32+
33+
return ((bool) $value) ? $this->getTrueValue() : $this->getFalseValue();
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected function configureOptions(OptionsResolver $resolver)
40+
{
41+
parent::configureOptions($resolver);
42+
43+
$resolver
44+
->setDefault('trueValue', 'true')
45+
->setDefault('falseValue', 'false')
46+
->setDefault('nullValue', 'null')
47+
->setAllowedTypes('trueValue', 'string')
48+
->setAllowedTypes('falseValue', 'string')
49+
->setAllowedTypes('nullValue', 'string')
50+
;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getTrueValue(): string
59+
{
60+
return $this->options['trueValue'];
61+
}
62+
63+
/**
64+
* @return string
65+
*/
66+
public function getFalseValue(): string
67+
{
68+
return $this->options['falseValue'];
69+
}
70+
71+
/**
72+
* @return string
73+
*/
74+
public function getNullValue(): string
75+
{
76+
return $this->options['nullValue'];
77+
}
78+
}

tests/Unit/ColumnTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Tests\Unit;
1414

15+
use Omines\DataTablesBundle\Column\BoolColumn;
1516
use Omines\DataTablesBundle\Column\TextColumn;
1617
use Omines\DataTablesBundle\DataTable;
1718
use PHPUnit\Framework\TestCase;
@@ -37,6 +38,19 @@ public function testTextColumn()
3738
$this->assertSame('foo', $column->getDataTable()->getName());
3839
}
3940

41+
public function testBoolColumn()
42+
{
43+
$column = new BoolColumn('test', 1, [
44+
'trueValue' => 'yes',
45+
'nullValue' => '<em>null</em>',
46+
]);
47+
48+
$this->assertSame('yes', $column->transform(5));
49+
$this->assertSame('yes', $column->transform(true));
50+
$this->assertSame('false', $column->transform(false));
51+
$this->assertStringStartsWith('<em>', $column->transform());
52+
}
53+
4054
public function testColumnWithClosures()
4155
{
4256
$column = new TextColumn('test', 1, [

0 commit comments

Comments
 (0)