Completed
Pull Request — master (#1343)
by Dmitry
08:46
created

MappingBuilder::buildIndexTemplateMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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