|
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\ConverterInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
|
11
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\NormalizerInterface; |
|
12
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Validation\ValidationException; |
|
13
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\ValidatorInterface; |
|
14
|
|
|
use AmaTeam\ElasticSearch\Mapping\Conversion\Converter; |
|
15
|
|
|
|
|
16
|
|
|
class Manager |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ProviderInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $provider; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ConverterInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $converter; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ValidatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $validator; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var NormalizerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $normalizer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ProviderInterface $provider |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ProviderInterface $provider) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->provider = $provider; |
|
41
|
|
|
$this->converter = new Converter($provider); |
|
42
|
|
|
$this->validator = new Validator(); |
|
43
|
|
|
$this->normalizer = new Normalizer(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function get(string $name): MappingInterface |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->convert($this->provider->get($name)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function find(string $name): ?MappingInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$mapping = $this->provider->find($name); |
|
54
|
|
|
return $mapping ? $this->convert($mapping) : null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function exists(string $name): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->provider->exists($name); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function convert(ClassMappingInterface $source): MappingInterface |
|
63
|
|
|
{ |
|
64
|
|
|
$mapping = $this->converter->convert($source); |
|
65
|
|
|
$mapping = $this->normalizer->normalize($mapping); |
|
66
|
|
|
$violations = $this->validator->validate($mapping); |
|
67
|
|
|
if ($violations->count() > 0) { |
|
68
|
|
|
$lines = ['Generated mapping failed validation:']; |
|
69
|
|
|
foreach ($violations as $violation) { |
|
70
|
|
|
$lines[] = $violation->getPropertyPath() . ': ' . $violation->getMessage(); |
|
71
|
|
|
} |
|
72
|
|
|
throw new ValidationException(implode(PHP_EOL, $lines), $violations); |
|
73
|
|
|
} |
|
74
|
|
|
return $mapping; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|