Completed
Push — master ( f7313f...f3d09b )
by Karel
03:49
created

MappingBuilder::fixProperties()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 9
nc 9
nop 1
crap 5
1
<?php
2
3
/**
4
 * This file is part of the FOSElasticaBundle project.
5
 *
6
 * (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
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\IndexConfig;
15
use FOS\ElasticaBundle\Configuration\TypeConfig;
16
17
class MappingBuilder
18
{
19
    /**
20
     * Builds mappings for an entire index.
21
     *
22
     * @param IndexConfig $indexConfig
23
     *
24
     * @return array
25
     */
26 3
    public function buildIndexMapping(IndexConfig $indexConfig)
27
    {
28 3
        $typeMappings = array();
29 3
        foreach ($indexConfig->getTypes() as $typeConfig) {
30 3
            $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
31
        }
32
33 3
        $mapping = array();
34 3
        if (!empty($typeMappings)) {
35 3
            $mapping['mappings'] = $typeMappings;
36
        }
37
        // 'warmers' => $indexConfig->getWarmers(),
38
39 3
        $settings = $indexConfig->getSettings();
40 3
        if (!empty($settings)) {
41 2
            $mapping['settings'] = $settings;
42
        }
43
44 3
        return $mapping;
45
    }
46
47
    /**
48
     * Builds mappings for a single type.
49
     *
50
     * @param TypeConfig $typeConfig
51
     *
52
     * @return array
53
     */
54 4
    public function buildTypeMapping(TypeConfig $typeConfig)
55
    {
56 4
        $mapping = $typeConfig->getMapping();
57
58 4
        if (null !== $typeConfig->getDynamicDateFormats()) {
59 3
            $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats();
60
        }
61
62 4
        if (null !== $typeConfig->getDateDetection()) {
63 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
64
        }
65
66 4
        if (null !== $typeConfig->getNumericDetection()) {
67 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
68
        }
69
70 4
        if ($typeConfig->getAnalyzer()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $typeConfig->getAnalyzer() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
71
            $mapping['analyzer'] = $typeConfig->getAnalyzer();
72
        }
73
74 4
        if ($typeConfig->getDynamic() !== null) {
75 2
            $mapping['dynamic'] = $typeConfig->getDynamic();
76
        }
77
78 4
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
79 3
            unset($mapping['dynamic_templates']);
80
        }
81
82 4
        $this->fixProperties($mapping['properties']);
83 4
        if (!$mapping['properties']) {
84 3
            unset($mapping['properties']);
85
        }
86
87 4
        if ($typeConfig->getModel()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $typeConfig->getModel() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88 1
            $mapping['_meta']['model'] = $typeConfig->getModel();
89
        }
90
91 4
        unset($mapping['_parent']['identifier'], $mapping['_parent']['property']);
92
93 4
        if (empty($mapping)) {
94
            // Empty mapping, we want it encoded as a {} instead of a []
95
            $mapping = new \ArrayObject();
96
        }
97
98 4
        return $mapping;
99
    }
100
101
    /**
102
     * Fixes any properties and applies basic defaults for any field that does not have
103
     * required options.
104
     *
105
     * @param $properties
106
     */
107 4
    private function fixProperties(&$properties)
108
    {
109 4
        foreach ($properties as $name => &$property) {
110 4
            unset($property['property_path']);
111
112 4
            if (!isset($property['type'])) {
113 3
                $property['type'] = 'text';
114
            }
115 4
            if (isset($property['fields'])) {
116 2
                $this->fixProperties($property['fields']);
117
            }
118 4
            if (isset($property['properties'])) {
119 4
                $this->fixProperties($property['properties']);
120
            }
121
        }
122 4
    }
123
}
124