Completed
Push — master ( d93623...edf55a )
by Asmir
11s
created

Configuration::addSerializersSection()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 4.0008

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 26
cts 27
cp 0.963
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 27
nc 1
nop 1
crap 4.0008
1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\SerializerBundle\DependencyInjection;
20
21
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
22
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
23
use Symfony\Component\Config\Definition\ConfigurationInterface;
24
use JMS\Serializer\Exception\InvalidArgumentException;
25
26
class Configuration implements ConfigurationInterface
27
{
28
    private $debug;
29
30
    /**
31
     * @param boolean $debug
32
     */
33 12
    public function __construct($debug = false)
34
    {
35 12
        $this->debug = $debug;
36 12
    }
37
38 12
    public function getConfigTreeBuilder()
39
    {
40 12
        $tb = new TreeBuilder();
41
42
        $root = $tb
43 12
            ->root('jms_serializer', 'array')
44 12
                ->children()
45 12
                    ->booleanNode('enable_short_alias')->defaultTrue()->end()
46 12
        ;
47
48 12
        $this->addHandlersSection($root);
49 12
        $this->addSerializersSection($root);
50 12
        $this->addMetadataSection($root);
51 12
        $this->addVisitorsSection($root);
52
53 12
        return $tb;
54
    }
55
56 12
    private function addHandlersSection(NodeBuilder $builder)
57
    {
58
        $builder
59 12
            ->arrayNode('handlers')
60 12
                ->addDefaultsIfNotSet()
61 12
                ->children()
62 12
                    ->arrayNode('datetime')
63 12
                        ->addDefaultsIfNotSet()
64 12
                        ->children()
65 12
                            ->scalarNode('default_format')->defaultValue(\DateTime::ISO8601)->end()
66 12
                            ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
67 12
                            ->scalarNode('cdata')->defaultTrue()->end()
68 12
                        ->end()
69 12
                   ->end()
70 12
                ->end()
71 12
            ->end()
72
        ;
73 12
    }
74
75 12
    private function addSerializersSection(NodeBuilder $builder)
76
    {
77
        $builder
78 12
            ->arrayNode('property_naming')
79 12
                ->addDefaultsIfNotSet()
80 12
                ->children()
81 12
                    ->scalarNode('id')->cannotBeEmpty()->end()
82 12
                    ->scalarNode('separator')->defaultValue('_')->end()
83 12
                    ->booleanNode('lower_case')->defaultTrue()->end()
84 12
                    ->booleanNode('enable_cache')->defaultTrue()->end()
85 12
                ->end()
86 12
            ->end()
87 12
            ->arrayNode('expression_evaluator')
88 12
                ->addDefaultsIfNotSet()
89 12
                ->children()
90 12
                    ->scalarNode('id')
91
                        ->defaultValue(function () {
92 10
                            if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
93 10
                                return 'jms_serializer.expression_evaluator';
94
                            }
95
                            return null;
96 12
                        })
97 12
                        ->validate()
98
                            ->always(function($v) {
99 2
                                if (!empty($v) && !class_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
100 1
                                    throw new InvalidArgumentException('You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features');
101
                                }
102 1
                                return $v;
103 12
                            })
104 12
                        ->end()
105 12
                ->end()
106 12
            ->end()
107
        ;
108 12
    }
109
110 12
    private function addMetadataSection(NodeBuilder $builder)
111
    {
112
        $builder
113 12
            ->arrayNode('metadata')
114 12
                ->addDefaultsIfNotSet()
115 12
                ->fixXmlConfig('directory', 'directories')
116 12
                ->children()
117 12
                    ->scalarNode('cache')->defaultValue('file')->end()
118 12
                    ->booleanNode('debug')->defaultValue($this->debug)->end()
119 12
                    ->arrayNode('file_cache')
120 12
                        ->addDefaultsIfNotSet()
121 12
                        ->children()
122 12
                            ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
123 12
                        ->end()
124 12
                    ->end()
125 12
                    ->booleanNode('auto_detection')->defaultTrue()->end()
126 12
                    ->booleanNode('infer_types_from_doctrine_metadata')
127 12
                        ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
128 12
                        ->defaultTrue()
129 12
                    ->end()
130 12
                    ->arrayNode('directories')
131 12
                        ->prototype('array')
132 12
                            ->children()
133 12
                                ->scalarNode('path')->isRequired()->end()
134 12
                                ->scalarNode('namespace_prefix')->defaultValue('')->end()
135 12
                            ->end()
136 12
                        ->end()
137 12
                    ->end()
138 12
                ->end()
139 12
            ->end()
140
        ;
141 12
    }
142
143 12
    private function addVisitorsSection(NodeBuilder $builder)
144
    {
145
        $builder
146 12
            ->arrayNode('visitors')
147 12
                ->addDefaultsIfNotSet()
148 12
                ->children()
149 12
                    ->arrayNode('json')
150 12
                        ->addDefaultsIfNotSet()
151 12
                        ->children()
152 12
                            ->scalarNode('options')
153 12
                                ->defaultValue(0)
154 12
                                ->beforeNormalization()
155
                                    ->ifArray()->then(function($v) {
156 1
                                        $options = 0;
157 1
                                        foreach ($v as $option) {
158 1 View Code Duplication
                                            if (is_numeric($option)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
159
                                                $options |= (int) $option;
160 1
                                            } elseif (defined($option)) {
161 1
                                                $options |= constant($option);
162 1
                                            } else {
163
                                                throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
164
                                            }
165 1
                                        }
166
167 1
                                        return $options;
168 12
                                    })
169 12
                                ->end()
170 12
                                ->beforeNormalization()
171
                                    ->ifString()->then(function($v) {
172 1 View Code Duplication
                                        if (is_numeric($v)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
173
                                            $value = (int) $v;
174 1
                                        } elseif (defined($v)) {
175 1
                                            $value = constant($v);
176 1
                                        } else {
177
                                            throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
178
                                        }
179
180 1
                                        return $value;
181 12
                                    })
182 12
                                ->end()
183 12
                                ->validate()
184 12
                                    ->always(function($v) {
185 3
                                        if (!is_int($v)) {
186
                                            throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
187
                                        }
188
189 3
                                        return $v;
190 12
                                    })
191 12
                                ->end()
192 12
                            ->end()
193 12
                        ->end()
194 12
                    ->end()
195 12
                    ->arrayNode('xml')
196 12
                        ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
197 12
                        ->addDefaultsIfNotSet()
198 12
                        ->children()
199 12
                            ->arrayNode('doctype_whitelist')
200 12
                                ->prototype('scalar')->end()
201 12
                            ->end()
202 12
                        ->end()
203 12
                    ->end()
204 12
                ->end()
205 12
            ->end()
206
        ;
207 12
    }
208
}
209