Completed
Pull Request — master (#1569)
by
unknown
03:13
created

MappingBuilder::buildIndexTemplateMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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