Completed
Pull Request — master (#542)
by Asmir
16:51
created

Configuration::addMetadataSection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 29
cts 29
cp 1
rs 8.8571
cc 1
eloc 29
nc 1
nop 1
crap 1
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 7
    public function __construct($debug = false)
34
    {
35 7
        $this->debug = $debug;
36 7
    }
37
38 7
    public function getConfigTreeBuilder()
39
    {
40 7
        $tb = new TreeBuilder();
41
42
        $root = $tb
43 7
            ->root('jms_serializer', 'array')
44 7
                ->children()
45 7
                    ->booleanNode('enable_short_alias')->defaultTrue()->end()
46 7
        ;
47
48 7
        $this->addHandlersSection($root);
49 7
        $this->addSerializersSection($root);
50 7
        $this->addMetadataSection($root);
51 7
        $this->addVisitorsSection($root);
52
53 7
        return $tb;
54
    }
55
56 7
    private function addHandlersSection(NodeBuilder $builder)
57
    {
58
        $builder
59 7
            ->arrayNode('handlers')
60 7
                ->addDefaultsIfNotSet()
61 7
                ->children()
62 7
                    ->arrayNode('datetime')
63 7
                        ->addDefaultsIfNotSet()
64 7
                        ->children()
65 7
                            ->scalarNode('default_format')->defaultValue(\DateTime::ISO8601)->end()
66 7
                            ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
67 7
                            ->scalarNode('cdata')->defaultTrue()->end()
68 7
                        ->end()
69 7
                   ->end()
70 7
                ->end()
71 7
            ->end()
72
        ;
73 7
    }
74
75 7
    private function addSerializersSection(NodeBuilder $builder)
76
    {
77
        $builder
78 7
            ->arrayNode('property_naming')
79 7
                ->addDefaultsIfNotSet()
80 7
                ->children()
81 7
                    ->scalarNode('id')->cannotBeEmpty()->end()
82 7
                    ->scalarNode('separator')->defaultValue('_')->end()
83 7
                    ->booleanNode('lower_case')->defaultTrue()->end()
84 7
                    ->booleanNode('enable_cache')->defaultTrue()->end()
85 7
                ->end()
86 7
            ->end()
87
        ;
88 7
    }
89
90 7
    private function addMetadataSection(NodeBuilder $builder)
91
    {
92
        $builder
93 7
            ->arrayNode('metadata')
94 7
                ->addDefaultsIfNotSet()
95 7
                ->fixXmlConfig('directory', 'directories')
96 7
                ->children()
97 7
                    ->scalarNode('cache')->defaultValue('file')->end()
98 7
                    ->booleanNode('debug')->defaultValue($this->debug)->end()
99 7
                    ->arrayNode('file_cache')
100 7
                        ->addDefaultsIfNotSet()
101 7
                        ->children()
102 7
                            ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
103 7
                        ->end()
104 7
                    ->end()
105 7
                    ->booleanNode('auto_detection')->defaultTrue()->end()
106 7
                    ->booleanNode('infer_types_from_doctrine_metadata')
107 7
                        ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
108 7
                        ->defaultTrue()
109 7
                    ->end()
110 7
                    ->arrayNode('directories')
111 7
                        ->prototype('array')
112 7
                            ->children()
113 7
                                ->scalarNode('path')->isRequired()->end()
114 7
                                ->scalarNode('namespace_prefix')->defaultValue('')->end()
115 7
                            ->end()
116 7
                        ->end()
117 7
                    ->end()
118 7
                ->end()
119 7
            ->end()
120
        ;
121 7
    }
122
123 7
    private function addVisitorsSection(NodeBuilder $builder)
124
    {
125
        $builder
126 7
            ->arrayNode('visitors')
127 7
                ->addDefaultsIfNotSet()
128 7
                ->children()
129 7
                    ->arrayNode('json')
130 7
                        ->addDefaultsIfNotSet()
131 7
                        ->children()
132 7
                            ->scalarNode('options')
133 7
                                ->defaultValue(0)
134 7
                                ->beforeNormalization()
135
                                    ->ifArray()->then(function($v) {
136 1
                                        $options = 0;
137 1
                                        foreach ($v as $option) {
138 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...
139
                                                $options |= (int) $option;
140 1
                                            } elseif (defined($option)) {
141 1
                                                $options |= constant($option);
142 1
                                            } else {
143
                                                throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
144
                                            }
145 1
                                        }
146
147 1
                                        return $options;
148 7
                                    })
149 7
                                ->end()
150 7
                                ->beforeNormalization()
151
                                    ->ifString()->then(function($v) {
152 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...
153
                                            $value = (int) $v;
154 1
                                        } elseif (defined($v)) {
155 1
                                            $value = constant($v);
156 1
                                        } else {
157
                                            throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
158
                                        }
159
160 1
                                        return $value;
161 7
                                    })
162 7
                                ->end()
163 7
                                ->validate()
164 7
                                    ->always(function($v) {
165 3
                                        if (!is_int($v)) {
166
                                            throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
167
                                        }
168
169 3
                                        return $v;
170 7
                                    })
171 7
                                ->end()
172 7
                            ->end()
173 7
                        ->end()
174 7
                    ->end()
175 7
                    ->arrayNode('xml')
176 7
                        ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
177 7
                        ->addDefaultsIfNotSet()
178 7
                        ->children()
179 7
                            ->arrayNode('doctype_whitelist')
180 7
                                ->prototype('scalar')->end()
181 7
                            ->end()
182 7
                        ->end()
183 7
                    ->end()
184 7
                ->end()
185 7
            ->end()
186
        ;
187 7
    }
188
}
189