Completed
Pull Request — dev (#9)
by Arnaud
03:07
created

StringField::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\Field;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * String field.
10
 *
11
 * It render a string that can be truncated according to is length
12
 * Note : according php7 (nightly), class can not be named String anymore
13
 */
14
class StringField extends Field
15
{
16
17
    /**
18
     * Render a string that can be truncated according to is length.
19
     *
20
     * @param $value
21
     * @return string
22
     */
23 1
    public function render($value)
24
    {
25 1
        if ($this->options->get('translation')) {
26 1
            $value = $this
27
                ->translator
28 1
                ->trans($value);
29 1
        }
30 1
        $maximumStringLength = $this
31
            ->options
32 1
            ->get('length');
33 1
        $replaceString = $this
34
            ->options
35 1
            ->get('replace');
36
37
        // truncate string if required
38 1
        if ($maximumStringLength && strlen($value) > $maximumStringLength) {
39
            $value = substr($value, 0, $maximumStringLength) . $replaceString;
40
        }
41
42 1
        return $value;
43
    }
44
45
    /**
46
     * Configure options resolver.
47
     *
48
     * @param OptionsResolver $resolver
49
     */
50 1
    public function configureOptions(OptionsResolver $resolver)
51
    {
52 1
        $resolver->setDefaults([
53 1
            'length' => $this->applicationConfiguration->getParameter('string_length'),
54 1
            'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'),
55 1
            'translation' => true,
56 1
        ]);
57 1
    }
58
59
    /**
60
     * Return form type.
61
     *
62
     * @return string
63
     */
64
    public function getType()
65
    {
66
        return 'string';
67
    }
68
}
69