Completed
Push — master ( 638d86...a7c2d2 )
by Kamil
22:18
created

DatetimeFieldType::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Grid\FieldTypes;
13
14
use Sylius\Component\Grid\DataExtractor\DataExtractorInterface;
15
use Sylius\Component\Grid\Definition\Field;
16
use Webmozart\Assert\Assert;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Mateusz Zalewski <[email protected]>
21
 */
22
class DatetimeFieldType implements FieldTypeInterface
23
{
24
    /**
25
     * @var DataExtractorInterface
26
     */
27
    private $dataExtractor;
28
29
    /**
30
     * @param DataExtractorInterface $dataExtractor
31
     */
32
    public function __construct(DataExtractorInterface $dataExtractor)
33
    {
34
        $this->dataExtractor = $dataExtractor;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function render(Field $field, $data, array $options)
41
    {
42
        $value = $this->dataExtractor->get($field, $data);
43
        if (null === $value) {
44
            return null;
45
        }
46
47
        Assert::isInstanceOf($value, \DateTime::class);
48
49
        return $value->format($options['format']);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function configureOptions(OptionsResolver $resolver)
56
    {
57
        $resolver->setDefaults([
58
            'format' => 'Y:m:d H:i:s'
59
        ]);
60
        $resolver->setAllowedTypes([
61
            'format' => 'string'
62
        ]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getName()
69
    {
70
        return 'datetime';
71
    }
72
}
73