Completed
Push — master ( 3cdfc2...4aa078 )
by Maksim
03:09
created

MappingBuilder::buildIndexMapping()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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