Completed
Pull Request — master (#90)
by Arnaud
04:05
created

StringField::render()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 11
cts 12
cp 0.9167
rs 8.7972
cc 4
eloc 15
nc 4
nop 1
crap 4.0092
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 2
    public function render($value)
37
    {
38 2
        if ($this->options->get('translation')) {
39
            $value = $this
40 2
                ->translator
41 2
                ->trans($value);
42
        }
43
        $maximumStringLength = $this
44 2
            ->options
45 2
            ->get('length');
46
        $replaceString = $this
47 2
            ->options
48 2
            ->get('replace');
49
50
        // truncate string if required
51 2
        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 2
        $value = strip_tags($value);
56
57 2
        return $value;
58
    }
59
60
    /**
61
     * Configure options resolver.
62
     *
63
     * @param OptionsResolver $resolver
64
     */
65 2
    public function configureOptions(OptionsResolver $resolver)
66
    {
67 2
        $resolver->setDefaults([
68 2
            'length' => $this->applicationConfiguration->getParameter('string_length'),
69 2
            'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'),
70
            'translation' => true,
71
        ]);
72 2
    }
73
74
    /**
75
     * Return form type.
76
     *
77
     * @return string
78
     */
79
    public function getType()
80
    {
81
        return 'string';
82
    }
83
    
84
    /**
85
     * Defines translator.
86
     *
87
     * @param TranslatorInterface $translator
88
     *
89
     * @return void
90
     */
91 2
    public function setTranslator(TranslatorInterface $translator)
92
    {
93 2
        $this->translator = $translator;
94 2
    }
95
    
96
    /**
97
     * Define twig environment.
98
     *
99
     * @param Twig_Environment $twig
100
     *
101
     * @return void
102
     */
103 2
    public function setTwig(Twig_Environment $twig)
104
    {
105 2
        $this->twig = $twig;
106 2
    }
107
}
108