Passed
Pull Request — master (#204)
by
unknown
23:41
created

textExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 40
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 4 2
A configureOptions() 0 19 3
A getExtendedTypes() 0 3 1
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\extension;
7
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\OptionsResolver\Options;
10
use midcom\datamanager\validation\pattern as validator;
11
use Symfony\Component\Validator\Constraints\Length;
12
use Symfony\Component\Form\FormBuilderInterface;
13
use midcom\datamanager\extension\subscriber\purifySubscriber;
14
use Symfony\Component\Form\AbstractTypeExtension;
15
use Symfony\Component\Form\Extension\Core\Type\TextType;
16
17
/**
18
 * Text extension
19
 */
20
class textExtension extends AbstractTypeExtension
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 135
    public function configureOptions(OptionsResolver $resolver)
26
    {
27 1
        $resolver->setDefault('constraints', []);
28 1
        helper::add_normalizers($resolver, [
29
            'type_config' => [
30 1
                'forbidden_patterns' => [],
31
                'maxlength' => 0,
32
                'purify' => false,
33
                'purify_config' => []
34
            ]
35
        ]);
36
        $resolver->setNormalizer('constraints', function (Options $options, $value) {
37 135
            if (!empty($options['type_config']['forbidden_patterns'])) {
38
                $value[] = new validator(['forbidden_patterns' => $options['type_config']['forbidden_patterns']]);
39
            }
40 135
            if (!empty($options['type_config']['maxlength'])) {
41 6
                $value[] = new Length(['max' => $options['type_config']['maxlength']]);
42
            }
43 135
            return $value;
44 1
        });
45 1
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 136
    public function buildForm(FormBuilderInterface $builder, array $options)
51
    {
52 136
        if (!empty($options['type_config']['purify'])) {
53
            $builder->addEventSubscriber(new purifySubscriber($options['type_config']['purify_config']));
54
        }
55 136
    }
56
57 1
    public static function getExtendedTypes() : iterable
58
    {
59 1
        return [TextType::class];
60
    }
61
}
62