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\Mapping\PropertyMappingInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ProviderInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
11
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\CompilerInterface; |
12
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ContextInterface; |
13
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\DefaultContext; |
14
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingView; |
15
|
|
|
use AmaTeam\ElasticSearch\Entity\Provider; |
16
|
|
|
use AmaTeam\ElasticSearch\Mapping\Mapping; |
17
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\PropertyMappingView; |
18
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
19
|
|
|
|
20
|
|
|
class Compiler implements CompilerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ProviderInterface |
24
|
|
|
*/ |
25
|
|
|
private $provider; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ProviderInterface $provider |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ProviderInterface $provider = null) |
31
|
|
|
{ |
32
|
|
|
$this->provider = $provider ?? new Provider(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function convert(ClassMappingInterface $source, ContextInterface $context = null): MappingInterface |
36
|
|
|
{ |
37
|
|
|
$context = $context ?? new DefaultContext(); |
38
|
|
|
return $this->renderDocumentMapping($source, $context); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function renderDocumentMapping(ClassMappingInterface $document, ContextInterface $context): MappingInterface |
42
|
|
|
{ |
43
|
|
|
$views = array_map([$document, 'getView'], $context->getViews()); |
44
|
|
|
array_unshift($views, $document->getDefaultView()); |
45
|
|
|
$view = ClassMappingView::merge(...array_filter($views)); |
46
|
|
|
$mapping = new Mapping(); |
47
|
|
|
$type = $context->isRootMapping() ? RootType::ID : $view->getType(); |
48
|
|
|
if ($type) { |
|
|
|
|
49
|
|
|
$mapping->setType($type); |
50
|
|
|
} |
51
|
|
|
$properties = []; |
52
|
|
|
foreach ($document->getProperties() as $property => $definition) { |
53
|
|
|
if (in_array($property, $view->getIgnoredProperties())) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
$innerContext = DefaultContext::from($context)->setRootMapping(false); |
57
|
|
|
$rendered = $this->compilePropertyMapping($definition, $innerContext); |
58
|
|
|
if ($rendered->getType()) { |
|
|
|
|
59
|
|
|
$properties[$property] = $rendered; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$mapping->setProperties($properties); |
63
|
|
|
$mapping->setParameters($view->getParameters()); |
64
|
|
|
return $mapping; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function compilePropertyMapping( |
68
|
|
|
PropertyMappingInterface $property, |
69
|
|
|
ContextInterface $context |
70
|
|
|
): MappingInterface { |
71
|
|
|
$views = array_map([$property, 'getView'], $context->getViews()); |
72
|
|
|
array_unshift($views, $property->getDefaultView()); |
73
|
|
|
$view = PropertyMappingView::merge(...array_filter($views)); |
74
|
|
|
$mapping = new Mapping(); |
75
|
|
|
if ($view->getTargetClass()) { |
|
|
|
|
76
|
|
|
$localViewNames = $context->getViews(); |
77
|
|
|
if (!empty($property->getForcedViewNames())) { |
78
|
|
|
$forcedViews = $property->getForcedViewNames(); |
79
|
|
|
$append = $property->shouldAppendForcedViews(); |
80
|
|
|
$localViewNames = $append ? array_merge($localViewNames, $forcedViews) : $forcedViews; |
81
|
|
|
} |
82
|
|
|
$source = $this->provider->get($view->getTargetClass()); |
83
|
|
|
$innerContext = DefaultContext::from($context)->setViews($localViewNames); |
84
|
|
|
$mapping = $this->renderDocumentMapping($source, $innerContext); |
85
|
|
|
} |
86
|
|
|
foreach ($view->getParameters() as $parameter => $value) { |
87
|
|
|
$mapping->setParameter($parameter, $value); |
88
|
|
|
} |
89
|
|
|
if ($view->getType()) { |
|
|
|
|
90
|
|
|
$mapping->setType($view->getType()); |
91
|
|
|
} |
92
|
|
|
return $mapping; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.