1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Entity; |
4
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Normalization\Context; |
6
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Normalization\ContextInterface; |
7
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\Normalization\Context as IndexingContext; |
8
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\NormalizerInterface as IndexingNormalizerInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\IndexingInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Normalization\Context as MappingContext; |
11
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\NormalizerInterface as MappingNormalizerInterface; |
12
|
|
|
use AmaTeam\ElasticSearch\API\Entity\NormalizerInterface; |
13
|
|
|
use AmaTeam\ElasticSearch\API\EntityInterface; |
14
|
|
|
use AmaTeam\ElasticSearch\API\MappingInterface; |
15
|
|
|
use AmaTeam\ElasticSearch\Indexing\Normalizer as IndexingNormalizer; |
16
|
|
|
use AmaTeam\ElasticSearch\Mapping\Normalizer as MappingNormalizer; |
17
|
|
|
|
18
|
|
|
class Normalizer implements NormalizerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var IndexingNormalizerInterface |
22
|
|
|
*/ |
23
|
|
|
private $indexingNormalizer; |
24
|
|
|
/** |
25
|
|
|
* @var MappingNormalizerInterface |
26
|
|
|
*/ |
27
|
|
|
private $mappingNormalizer; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->indexingNormalizer = new IndexingNormalizer(); |
32
|
|
|
$this->mappingNormalizer = new MappingNormalizer(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function normalize(EntityInterface $entity, ContextInterface $context = null): EntityInterface |
36
|
|
|
{ |
37
|
|
|
$context = $context ?? new Context(); |
38
|
|
|
$entity = Operations::from($entity) |
39
|
|
|
->setIndexing($this->normalizeIndexing($entity->getIndexing(), $context)) |
40
|
|
|
->setMapping($this->normalizeMapping($entity->getMapping(), $context)); |
41
|
|
|
return $entity; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function normalizeIndexing(IndexingInterface $indexing, ContextInterface $context): IndexingInterface |
45
|
|
|
{ |
46
|
|
|
$configuration = (new IndexingContext()) |
47
|
|
|
->setPreserveUnknownOptions($context->shouldPreserveUnknownEntries()) |
48
|
|
|
->setPreservedOptions($context->getPreservedIndexingOptions()); |
49
|
|
|
return $this->indexingNormalizer->normalize($indexing, $configuration); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function normalizeMapping(MappingInterface $mapping, ContextInterface $context): MappingInterface |
53
|
|
|
{ |
54
|
|
|
$configuration = (new MappingContext()) |
55
|
|
|
->setRootMapping(true) |
56
|
|
|
->setPreserveUnknownParameters($context->shouldPreserveUnknownEntries()) |
57
|
|
|
->setPreservedParameters($context->getPreservedMappingParameters()); |
58
|
|
|
return $this->mappingNormalizer->normalize($mapping, $configuration); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|