Completed
Pull Request — master (#49)
by Daniel
08:41
created

DateTimeColumn   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildCell() 0 18 4
A configureOptions() 0 4 1
A getParent() 0 4 1
1
<?php
2
3
namespace Psi\Component\Grid\Column;
4
5
use Psi\Component\Grid\ColumnInterface;
6
use Psi\Component\Grid\View\Cell;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class DateTimeColumn implements ColumnInterface
10
{
11
    public function buildCell(Cell $cell, array $options)
12
    {
13
        $value = $cell->value;
14
15
        if (null === $value) {
16
            return;
17
        }
18
19
        if (false === $value instanceof \DateTime) {
20
            throw new \InvalidArgumentException(sprintf(
21
                'The DateTime column requires a \DateTime value at "%s", but got "%s"',
22
                $options['property'], is_object($value) ? get_class($value) : gettype($value)
23
            ));
24
        }
25
26
        $cell->template = 'DateTime';
27
        $cell->parameters['format'] = $options['format'];
28
    }
29
30
    public function configureOptions(OptionsResolver $resolver)
31
    {
32
        $resolver->setDefault('format', 'c');
33
    }
34
35
    public function getParent()
36
    {
37
        return PropertyColumn::class;
38
    }
39
}
40