1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Mapping\Conversion; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\ProviderInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\PropertyMappingInterface; |
11
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ConverterInterface; |
12
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ContextInterface; |
13
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\DefaultContext; |
14
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\ClassMappingView; |
15
|
|
|
use AmaTeam\ElasticSearch\Entity\Provider; |
16
|
|
|
use AmaTeam\ElasticSearch\Mapping\Mapping; |
17
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\PropertyMappingView; |
18
|
|
|
|
19
|
|
|
class Converter implements ConverterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ProviderInterface |
23
|
|
|
*/ |
24
|
|
|
private $provider; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ProviderInterface $provider |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ProviderInterface $provider = null) |
30
|
|
|
{ |
31
|
|
|
$this->provider = $provider ?? new Provider(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function convert(ClassMappingInterface $source, ContextInterface $context = null): MappingInterface |
35
|
|
|
{ |
36
|
|
|
$context = $context ?? new DefaultContext(); |
37
|
|
|
return $this->renderDocumentMapping($source, $context->getViews()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function renderDocumentMapping(ClassMappingInterface $document, array $viewNames): MappingInterface |
41
|
|
|
{ |
42
|
|
|
$views = array_map([$document, 'getView'], $viewNames); |
43
|
|
|
array_unshift($views, $document->getDefaultView()); |
44
|
|
|
$view = ClassMappingView::merge(...array_filter($views)); |
45
|
|
|
$mapping = new Mapping(); |
46
|
|
|
if ($view->getType()) { |
|
|
|
|
47
|
|
|
$mapping->setType($view->getType()); |
48
|
|
|
} |
49
|
|
|
$properties = []; |
50
|
|
|
foreach ($document->getProperties() as $property => $definition) { |
51
|
|
|
$properties[$property] = $this->renderPropertyMapping($definition, $viewNames); |
52
|
|
|
} |
53
|
|
|
$mapping->setProperties($properties); |
54
|
|
|
$mapping->setParameters($view->getParameters()); |
55
|
|
|
return $mapping; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function renderPropertyMapping(PropertyMappingInterface $property, array $viewNames): MappingInterface |
59
|
|
|
{ |
60
|
|
|
$views = array_map([$property, 'getView'], $viewNames); |
61
|
|
|
array_unshift($views, $property->getDefaultView()); |
62
|
|
|
$view = PropertyMappingView::merge(...array_filter($views)); |
63
|
|
|
$mapping = new Mapping(); |
64
|
|
|
if ($view->getTargetClass()) { |
|
|
|
|
65
|
|
|
$localViewNames = $viewNames; |
66
|
|
|
if (!empty($property->getForcedViewNames())) { |
67
|
|
|
$forcedViews = $property->getForcedViewNames(); |
68
|
|
|
$append = $property->getAppendForcedViews(); |
69
|
|
|
$localViewNames = $append ? array_merge($localViewNames, $forcedViews) : $forcedViews; |
70
|
|
|
} |
71
|
|
|
$source = $this->provider->get($view->getTargetClass()); |
72
|
|
|
$mapping = $this->renderDocumentMapping($source->getMapping(), $localViewNames); |
73
|
|
|
} |
74
|
|
|
foreach ($view->getParameters() as $parameter => $value) { |
75
|
|
|
$mapping->setParameter($parameter, $value); |
76
|
|
|
} |
77
|
|
|
if ($view->getType()) { |
|
|
|
|
78
|
|
|
$mapping->setType($view->getType()); |
79
|
|
|
} |
80
|
|
|
return $mapping; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: