Completed
Pull Request — dev (#9)
by Arnaud
02:46
created

Date::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\Field;
6
use DateTime;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Date field.
11
 */
12
class Date extends Field
13
{
14
    /**
15
     * Date format.
16
     *
17
     * @var string
18
     */
19
    protected $format;
20
21
    /**
22
     * Render and format a Datetime.
23
     *
24
     * @param $value
25
     *
26
     * @return string
27
     */
28 1
    public function render($value)
29
    {
30 1
        if ($value instanceof DateTime) {
31 1
            $value = $value->format($this->format);
32 1
        }
33
34 1
        return $value;
35
    }
36
37
    /**
38
     * @param OptionsResolver $resolver
39
     */
40 1
    public function configureOptions(OptionsResolver $resolver)
41
    {
42 1
        $resolver->setDefaults([
43 1
            'format' => $this
44
                ->applicationConfiguration
45 1
                ->getParameter('date_format'),
46 1
        ]);
47 1
    }
48
49
    /**
50
     * @param array $options
51
     */
52 1
    public function setOptions(array $options)
53
    {
54 1
        $this->format = $options['format'];
55 1
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getType()
61
    {
62
        return 'date';
63
    }
64
}
65