|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Entity\AssemblerInterface; |
|
6
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Descriptor\ManagerInterface; |
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Loader\Context; |
|
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Loader\ContextInterface; |
|
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\LoaderInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\API\Entity\NormalizerInterface; |
|
11
|
|
|
use AmaTeam\ElasticSearch\API\Entity\ValidatorInterface; |
|
12
|
|
|
use AmaTeam\ElasticSearch\API\EntityInterface; |
|
13
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Validation\ValidationException; |
|
14
|
|
|
|
|
15
|
|
|
class Loader implements LoaderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ManagerInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $manager; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var AssemblerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $assembler; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ValidatorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $validator; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var NormalizerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $normalizer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ManagerInterface $manager |
|
36
|
|
|
* @param AssemblerInterface|null $assembler |
|
37
|
|
|
* @param ValidatorInterface|null $validator |
|
38
|
|
|
* @param NormalizerInterface|null $normalizer |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
ManagerInterface $manager, |
|
42
|
|
|
AssemblerInterface $assembler = null, |
|
43
|
|
|
ValidatorInterface $validator = null, |
|
44
|
|
|
NormalizerInterface $normalizer = null |
|
45
|
|
|
) { |
|
46
|
|
|
$this->manager = $manager; |
|
47
|
|
|
$this->assembler = $assembler ?? new Assembler($manager); |
|
48
|
|
|
$this->validator = $validator ?? new Validator(); |
|
49
|
|
|
$this->normalizer = $normalizer ?? new Normalizer(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function load(string $name, ContextInterface $context = null): ?EntityInterface |
|
54
|
|
|
{ |
|
55
|
|
|
$descriptor = $this->manager->find($name); |
|
56
|
|
|
if (!$descriptor) { |
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
$context = $context ?? new Context(); |
|
60
|
|
|
$entity = $this->assembler->assemble($descriptor); |
|
61
|
|
|
$entity = $this->normalize($entity, $context); |
|
62
|
|
|
$this->validate($entity, $context); |
|
63
|
|
|
return $entity; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function normalize(EntityInterface $entity, ContextInterface $context): EntityInterface |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$context->shouldNormalize()) { |
|
69
|
|
|
return $entity; |
|
70
|
|
|
} |
|
71
|
|
|
return $this->normalizer->normalize($entity, $context); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function validate(EntityInterface $entity, ContextInterface $context): void |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$context->shouldValidate()) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
$violations = $this->validator->validate($entity, $context); |
|
80
|
|
|
if ($violations->count() === 0) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
$lines = [sprintf('Entity metadata `%s` has failed validation:', $entity->getName())]; |
|
84
|
|
|
foreach ($violations as $violation) { |
|
85
|
|
|
$lines[] = $violation->getPropertyPath() . ': ' . $violation->getMessage(); |
|
86
|
|
|
} |
|
87
|
|
|
throw new ValidationException(implode(PHP_EOL, $lines), $violations); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function exists(string $name): bool |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->manager->exists($name); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.