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
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
public function getConfigTreeBuilder() |
21
|
|
|
{ |
22
|
|
|
$treeBuilder = new TreeBuilder(); |
23
|
|
|
$rootNode = $treeBuilder->root('i18n_form'); |
24
|
|
|
|
25
|
|
|
$this->convertStringToArray = function ($v) { |
26
|
|
|
return preg_split('/\s*[,|]\s*/', $v); |
27
|
|
|
}; |
28
|
|
|
|
29
|
|
|
$this->localesSection($rootNode); |
30
|
|
|
$this->requiredLocalesSection($rootNode); |
31
|
|
|
$this->excludedFieldsSection($rootNode); |
32
|
|
|
|
33
|
|
|
return $treeBuilder; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ArrayNodeDefinition $rootNode |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
private function localesSection(ArrayNodeDefinition $rootNode) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$rootNode |
42
|
|
|
->children() |
43
|
|
|
->arrayNode('locales') |
44
|
|
|
->defaultValue(['en']) |
45
|
|
|
->beforeNormalization() |
46
|
|
|
->ifString() |
47
|
|
|
->then($this->convertStringToArray) |
48
|
|
|
->end() |
49
|
|
|
->end() |
50
|
|
|
->requiresAtLeastOneElement() |
|
|
|
|
51
|
|
|
->prototype('scalar') |
52
|
|
|
->end() |
53
|
|
|
->end() |
54
|
|
|
; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ArrayNodeDefinition $rootNode |
59
|
|
|
*/ |
60
|
|
|
private function requiredLocalesSection(ArrayNodeDefinition $rootNode) |
61
|
|
|
{ |
62
|
|
|
$rootNode |
63
|
|
|
->children() |
64
|
|
|
->arrayNode('required_locales') |
65
|
|
|
->beforeNormalization() |
66
|
|
|
->ifString() |
67
|
|
|
->then($this->convertStringToArray) |
68
|
|
|
->end() |
69
|
|
|
->end() |
70
|
|
|
->prototype('scalar') |
|
|
|
|
71
|
|
|
->end() |
72
|
|
|
->end() |
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ArrayNodeDefinition $rootNode |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function excludedFieldsSection(ArrayNodeDefinition $rootNode) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$rootNode |
82
|
|
|
->children() |
83
|
|
|
->arrayNode('locales') |
84
|
|
|
->defaultValue(['id', 'locale', 'translatable']) |
85
|
|
|
->beforeNormalization() |
86
|
|
|
->ifString() |
87
|
|
|
->then($this->convertStringToArray) |
88
|
|
|
->end() |
89
|
|
|
->end() |
90
|
|
|
->prototype('scalar') |
91
|
|
|
->end() |
92
|
|
|
->end() |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.