Completed
Push — 0.3-dev ( 291149...449c2f )
by Arnaud
14:26
created

StringField::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\Field;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
/**
10
 * String field.
11
 *
12
 * It render a string that can be truncated according to is length
13
 * Note : according php7 (nightly), class can not be named String anymore
14
 */
15
class StringField extends Field
16
{
17
    /**
18
     * Length before truncate the string. If null, no truncation will be made.
19
     *
20
     * @var int
21
     */
22
    protected $length;
23
24
    /**
25
     * String use for replace truncated part.
26
     *
27
     * @var string
28
     */
29
    protected $replace;
30
31
    /**
32
     * True if value should be translated at render.
33
     *
34
     * @var bool
35
     */
36
    protected $translation;
37
38
    /**
39
     * Translator.
40
     *
41
     * @var TranslatorInterface
42
     */
43
    protected $translator;
44
45
    /**
46
     * Render a string that can be truncated according to is length.
47
     *
48
     * @param $value
49
     * @return string
50
     */
51
    public function render($value)
52
    {
53
        if ($this->translation) {
54
            $value = $this->translator->trans($value);
55
        }
56
        if ($this->length && strlen($value) > $this->length) {
57
            $value = substr($value, 0, $this->length) . $this->replace;
58
        }
59
60
        return $value;
61
    }
62
63
    /**
64
     * Configure options resolver.
65
     *
66
     * @param OptionsResolver $resolver
67
     * @return mixed|void
68
     */
69
    public function configureOptions(OptionsResolver $resolver)
70
    {
71
        $resolver->setDefaults([
72
            'length' => $this->configuration->getStringLength(),
73
            'replace' => $this->configuration->getStringLengthTruncate(),
74
            'translation' => true,
75
        ]);
76
    }
77
78
    /**
79
     * Define configured options
80
     *
81
     * @param array $options
82
     * @return void
83
     */
84
    public function setOptions(array $options)
85
    {
86
        $this->length = (int)$options['length'];
87
        $this->replace = $options['replace'];
88
        $this->translation = $options['translation'];
89
    }
90
91
    /**
92
     * Define translator.
93
     *
94
     * @param TranslatorInterface $translator
95
     */
96
    public function setTranslator(TranslatorInterface $translator)
97
    {
98
        $this->translator = $translator;
99
    }
100
101
    /**
102
     * Return form type.
103
     *
104
     * @return string
105
     */
106
    public function getType()
107
    {
108
        return 'string';
109
    }
110
}
111