1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Mapping; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ProviderInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ContextInterface as ConversionContextInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ConverterInterface; |
11
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\ManagerInterface; |
12
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
13
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Normalization\ContextInterface as NormalizationContextInterface; |
14
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\NormalizerInterface; |
15
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Validation\ContextInterface as ValidationContextInterface; |
16
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Validation\ValidationException; |
17
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\ValidatorInterface; |
18
|
|
|
use AmaTeam\ElasticSearch\Mapping\Conversion\Converter; |
19
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
20
|
|
|
|
21
|
|
|
class Manager implements ManagerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ProviderInterface |
25
|
|
|
*/ |
26
|
|
|
private $provider; |
27
|
|
|
/** |
28
|
|
|
* @var ConverterInterface |
29
|
|
|
*/ |
30
|
|
|
private $converter; |
31
|
|
|
/** |
32
|
|
|
* @var ValidatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $validator; |
35
|
|
|
/** |
36
|
|
|
* @var NormalizerInterface |
37
|
|
|
*/ |
38
|
|
|
private $normalizer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ProviderInterface $provider |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ProviderInterface $provider) |
44
|
|
|
{ |
45
|
|
|
$this->provider = $provider; |
46
|
|
|
$this->converter = new Converter($provider); |
47
|
|
|
$this->validator = new Validator(); |
48
|
|
|
$this->normalizer = new Normalizer(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function get(string $name, ConversionContextInterface $context = null): MappingInterface |
52
|
|
|
{ |
53
|
|
|
return $this->convert($this->provider->get($name), $context); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function find(string $name, ConversionContextInterface $context = null): ?MappingInterface |
57
|
|
|
{ |
58
|
|
|
$mapping = $this->provider->find($name); |
59
|
|
|
return $mapping ? $this->convert($mapping, $context) : null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function exists(string $name): bool |
63
|
|
|
{ |
64
|
|
|
return $this->provider->exists($name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function convert(ClassMappingInterface $source, ConversionContextInterface $context = null): MappingInterface |
68
|
|
|
{ |
69
|
|
|
$mapping = $this->converter->convert($source, $context); |
70
|
|
|
$mapping = $this->normalizer->normalize($mapping); |
71
|
|
|
$violations = $this->validator->validate($mapping); |
72
|
|
|
if ($violations->count() > 0) { |
73
|
|
|
$lines = ['Generated mapping failed validation:']; |
74
|
|
|
foreach ($violations as $violation) { |
75
|
|
|
$lines[] = $violation->getPropertyPath() . ': ' . $violation->getMessage(); |
76
|
|
|
} |
77
|
|
|
throw new ValidationException(implode(PHP_EOL, $lines), $violations); |
78
|
|
|
} |
79
|
|
|
return $mapping; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function validate( |
83
|
|
|
MappingInterface $mapping, |
84
|
|
|
ValidationContextInterface $context = null |
85
|
|
|
): ConstraintViolationListInterface { |
86
|
|
|
return $this->validator->validate($mapping, $context); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function normalize( |
90
|
|
|
MappingInterface $mapping, |
91
|
|
|
NormalizationContextInterface $context = null |
92
|
|
|
): MappingInterface { |
93
|
|
|
return $this->normalizer->normalize($mapping, $context); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|