DateType::render()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 4
1
<?php
2
/*
3
 * This file is part of cwdFancyGridBundle
4
 *
5
 * (c)2017 cwd.at GmbH <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
namespace Cwd\FancyGridBundle\Column;
12
13
use Cwd\FancyGridBundle\Grid\Exception\InvalidArgumentException;
14
use Symfony\Component\OptionsResolver\OptionsResolver;
15
16
/**
17
 * Class NumberType
18
 * @package Cwd\FancyGridBundle\Column
19
 * @author Ludwig Ruderstaler <[email protected]>
20
 */
21
class DateType extends AbstractColumn
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function configureOptions(OptionsResolver $resolver)
27
    {
28
        parent::configureOptions($resolver);
29
30
        $resolver->setDefaults(array(
31
            'align' => 'right',
32
            'cellAlign' => 'right',
33
            'format' => [
34
                'read' => 'Y-m-d H:i:s',
35
                'write' => 'd.m.Y H:i:s',
36
                'edit' => 'Y-m-d'
37
            ],
38
            'width' => 150,
39
            'type' => 'date'
40
        ));
41
42
        $resolver->setAllowedTypes('attr', 'array');
43
    }
44
45
    /**
46
     * @param mixed             $value
47
     * @param mixed             $object
48
     * @param mixed             $primary
49
     * @param \Twig_Environment $twig
50
     *
51
     * @return string
52
     */
53
    public function render($value, $object, $primary, \Twig_Environment $twig)
54
    {
55
        if ($value === null) {
56
            return null;
57
        }
58
59
        if (!$value instanceof \DateTime) {
60
            throw new InvalidArgumentException('%s is not of expected \DateTime', $this->getName());
61
        }
62
63
        return $value->format($this->getOption('format')['read']);
64
    }
65
}
66