Completed
Push — 3.1.x ( fd2df9...19ca60 )
by Tim
05:29 queued 12s
created

MappingBuilder::fixProperties()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0957

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
ccs 14
cts 16
cp 0.875
rs 8.2222
cc 7
eloc 11
nc 17
nop 1
crap 7.0957
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
     * Skip adding default information to certain fields.
21
     *
22
     * @var array
23
     */
24
    private $skipTypes = array('completion');
25
26
    /**
27
     * Builds mappings for an entire index.
28
     *
29
     * @param IndexConfig $indexConfig
30
     *
31
     * @return array
32
     */
33 2
    public function buildIndexMapping(IndexConfig $indexConfig)
34
    {
35 2
        $typeMappings = array();
36 2
        foreach ($indexConfig->getTypes() as $typeConfig) {
37 2
            $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
38 2
        }
39
40 2
        $mapping = array();
41 2
        if (!empty($typeMappings)) {
42 2
            $mapping['mappings'] = $typeMappings;
43 2
        }
44
        // 'warmers' => $indexConfig->getWarmers(),
45
46 2
        $settings = $indexConfig->getSettings();
47 2
        if (!empty($settings)) {
48 1
            $mapping['settings'] = $settings;
49 1
        }
50
51 2
        return $mapping;
52
    }
53
54
    /**
55
     * Builds mappings for a single type.
56
     *
57
     * @param TypeConfig $typeConfig
58
     *
59
     * @return array
60
     */
61 3
    public function buildTypeMapping(TypeConfig $typeConfig)
62
    {
63 3
        $mapping = $typeConfig->getMapping();
64
65 3
        if (null !== $typeConfig->getDynamicDateFormats()) {
66 3
            $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats();
67 3
        }
68
69 3
        if (null !== $typeConfig->getDateDetection()) {
70 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
71 2
        }
72
73 3
        if (null !== $typeConfig->getNumericDetection()) {
74 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
75 2
        }
76
77 3
        if ($typeConfig->getIndexAnalyzer()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $typeConfig->getIndexAnalyzer() 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...
78 1
            $mapping['index_analyzer'] = $typeConfig->getIndexAnalyzer();
79 1
        }
80
81 3
        if ($typeConfig->getSearchAnalyzer()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $typeConfig->getSearchAnalyzer() 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...
82 2
            $mapping['search_analyzer'] = $typeConfig->getSearchAnalyzer();
83 2
        }
84
85 3
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
86 2
            unset($mapping['dynamic_templates']);
87 2
        }
88
89 3
        $this->fixProperties($mapping['properties']);
90 3
        if (!$mapping['properties']) {
91 2
            unset($mapping['properties']);
92 2
        }
93
94 3
        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...
95 1
            $mapping['_meta']['model'] = $typeConfig->getModel();
96 1
        }
97
98 3
        if (empty($mapping)) {
99
            // Empty mapping, we want it encoded as a {} instead of a []
100
            $mapping = new \stdClass();
101
        }
102
103 3
        return $mapping;
104
    }
105
106
    /**
107
     * Fixes any properties and applies basic defaults for any field that does not have
108
     * required options.
109
     *
110
     * @param $properties
111
     */
112 3
    private function fixProperties(&$properties)
113
    {
114 3
        foreach ($properties as $name => &$property) {
115 3
            unset($property['property_path']);
116
117 3
            if (!isset($property['type'])) {
118 3
                $property['type'] = 'string';
119 3
            }
120 3
            if ($property['type'] == 'multi_field' && isset($property['fields'])) {
121
                $this->fixProperties($property['fields']);
122
            }
123 3
            if (isset($property['properties'])) {
124 2
                $this->fixProperties($property['properties']);
125 2
            }
126 3
            if (in_array($property['type'], $this->skipTypes)) {
127 2
                continue;
128
            }
129 3
        }
130 3
    }
131
}
132