1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Mapping; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\DocumentMappingInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\PropertyMappingInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\RendererInterface; |
11
|
|
|
|
12
|
|
|
class Renderer implements RendererInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var MappingProvider |
16
|
|
|
*/ |
17
|
|
|
private $provider; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param MappingProvider $provider |
21
|
|
|
*/ |
22
|
|
|
public function __construct(MappingProvider $provider = null) |
23
|
|
|
{ |
24
|
|
|
$this->provider = $provider ?? new MappingProvider(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function render(DocumentMappingInterface $source, string ...$views): MappingInterface |
28
|
|
|
{ |
29
|
|
|
if ($source->getForcedViewName()) { |
|
|
|
|
30
|
|
|
$views = [$source->getForcedViewName()]; |
31
|
|
|
} |
32
|
|
|
return $this->renderDocumentMapping($source, $views); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function renderDocumentMapping(DocumentMappingInterface $document, array $viewNames): MappingInterface |
36
|
|
|
{ |
37
|
|
|
$views = array_map([$document, 'getView'], $viewNames); |
38
|
|
|
array_unshift($views, $document->getDefaultView()); |
39
|
|
|
$view = DocumentMappingView::merge(...array_filter($views)); |
40
|
|
|
$mapping = new Mapping(); |
41
|
|
|
if ($view->getType()) { |
|
|
|
|
42
|
|
|
$mapping->setType($view->getType()); |
43
|
|
|
} |
44
|
|
|
$properties = []; |
45
|
|
|
foreach ($document->getProperties() as $property => $definition) { |
46
|
|
|
$properties[$property] = $this->renderPropertyMapping($definition, $viewNames); |
47
|
|
|
} |
48
|
|
|
$mapping->setProperties($properties); |
49
|
|
|
$mapping->setParameters($view->getParameters()); |
50
|
|
|
return $mapping; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function renderPropertyMapping(PropertyMappingInterface $property, array $viewNames): MappingInterface |
54
|
|
|
{ |
55
|
|
|
$views = array_map([$property, 'getView'], $viewNames); |
56
|
|
|
array_unshift($views, $property->getDefaultView()); |
57
|
|
|
$view = PropertyMappingView::merge(...array_filter($views)); |
58
|
|
|
$mapping = new Mapping(); |
59
|
|
|
if ($view->getTargetClass()) { |
|
|
|
|
60
|
|
|
$localViewNames = $viewNames; |
61
|
|
|
$source = $this->provider->getMapping($view->getTargetClass()); |
62
|
|
|
$localViewNames = $source->getForcedViewName() ? [$source->getForcedViewName()] : $localViewNames; |
63
|
|
|
$localViewNames = $property->getForcedViewName() ? [$property->getForcedViewName()] : $localViewNames; |
64
|
|
|
$mapping = $this->renderDocumentMapping($source, $localViewNames); |
65
|
|
|
} |
66
|
|
|
foreach ($view->getParameters() as $parameter => $value) { |
67
|
|
|
$mapping->setParameter($parameter, $value); |
68
|
|
|
} |
69
|
|
|
if ($view->getType()) { |
|
|
|
|
70
|
|
|
$mapping->setType($view->getType()); |
71
|
|
|
} |
72
|
|
|
return $mapping; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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: