|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <https://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
|
|
|
|
|
17
|
|
|
class MappingBuilder |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Builds mappings for an entire index. |
|
21
|
|
|
*/ |
|
22
|
8 |
|
public function buildIndexMapping(IndexConfigInterface $indexConfig): array |
|
23
|
|
|
{ |
|
24
|
8 |
|
$mappingIndex = []; |
|
25
|
8 |
|
$mapping = $this->buildMapping($indexConfig->getModel(), $indexConfig); |
|
26
|
8 |
|
$settings = $indexConfig->getSettings(); |
|
27
|
|
|
|
|
28
|
8 |
|
if ($mapping) { |
|
|
|
|
|
|
29
|
8 |
|
$mappingIndex['mappings'] = $mapping; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
8 |
|
if ($settings) { |
|
|
|
|
|
|
33
|
5 |
|
$mappingIndex['settings'] = $settings; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
8 |
|
return $mappingIndex; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Builds mappings for an entire index template. |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig): array |
|
43
|
|
|
{ |
|
44
|
5 |
|
$mapping = $this->buildIndexMapping($indexTemplateConfig); |
|
45
|
5 |
|
$mapping['template'] = $indexTemplateConfig->getTemplate(); |
|
46
|
|
|
|
|
47
|
5 |
|
return $mapping; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Builds mappings for a single type. |
|
52
|
|
|
*/ |
|
53
|
9 |
|
public function buildMapping(?string $model, IndexConfigInterface $indexConfig): array |
|
54
|
|
|
{ |
|
55
|
9 |
|
$mapping = $indexConfig->getMapping(); |
|
56
|
|
|
|
|
57
|
9 |
|
if (null !== $indexConfig->getDynamicDateFormats()) { |
|
58
|
7 |
|
$mapping['dynamic_date_formats'] = $indexConfig->getDynamicDateFormats(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
9 |
|
if (null !== $indexConfig->getDateDetection()) { |
|
62
|
1 |
|
$mapping['date_detection'] = $indexConfig->getDateDetection(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
9 |
|
if (null !== $indexConfig->getNumericDetection()) { |
|
66
|
1 |
|
$mapping['numeric_detection'] = $indexConfig->getNumericDetection(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
9 |
|
if ($indexConfig->getAnalyzer()) { |
|
70
|
|
|
$mapping['analyzer'] = $indexConfig->getAnalyzer(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
9 |
|
if (null !== $indexConfig->getDynamic()) { |
|
74
|
1 |
|
$mapping['dynamic'] = $indexConfig->getDynamic(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
9 |
|
if (isset($mapping['dynamic_templates']) && !$mapping['dynamic_templates']) { |
|
78
|
6 |
|
unset($mapping['dynamic_templates']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
$this->fixProperties($mapping['properties']); |
|
82
|
9 |
|
if (!$mapping['properties']) { |
|
83
|
|
|
unset($mapping['properties']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
if ($model) { |
|
87
|
2 |
|
$mapping['_meta']['model'] = $model; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
9 |
|
return $mapping; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Fixes any properties and applies basic defaults for any field that does not have |
|
95
|
|
|
* required options. |
|
96
|
|
|
*/ |
|
97
|
9 |
|
private function fixProperties(array &$properties): void |
|
98
|
|
|
{ |
|
99
|
9 |
|
foreach ($properties as $name => &$property) { |
|
100
|
9 |
|
unset($property['property_path']); |
|
101
|
9 |
|
$property['type'] = $property['type'] ?? 'text'; |
|
102
|
|
|
|
|
103
|
9 |
|
if (isset($property['fields'])) { |
|
104
|
1 |
|
$this->fixProperties($property['fields']); |
|
105
|
|
|
} |
|
106
|
9 |
|
if (isset($property['properties'])) { |
|
107
|
1 |
|
$this->fixProperties($property['properties']); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
9 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.