Completed
Push — symfony3-fqcn ( cea3b6...24b82c )
by Kamil
103:48 queued 86:45
created

DatetimeFieldType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 59
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A render() 0 11 2
A configureOptions() 0 9 1
A getBlockPrefix() 0 4 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 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->setDefaults([
58
            'format' => 'Y:m:d H:i:s'
59
        ]);
60
        $resolver->setAllowedTypes([
0 ignored issues
show
Documentation introduced by
array('format' => 'string') is of type array<string,string,{"format":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
            'format' => 'string'
62
        ]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getName()
69
    {
70
        return 'datetime';
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getBlockPrefix()
77
    {
78
        return 'datetime';
79
    }
80
}
81