Completed
Push — master ( 91bc8f...12870b )
by Nikola
05:50
created

TwigConfiguration::configureTwigFormatOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 26
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoader Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration;
11
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
14
class TwigConfiguration
15
{
16
    public function getTwigConfigurationNode()
17
    {
18
        $twigNode = new ArrayNodeDefinition('twig');
19
20
21
        $this
22
            ->configureTwigOptions($twigNode)
23
            ->configureTwigFormatOptions($twigNode)
24
        ;
25
26
        $twigNode->addDefaultsIfNotSet();
27
28
        return $twigNode;
29
    }
30
31
    private function configureTwigOptions(ArrayNodeDefinition $node)
32
    {
33
        $node
34
            ->fixXmlConfig('path')
35
            ->children()
36
                ->variableNode('autoescape')->defaultValue(false)->end()
37
                ->scalarNode('autoescape_service')->defaultNull()->end()
38
                ->scalarNode('autoescape_service_method')->defaultNull()->end()
39
                ->scalarNode('base_template_class')->example('Twig_Template')->cannotBeEmpty()->end()
40
                ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/query_resources/twig')->end()
41
                ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
42
                ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
43
                ->booleanNode('strict_variables')->end()
44
                ->scalarNode('auto_reload')->end()
45
                ->integerNode('optimizations')->min(-1)->end()
46
                ->arrayNode('paths')
47
                    ->normalizeKeys(false)
48
                    ->useAttributeAsKey('paths')
49
                    ->beforeNormalization()
50
                    ->always()
51
                    ->then(function ($paths) {
52
                        $normalized = array();
53
                        foreach ($paths as $path => $namespace) {
54
                            if (is_array($namespace)) {
55
                                // xml
56
                                $path = $namespace['value'];
57
                                $namespace = $namespace['namespace'];
58
                            }
59
60
                            // path within the default namespace
61
                            if (ctype_digit((string) $path)) {
62
                                $path = $namespace;
63
                                $namespace = null;
64
                            }
65
66
                            $normalized[$path] = $namespace;
67
                        }
68
69
                        return $normalized;
70
                    })
71
                    ->end()
72
                    ->prototype('variable')->end()
73
                ->end()
74
            ->end()
75
        ;
76
77
        return $this;
78
    }
79
80
    private function configureTwigFormatOptions(ArrayNodeDefinition $node)
81
    {
82
        $node
83
            ->children()
84
                ->arrayNode('date')
85
                    ->info('The default format options used by the date filter')
86
                    ->addDefaultsIfNotSet()
87
                    ->children()
88
                        ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
89
                        ->scalarNode('interval_format')->defaultValue('%d days')->end()
90
                        ->scalarNode('timezone')
91
                            ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
92
                            ->defaultNull()
93
                        ->end()
94
                    ->end()
95
                ->end()
96
                ->arrayNode('number_format')
97
                    ->info('The default format options for the number_format filter')
98
                    ->addDefaultsIfNotSet()
99
                    ->children()
100
                        ->integerNode('decimals')->defaultValue(0)->end()
101
                        ->scalarNode('decimal_point')->defaultValue('.')->end()
102
                        ->scalarNode('thousands_separator')->defaultValue(',')->end()
103
                    ->end()
104
                ->end()
105
            ->end()
106
        ;
107
108
        return $this;
109
    }
110
111
    public static function build()
112
    {
113
        $configuration = new static();
114
        return $configuration->getTwigConfigurationNode();
115
    }
116
}
117