Completed
Pull Request — master (#1343)
by Dmitry
05:49
created

MappingBuilder::fixProperties()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 9
nop 1
crap 5
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Index;
13
14
use FOS\ElasticaBundle\Configuration\IndexConfigAbstract;
15
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
16
use FOS\ElasticaBundle\Configuration\TypeConfig;
17
18
class MappingBuilder
19
{
20
    /**
21
     * Builds mappings for an entire index.
22
     *
23
     * @param IndexConfigAbstract $indexConfig
24
     *
25
     * @return array
26
     */
27 5
    public function buildIndexMapping(IndexConfigAbstract $indexConfig)
28
    {
29 5
        $typeMappings = [];
30 5
        foreach ($indexConfig->getTypes() as $typeConfig) {
31 5
            $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
32
        }
33
34 5
        $mapping = [];
35 5
        if (!empty($typeMappings)) {
36 5
            $mapping['mappings'] = $typeMappings;
37
        }
38
        // 'warmers' => $indexConfig->getWarmers(),
39
40 5
        $settings = $indexConfig->getSettings();
41 5
        if (!empty($settings)) {
42 2
            $mapping['settings'] = $settings;
43
        }
44
45 5
        return $mapping;
46
    }
47
48
    /**
49
     * Builds mappings for an entire index template.
50
     *
51
     * @param IndexTemplateConfig $indexTemplateConfig
52
     *
53
     * @return array
54
     */
55
    public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig)
56
    {
57
        $mapping = $this->buildIndexMapping($indexTemplateConfig);
58
        $mapping['template'] = $indexTemplateConfig->getTemplate();
59
        return $mapping;
60
    }
61
62
    /**
63
     * Builds mappings for a single type.
64
     *
65
     * @param TypeConfig $typeConfig
66
     *
67
     * @return array
68
     */
69 6
    public function buildTypeMapping(TypeConfig $typeConfig)
70
    {
71 6
        $mapping = $typeConfig->getMapping();
72
73 6
        if (null !== $typeConfig->getDynamicDateFormats()) {
74 5
            $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats();
75
        }
76
77 6
        if (null !== $typeConfig->getDateDetection()) {
78 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
79
        }
80
81 6
        if (null !== $typeConfig->getNumericDetection()) {
82 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
83
        }
84
85 6
        if ($typeConfig->getAnalyzer()) {
86
            $mapping['analyzer'] = $typeConfig->getAnalyzer();
87
        }
88
89 6
        if (null !== $typeConfig->getDynamic()) {
90 2
            $mapping['dynamic'] = $typeConfig->getDynamic();
91
        }
92
93 6
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
94 5
            unset($mapping['dynamic_templates']);
95
        }
96
97 6
        $this->fixProperties($mapping['properties']);
98 6
        if (!$mapping['properties']) {
99 3
            unset($mapping['properties']);
100
        }
101
102 6
        if ($typeConfig->getModel()) {
103 3
            $mapping['_meta']['model'] = $typeConfig->getModel();
104
        }
105
106 6
        unset($mapping['_parent']['identifier'], $mapping['_parent']['property']);
107
108 6
        if (empty($mapping)) {
109
            // Empty mapping, we want it encoded as a {} instead of a []
110
            $mapping = new \ArrayObject();
111
        }
112
113 6
        return $mapping;
114
    }
115
116
    /**
117
     * Fixes any properties and applies basic defaults for any field that does not have
118
     * required options.
119
     *
120
     * @param $properties
121
     */
122 6
    private function fixProperties(&$properties)
123
    {
124 6
        foreach ($properties as $name => &$property) {
125 6
            unset($property['property_path']);
126
127 6
            if (!isset($property['type'])) {
128 5
                $property['type'] = 'text';
129
            }
130 6
            if (isset($property['fields'])) {
131 2
                $this->fixProperties($property['fields']);
132
            }
133 6
            if (isset($property['properties'])) {
134 6
                $this->fixProperties($property['properties']);
135
            }
136
        }
137 6
    }
138
}
139