Completed
Pull Request — master (#90)
by Arnaud
02:15
created

StringField::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use LAG\AdminBundle\Field\TranslatorAwareInterface;
7
use LAG\AdminBundle\Field\TwigAwareInterface;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\Translation\TranslatorInterface;
10
use Twig_Environment;
11
12
/**
13
 * String field.
14
 *
15
 * It render a string that can be truncated according to is length
16
 * Note : according php7 (nightly), class can not be named String anymore
17
 */
18
class StringField extends AbstractField implements TranslatorAwareInterface, TwigAwareInterface
19
{
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    protected $translator;
24
    
25
    /**
26
     * @var Twig_Environment
27
     */
28
    protected $twig;
29
30
    /**
31
     * Render a string that can be truncated according to is length.
32
     *
33
     * @param $value
34
     * @return string
35
     */
36 1
    public function render($value)
37
    {
38 1
        if ($this->options->get('translation')) {
39
            $value = $this
40 1
                ->translator
41 1
                ->trans($value);
42
        }
43
        $maximumStringLength = $this
44 1
            ->options
45 1
            ->get('length');
46
        $replaceString = $this
47 1
            ->options
48 1
            ->get('replace');
49
50
        // truncate string if required
51 1
        if ($maximumStringLength && strlen($value) > $maximumStringLength) {
52
            $value = substr($value, 0, $maximumStringLength).$replaceString;
53
        }
54
        // #69 : strip tags to avoid layout destruction when content contains html
55 1
        $value = strip_tags($value);
56
57 1
        return $value;
58
    }
59
60
    /**
61
     * Configure options resolver.
62
     *
63
     * @param OptionsResolver $resolver
64
     */
65 1
    public function configureOptions(OptionsResolver $resolver)
66
    {
67 1
        parent::configureOptions($resolver);
68
        
69 1
        $resolver->setDefaults([
70 1
            'length' => $this->applicationConfiguration->getParameter('string_length'),
71 1
            'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'),
72
            'translation' => true,
73
        ]);
74 1
    }
75
76
    /**
77
     * Return form type.
78
     *
79
     * @return string
80
     */
81
    public function getType()
82
    {
83
        return 'string';
84
    }
85
    
86
    /**
87
     * Defines translator.
88
     *
89
     * @param TranslatorInterface $translator
90
     *
91
     * @return void
92
     */
93 1
    public function setTranslator(TranslatorInterface $translator)
94
    {
95 1
        $this->translator = $translator;
96 1
    }
97
    
98
    /**
99
     * Define twig environment.
100
     *
101
     * @param Twig_Environment $twig
102
     *
103
     * @return void
104
     */
105 1
    public function setTwig(Twig_Environment $twig)
106
    {
107 1
        $this->twig = $twig;
108 1
    }
109
}
110