Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

Normalizer::normalizeParameters()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 8.2222
cc 7
eloc 11
nc 5
nop 2
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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