|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Mapping; |
|
6
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Normalization\Context; |
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Normalization\ContextInterface; |
|
8
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\NormalizerInterface; |
|
9
|
|
|
use AmaTeam\ElasticSearch\API\MappingInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\Infrastructure\Registry; |
|
11
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
|
12
|
|
|
|
|
13
|
|
|
class Normalizer implements NormalizerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public function normalize(MappingInterface $mapping, ContextInterface $context = null): MappingInterface |
|
16
|
|
|
{ |
|
17
|
|
|
$context = $context ?? new Context(); |
|
18
|
|
|
$target = new Mapping(); |
|
19
|
|
|
$type = $context->isRootMapping() ? RootType::ID : $mapping->getType(); |
|
20
|
|
|
if ($type) { |
|
|
|
|
|
|
21
|
|
|
$target->setType($type); |
|
22
|
|
|
} |
|
23
|
|
|
$target->setParameters($this->normalizeParameters($mapping->getParameters(), $context)); |
|
24
|
|
|
$target->setProperties($this->normalizeProperties($mapping->getProperties(), $context)); |
|
25
|
|
|
return $target; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function normalizeProperties(array $properties, ContextInterface $context): array |
|
29
|
|
|
{ |
|
30
|
|
|
$result = []; |
|
31
|
|
|
$innerContext = Context::from($context)->setRootMapping(false); |
|
32
|
|
|
foreach ($properties as $name => $property) { |
|
33
|
|
|
$result[$name] = $this->normalize($property, $innerContext); |
|
34
|
|
|
} |
|
35
|
|
|
return $result; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function normalizeParameters(array $parameters, ContextInterface $context): array |
|
39
|
|
|
{ |
|
40
|
|
|
$result = []; |
|
41
|
|
|
$registry = Registry::getInstance(); |
|
42
|
|
|
foreach ($parameters as $name => $value) { |
|
43
|
|
|
$parameter = $registry->find($name); |
|
44
|
|
|
if ($parameter) { |
|
45
|
|
|
if ($value !== null || $parameter->nullValueAllowed()) { |
|
46
|
|
|
$result[$parameter->getId()] = $value; |
|
47
|
|
|
} |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
if ($context->shouldPreserveUnknownParameters() || in_array($name, $context->getPreservedParameters())) { |
|
51
|
|
|
$result[$name] = $value; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: