|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Entity\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Descriptor\ManagerInterface; |
|
6
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Conversion\ContextInterface; |
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Conversion\DefaultContext; |
|
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ConverterInterface; |
|
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\MappingInterface as PropertyMappingInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\ViewInterface as PropertyViewInterface; |
|
11
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\MappingInterface as StructureMappingInterface; |
|
12
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\ViewInterface as StructureViewInterface; |
|
13
|
|
|
use AmaTeam\ElasticSearch\API\MappingInterface; |
|
14
|
|
|
use AmaTeam\ElasticSearch\API\Mapping; |
|
15
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\ViewOperations as StructureViews; |
|
16
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations as PropertyViews; |
|
17
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
|
18
|
|
|
|
|
19
|
|
|
class Converter implements ConverterInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $manager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ManagerInterface $manager |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ManagerInterface $manager) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->manager = $manager; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function convert(StructureMappingInterface $mapping, ContextInterface $context = null): MappingInterface |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->convertStructureMapping($mapping, $context ?? new DefaultContext()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function convertStructureMapping(StructureMappingInterface $mapping, ContextInterface $context): Mapping |
|
40
|
|
|
{ |
|
41
|
|
|
$target = new Mapping(); |
|
42
|
|
|
$views = static::computeStructureViews($mapping, $context); |
|
43
|
|
|
$view = StructureViews::merge(...$views); |
|
44
|
|
|
$type = $context->isRootLevelMapping() ? RootType::ID : $view->getType(); |
|
45
|
|
|
if ($type) { |
|
|
|
|
|
|
46
|
|
|
$target->setType($type); |
|
47
|
|
|
} |
|
48
|
|
|
foreach ($view->getParameters() as $parameter => $value) { |
|
49
|
|
|
$target->setParameter($parameter, $value); |
|
50
|
|
|
} |
|
51
|
|
|
foreach ($mapping->getProperties() as $name => $property) { |
|
52
|
|
|
if (in_array($name, $view->getIgnoredProperties(), true)) { |
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
$innerContext = DefaultContext::from($context)->setRootLevelMapping(false); |
|
56
|
|
|
$target->setProperty($name, $this->convertPropertyMapping($property, $innerContext)); |
|
57
|
|
|
} |
|
58
|
|
|
return $target; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function convertPropertyMapping(PropertyMappingInterface $mapping, ContextInterface $context): Mapping |
|
62
|
|
|
{ |
|
63
|
|
|
$views = static::computePropertyViews($mapping, $context); |
|
64
|
|
|
$view = PropertyViews::merge(...$views); |
|
65
|
|
|
$target = new Mapping(); |
|
66
|
|
|
if ($view->getTargetEntity()) { |
|
|
|
|
|
|
67
|
|
|
$descriptor = $this->manager->get($view->getTargetEntity()); |
|
68
|
|
|
$viewNames = static::computeChildViews($view, $context); |
|
69
|
|
|
$innerContext = DefaultContext::from($context) |
|
70
|
|
|
->setViews($viewNames) |
|
71
|
|
|
->setRootLevelMapping(false); |
|
72
|
|
|
$target = $this->convertStructureMapping($descriptor->getMapping(), $innerContext); |
|
73
|
|
|
} |
|
74
|
|
|
if ($view->getType()) { |
|
|
|
|
|
|
75
|
|
|
$target->setType($view->getType()); |
|
76
|
|
|
} |
|
77
|
|
|
foreach ($view->getParameters() as $parameter => $value) { |
|
78
|
|
|
$target->setParameter($parameter, $value); |
|
79
|
|
|
} |
|
80
|
|
|
return $target; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param StructureMappingInterface $mapping |
|
85
|
|
|
* @param ContextInterface $context |
|
86
|
|
|
* @return StructureViewInterface[] |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
private static function computeStructureViews(StructureMappingInterface $mapping, ContextInterface $context): array |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$result = [$mapping->getDefaultView()]; |
|
91
|
|
|
$sources = $mapping->getViews(); |
|
92
|
|
|
foreach ($context->getViews() as $name) { |
|
93
|
|
|
if (isset($sources[$name])) { |
|
94
|
|
|
$result[] = $sources[$name]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param PropertyMappingInterface $mapping |
|
102
|
|
|
* @param ContextInterface $context |
|
103
|
|
|
* @return PropertyViewInterface[] |
|
104
|
|
|
*/ |
|
105
|
|
View Code Duplication |
private static function computePropertyViews(PropertyMappingInterface $mapping, ContextInterface $context): array |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$result = [$mapping->getDefaultView()]; |
|
108
|
|
|
$sources = $mapping->getViews(); |
|
109
|
|
|
foreach ($context->getViews() as $name) { |
|
110
|
|
|
if (isset($sources[$name])) { |
|
111
|
|
|
$result[] = $sources[$name]; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private static function computeChildViews(PropertyViewInterface $view, ContextInterface $context): array |
|
118
|
|
|
{ |
|
119
|
|
|
if (!$view->shouldAppendChildViews()) { |
|
120
|
|
|
return $view->getChildViews() ?? []; |
|
121
|
|
|
} |
|
122
|
|
|
return array_unique(array_merge($context->getViews(), $view->getChildViews())); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: