1 | <?php |
||
18 | class MappingBuilder |
||
19 | { |
||
20 | /** |
||
21 | * Builds mappings for an entire index. |
||
22 | * |
||
23 | * @param IndexConfigInterface $indexConfig |
||
24 | * |
||
25 | * @return array |
||
26 | */ |
||
27 | 1 | public function buildIndexMapping(IndexConfigInterface $indexConfig) |
|
28 | { |
||
29 | 1 | $typeMappings = []; |
|
30 | 1 | foreach ($indexConfig->getTypes() as $typeConfig) { |
|
31 | 1 | $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig); |
|
32 | } |
||
33 | |||
34 | 1 | $mapping = []; |
|
35 | 1 | if (!empty($typeMappings)) { |
|
36 | 1 | $mapping['mappings'] = $typeMappings; |
|
37 | } |
||
38 | // 'warmers' => $indexConfig->getWarmers(), |
||
39 | |||
40 | 1 | $settings = $indexConfig->getSettings(); |
|
41 | 1 | if (!empty($settings)) { |
|
42 | $mapping['settings'] = $settings; |
||
43 | } |
||
44 | |||
45 | 1 | return $mapping; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Builds mappings for an entire index template. |
||
50 | * |
||
51 | * @param IndexTemplateConfig $indexTemplateConfig |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | 1 | public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig) |
|
62 | |||
63 | /** |
||
64 | * Builds mappings for a single type. |
||
65 | * |
||
66 | * @param TypeConfig $typeConfig |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | 2 | public function buildTypeMapping(TypeConfig $typeConfig) |
|
71 | { |
||
72 | 2 | $mapping = $typeConfig->getMapping(); |
|
73 | |||
74 | 2 | if (null !== $typeConfig->getDynamicDateFormats()) { |
|
75 | $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); |
||
76 | } |
||
77 | |||
78 | 2 | if (null !== $typeConfig->getDateDetection()) { |
|
79 | $mapping['date_detection'] = $typeConfig->getDateDetection(); |
||
80 | } |
||
81 | |||
82 | 2 | if (null !== $typeConfig->getNumericDetection()) { |
|
83 | $mapping['numeric_detection'] = $typeConfig->getNumericDetection(); |
||
84 | } |
||
85 | |||
86 | 2 | if ($typeConfig->getAnalyzer()) { |
|
87 | $mapping['analyzer'] = $typeConfig->getAnalyzer(); |
||
88 | } |
||
89 | |||
90 | 2 | if (null !== $typeConfig->getDynamic()) { |
|
91 | $mapping['dynamic'] = $typeConfig->getDynamic(); |
||
92 | } |
||
93 | |||
94 | 2 | if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { |
|
95 | unset($mapping['dynamic_templates']); |
||
96 | } |
||
97 | |||
98 | 2 | $this->fixProperties($mapping['properties']); |
|
99 | 2 | if (!$mapping['properties']) { |
|
100 | unset($mapping['properties']); |
||
101 | } |
||
102 | |||
103 | 2 | if ($typeConfig->getModel()) { |
|
104 | $mapping['_meta']['model'] = $typeConfig->getModel(); |
||
105 | } |
||
106 | |||
107 | 2 | unset($mapping['_parent']['identifier'], $mapping['_parent']['property']); |
|
108 | |||
109 | 2 | if (empty($mapping)) { |
|
110 | // Empty mapping, we want it encoded as a {} instead of a [] |
||
111 | $mapping = new \ArrayObject(); |
||
112 | } |
||
113 | |||
114 | 2 | return $mapping; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Fixes any properties and applies basic defaults for any field that does not have |
||
119 | * required options. |
||
120 | * |
||
121 | * @param $properties |
||
122 | */ |
||
123 | 2 | private function fixProperties(&$properties) |
|
139 | } |
||
140 |