|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Indexing; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Indexing; |
|
6
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\AnalysisInterface; |
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\Normalization\Context; |
|
8
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\Normalization\ContextInterface; |
|
9
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\NormalizerInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\API\IndexingInterface; |
|
11
|
|
|
use AmaTeam\ElasticSearch\Indexing\Option\Infrastructure\Registry; |
|
12
|
|
|
|
|
13
|
|
|
class Normalizer implements NormalizerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Registry |
|
17
|
|
|
*/ |
|
18
|
|
|
private $optionRegistry; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Registry $optionRegistry |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(Registry $optionRegistry = null) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->optionRegistry = $optionRegistry ?? Registry::getInstance(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function normalize(IndexingInterface $indexing, ContextInterface $context = null): IndexingInterface |
|
29
|
|
|
{ |
|
30
|
|
|
$context = $context ?? new Context(); |
|
31
|
|
|
return (new Indexing()) |
|
32
|
|
|
->setType($indexing->getType()) |
|
33
|
|
|
->setWriteIndices(array_unique($indexing->getWriteIndices())) |
|
34
|
|
|
->setReadIndices(array_unique($indexing->getReadIndices())) |
|
35
|
|
|
->setAnalysis($this->normalizeAnalysis($indexing->getAnalysis(), $context)) |
|
36
|
|
|
->setOptions($this->normalizeOptions($indexing->getOptions(), $context)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function normalizeAnalysis(AnalysisInterface $analysis, ContextInterface $context): AnalysisInterface |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
// TODO |
|
42
|
|
|
return $analysis; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function normalizeOptions(array $options, ContextInterface $context): array |
|
46
|
|
|
{ |
|
47
|
|
|
$result = []; |
|
48
|
|
|
foreach ($options as $name => $value) { |
|
49
|
|
|
$option = $this->optionRegistry->find($name); |
|
50
|
|
|
if ($option) { |
|
51
|
|
|
$result[$option->getId()] = $value; |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
if ($context->shouldPreserveUnknownOptions() || in_array($name, $context->getPreservedOptions())) { |
|
55
|
|
|
$result[$name] = $value; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.