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

TwigFieldType::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\Bundle\GridBundle\FieldTypes;
13
14
use Sylius\Component\Grid\DataExtractor\DataExtractorInterface;
15
use Sylius\Component\Grid\Definition\Field;
16
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
class TwigFieldType implements FieldTypeInterface
23
{
24
    /**
25
     * @var DataExtractorInterface
26
     */
27
    private $dataExtractor;
28
29
    /**
30
     * @var \Twig_Environment
31
     */
32
    private $twig;
33
34
    /**
35
     * @param DataExtractorInterface $dataExtractor
36
     * @param \Twig_Environment $twig
37
     */
38
    public function __construct(DataExtractorInterface $dataExtractor, \Twig_Environment $twig)
39
    {
40
        $this->dataExtractor = $dataExtractor;
41
        $this->twig = $twig;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function render(Field $field, $data, array $options)
48
    {
49
        if ('.' !== $field->getPath()) {
50
            $data = $this->dataExtractor->get($field, $data);
51
        }
52
53
        return $this->twig->render($options['template'], ['data' => $data]);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function configureOptions(OptionsResolver $resolver)
60
    {
61
        $resolver->setRequired([
62
            'template'
63
        ]);
64
        $resolver->setAllowedTypes([
65
            'template' => ['string'],
66
        ]);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getName()
73
    {
74
        return 'twig';
75
    }
76
}
77