Completed
Push — master ( 2bf877...d0e692 )
by Vladimir
03:44
created

Configuration::excludedFieldsSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Koff\Bundle\I18nFormBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * Class Configuration.
11
 *
12
 * @author Sadicov Vladimir <[email protected]>
13
 */
14
class Configuration implements ConfigurationInterface
15
{
16
    private $convertStringToArray = null;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getConfigTreeBuilder()
22
    {
23
        $treeBuilder = new TreeBuilder();
24
        $rootNode = $treeBuilder->root('i18n_form');
25
26
        $this->convertStringToArray = function ($v) {
27
            return preg_split('/\s*[,|]\s*/', $v);
28
        };
29
30
        $this->localesSection($rootNode);
31
        $this->requiredLocalesSection($rootNode);
32
        $this->excludedFieldsSection($rootNode);
33
        $this->formThemeSection($rootNode);
34
35
        return $treeBuilder;
36
    }
37
38
    /**
39
     * @param ArrayNodeDefinition $rootNode
40
     */
41
    private function localesSection(ArrayNodeDefinition $rootNode)
42
    {
43
        $rootNode
44
            ->children()
45
                ->arrayNode('locales')
46
                    ->defaultValue(['en'])
47
                    ->requiresAtLeastOneElement()
48
                    ->scalarPrototype()->end()
49
                    ->beforeNormalization()->ifString()->then($this->convertStringToArray)->end()
0 ignored issues
show
Bug introduced by
The method beforeNormalization() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...\Builder\NodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                    ->/** @scrutinizer ignore-call */ beforeNormalization()->ifString()->then($this->convertStringToArray)->end()
Loading history...
50
                ->end()
51
        ;
52
    }
53
54
    /**
55
     * @param ArrayNodeDefinition $rootNode
56
     */
57
    private function requiredLocalesSection(ArrayNodeDefinition $rootNode)
58
    {
59
        $rootNode
60
            ->children()
61
                ->arrayNode('required_locales')
62
                    ->scalarPrototype()->end()
63
                    ->beforeNormalization()->ifString()->then($this->convertStringToArray)->end()
64
                ->end()
65
        ;
66
    }
67
68
    /**
69
     * @param ArrayNodeDefinition $rootNode
70
     */
71
    private function excludedFieldsSection(ArrayNodeDefinition $rootNode)
72
    {
73
        $rootNode
74
            ->children()
75
                ->arrayNode('excluded_fields')
76
                    ->defaultValue(['id', 'locale', 'translatable'])
77
                    ->scalarPrototype()->end()
78
                    ->beforeNormalization()->ifString()->then($this->convertStringToArray)->end()
79
                ->end()
80
        ;
81
    }
82
83
    private function formThemeSection(ArrayNodeDefinition $rootNode)
84
    {
85
        $rootNode
86
            ->children()
87
                ->scalarNode('form_theme')
88
                    ->validate()->ifNotInArray(['bootstrap_3', 'bootstrap_4'])->thenUnset()->end()
89
                ->end()
90
        ;
91
    }
92
}
93