Completed
Pull Request — master (#90)
by Arnaud
16:42
created

StringField   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 83
ccs 10
cts 20
cp 0.5
rs 10
c 1
b 0
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 20 4
A getType() 0 4 1
A setTranslator() 0 4 1
A setTwig() 0 4 1
A getConfigurationClass() 0 4 1
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use LAG\AdminBundle\Field\Configuration\StringFieldConfiguration;
7
use LAG\AdminBundle\Field\TranslatorAwareInterface;
8
use LAG\AdminBundle\Field\TwigAwareInterface;
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['translation']) {
39
            $value = $this
40
                ->translator
41
                ->trans($value)
42
            ;
43
        }
44 2
        $maximumStringLength = $this->options['length'];
45 2
        $replaceString = $this->options['replace'];
46
47
        // Truncate string if required
48 2
        if ($maximumStringLength && strlen($value) > $maximumStringLength) {
49
            $value = substr($value, 0, $maximumStringLength).$replaceString;
50
        }
51
        // #69 : strip tags to avoid to break the layout when content contains html
52 2
        $value = strip_tags($value);
53
54 2
        return $value;
55
    }
56
57
    /**
58
     * Return form type.
59
     *
60
     * @return string
61
     */
62
    public function getType()
63
    {
64
        return 'string';
65
    }
66
    
67
    /**
68
     * Defines translator.
69
     *
70
     * @param TranslatorInterface $translator
71
     *
72
     * @return void
73
     */
74
    public function setTranslator(TranslatorInterface $translator)
75
    {
76
        $this->translator = $translator;
77
    }
78
    
79
    /**
80
     * Define twig environment.
81
     *
82
     * @param Twig_Environment $twig
83
     *
84
     * @return void
85
     */
86 2
    public function setTwig(Twig_Environment $twig)
87
    {
88 2
        $this->twig = $twig;
89 2
    }
90
    
91
    /**
92
     * Return the Field's configuration class.
93
     *
94
     * @return string
95
     */
96
    public function getConfigurationClass()
97
    {
98
        return StringFieldConfiguration::class;
99
    }
100
}
101