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
|
|
|
|
34
|
|
|
return $treeBuilder; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ArrayNodeDefinition $rootNode |
39
|
|
|
*/ |
40
|
|
|
private function localesSection(ArrayNodeDefinition $rootNode) |
41
|
|
|
{ |
42
|
|
|
$rootNode |
43
|
|
|
->children() |
44
|
|
|
->arrayNode('locales') |
45
|
|
|
->defaultValue(['en']) |
46
|
|
|
->beforeNormalization() |
47
|
|
|
->ifString()->then($this->convertStringToArray) |
48
|
|
|
->end() |
49
|
|
|
->requiresAtLeastOneElement() |
|
|
|
|
50
|
|
|
->prototype('scalar') |
51
|
|
|
->end() |
52
|
|
|
->end() |
53
|
|
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ArrayNodeDefinition $rootNode |
58
|
|
|
*/ |
59
|
|
|
private function requiredLocalesSection(ArrayNodeDefinition $rootNode) |
60
|
|
|
{ |
61
|
|
|
$rootNode |
62
|
|
|
->children() |
63
|
|
|
->arrayNode('required_locales') |
64
|
|
|
->beforeNormalization() |
65
|
|
|
->ifString()->then($this->convertStringToArray) |
66
|
|
|
->end() |
67
|
|
|
->prototype('scalar') |
|
|
|
|
68
|
|
|
->end() |
69
|
|
|
->end() |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ArrayNodeDefinition $rootNode |
75
|
|
|
*/ |
76
|
|
|
private function excludedFieldsSection(ArrayNodeDefinition $rootNode) |
77
|
|
|
{ |
78
|
|
|
$rootNode |
79
|
|
|
->children() |
80
|
|
|
->arrayNode('locales') |
81
|
|
|
->defaultValue(['id', 'locale', 'translatable']) |
82
|
|
|
->beforeNormalization() |
83
|
|
|
->ifString()->then($this->convertStringToArray) |
84
|
|
|
->end() |
85
|
|
|
->prototype('scalar') |
86
|
|
|
->end() |
87
|
|
|
->end() |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|