Completed
Push — symfony3-wololo ( 00fd93...5670c9 )
by Kamil
18:49
created

DatetimeFieldType::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
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 Symfony\Component\OptionsResolver\OptionsResolver;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Mateusz Zalewski <[email protected]>
21
 */
22
final 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->setDefault('format', 'Y:m:d H:i:s');
58
        $resolver->setAllowedTypes('format', 'string');
59
    }
60
}
61