Completed
Pull Request — master (#83)
by Daniel
08:26
created

DateTimeColumn::buildCell()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Column;
6
7
use Psi\Component\Grid\View\Cell;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class DateTimeColumn extends AbstractColumn
11
{
12
    public function buildCell(Cell $cell, array $options)
13
    {
14
        $value = $cell->value;
15
16
        if (null === $value) {
17
            return;
18
        }
19
20
        if (false === $value instanceof \DateTime) {
21
            if (strtotime($value)) {
22
                $cell->value = new \DateTime($value);
23
            } else {
24
                throw new \InvalidArgumentException(sprintf(
25
                    'The DateTime column requires a \DateTime value at "%s", but got "%s"',
26
                    $options['property'], is_object($value) ? get_class($value) : gettype($value)
27
                ));
28
            }
29
        }
30
31
        $cell->parameters['format'] = $options['format'];
32
    }
33
34
    public function configureOptions(OptionsResolver $resolver)
35
    {
36
        $resolver->setDefault('format', 'c');
37
    }
38
39
    public function getParent()
40
    {
41
        return PropertyColumn::class;
42
    }
43
}
44