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
|
|
|
use FOS\ElasticaBundle\Event\PostMappingBuildEvent; |
17
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
|
19
|
|
|
class MappingBuilder |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EventDispatcherInterface |
23
|
|
|
*/ |
24
|
|
|
private $dispatcher; |
25
|
|
|
|
26
|
10 |
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
27
|
|
|
{ |
28
|
10 |
|
$this->dispatcher = $eventDispatcher; |
29
|
10 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Builds mappings for an entire index. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
8 |
|
public function buildIndexMapping(IndexConfigInterface $indexConfig) |
37
|
|
|
{ |
38
|
8 |
|
$mapping = $this->buildMapping($indexConfig->getModel(), $indexConfig); |
39
|
8 |
|
$event = new PostMappingBuildEvent($indexConfig, $mapping); |
|
|
|
|
40
|
8 |
|
$this->dispatcher->dispatch($event); |
|
|
|
|
41
|
|
|
|
42
|
8 |
|
$mapping = $event->getMapping(); |
43
|
|
|
|
44
|
8 |
|
$mappingIndex = []; |
45
|
8 |
|
if (!empty($mapping)) { |
46
|
8 |
|
$mappingIndex['mappings'] = $mapping; |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
$settings = $indexConfig->getSettings(); |
50
|
8 |
|
if (!empty($settings)) { |
51
|
5 |
|
$mappingIndex['settings'] = $settings; |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
return $mappingIndex; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Builds mappings for an entire index template. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
5 |
|
public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig) |
63
|
|
|
{ |
64
|
5 |
|
$mapping = $this->buildIndexMapping($indexTemplateConfig); |
65
|
5 |
|
$mapping['template'] = $indexTemplateConfig->getTemplate(); |
66
|
|
|
|
67
|
5 |
|
return $mapping; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Builds mappings for a single type. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
9 |
|
public function buildMapping(?string $model, IndexConfigInterface $indexConfig) |
76
|
|
|
{ |
77
|
9 |
|
$mapping = $indexConfig->getMapping(); |
78
|
|
|
|
79
|
9 |
|
if (null !== $indexConfig->getDynamicDateFormats()) { |
80
|
7 |
|
$mapping['dynamic_date_formats'] = $indexConfig->getDynamicDateFormats(); |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
if (null !== $indexConfig->getDateDetection()) { |
84
|
1 |
|
$mapping['date_detection'] = $indexConfig->getDateDetection(); |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
if (null !== $indexConfig->getNumericDetection()) { |
88
|
1 |
|
$mapping['numeric_detection'] = $indexConfig->getNumericDetection(); |
89
|
|
|
} |
90
|
|
|
|
91
|
9 |
|
if ($indexConfig->getAnalyzer()) { |
92
|
|
|
$mapping['analyzer'] = $indexConfig->getAnalyzer(); |
93
|
|
|
} |
94
|
|
|
|
95
|
9 |
|
if (null !== $indexConfig->getDynamic()) { |
96
|
1 |
|
$mapping['dynamic'] = $indexConfig->getDynamic(); |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { |
100
|
6 |
|
unset($mapping['dynamic_templates']); |
101
|
|
|
} |
102
|
|
|
|
103
|
9 |
|
$this->fixProperties($mapping['properties']); |
104
|
9 |
|
if (!$mapping['properties']) { |
105
|
|
|
unset($mapping['properties']); |
106
|
|
|
} |
107
|
|
|
|
108
|
9 |
|
if ($model) { |
109
|
2 |
|
$mapping['_meta']['model'] = $model; |
110
|
|
|
} |
111
|
|
|
|
112
|
9 |
|
if (empty($mapping)) { |
113
|
|
|
// Empty mapping, we want it encoded as a {} instead of a [] |
114
|
|
|
$mapping = new \ArrayObject(); |
115
|
|
|
} |
116
|
|
|
|
117
|
9 |
|
return $mapping; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Fixes any properties and applies basic defaults for any field that does not have |
122
|
|
|
* required options. |
123
|
|
|
* |
124
|
|
|
* @param $properties |
125
|
|
|
*/ |
126
|
9 |
|
private function fixProperties(&$properties) |
127
|
|
|
{ |
128
|
9 |
|
foreach ($properties as $name => &$property) { |
129
|
9 |
|
unset($property['property_path']); |
130
|
|
|
|
131
|
9 |
|
if (!isset($property['type'])) { |
132
|
3 |
|
$property['type'] = 'text'; |
133
|
|
|
} |
134
|
9 |
|
if (isset($property['fields'])) { |
135
|
1 |
|
$this->fixProperties($property['fields']); |
136
|
|
|
} |
137
|
9 |
|
if (isset($property['properties'])) { |
138
|
1 |
|
$this->fixProperties($property['properties']); |
139
|
|
|
} |
140
|
|
|
} |
141
|
9 |
|
} |
142
|
|
|
} |
143
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.