Completed
Pull Request — dev (#9)
by Arnaud
02:46
created

StringField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 81.82%

Importance

Changes 3
Bugs 3 Features 1
Metric Value
wmc 6
c 3
b 3
f 1
lcom 2
cbo 5
dl 0
loc 55
ccs 18
cts 22
cp 0.8182
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 4
A configureOptions() 0 8 1
A getType() 0 4 1
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 2
    public function render($value)
24
    {
25 2
        if ($this->options->get('translation')) {
26 2
            $value = $this
27
                ->translator
28 2
                ->trans($value);
29 2
        }
30 2
        $maximumStringLength = $this
31
            ->options
32 2
            ->get('length');
33 2
        $replaceString = $this
34
            ->options
35 2
            ->get('replace');
36
37
        // truncate string if required
38 2
        if ($maximumStringLength && strlen($value) > $maximumStringLength) {
39
            $value = substr($value, 0, $maximumStringLength) . $replaceString;
40
        }
41
42 2
        return $value;
43
    }
44
45
    /**
46
     * Configure options resolver.
47
     *
48
     * @param OptionsResolver $resolver
49
     */
50 2
    public function configureOptions(OptionsResolver $resolver)
51
    {
52 2
        $resolver->setDefaults([
53 2
            'length' => $this->applicationConfiguration->getParameter('string_length'),
54 2
            'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'),
55 2
            'translation' => true,
56 2
        ]);
57 2
    }
58
59
    /**
60
     * Return form type.
61
     *
62
     * @return string
63
     */
64
    public function getType()
65
    {
66
        return 'string';
67
    }
68
}
69