Passed
Push — feature/initial-implementation ( 0c2c6c...79751f )
by Fike
01:54
created

Renderer::renderPropertyMapping()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 15
nc 20
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping;
6
7
use AmaTeam\ElasticSearch\API\Mapping\DocumentMappingInterface;
8
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface;
9
use AmaTeam\ElasticSearch\API\Mapping\PropertyMappingInterface;
10
use AmaTeam\ElasticSearch\API\Mapping\RendererInterface;
11
12
class Renderer implements RendererInterface
13
{
14
    /**
15
     * @var MappingProvider
16
     */
17
    private $provider;
18
19
    /**
20
     * @param MappingProvider $provider
21
     */
22
    public function __construct(MappingProvider $provider = null)
23
    {
24
        $this->provider = $provider ?? new MappingProvider();
25
    }
26
27
    public function render(DocumentMappingInterface $source, string ...$views): MappingInterface
28
    {
29
        if ($source->getForcedViewName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $source->getForcedViewName() 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...
30
            $views = [$source->getForcedViewName()];
31
        }
32
        return $this->renderDocumentMapping($source, $views);
33
    }
34
35
    private function renderDocumentMapping(DocumentMappingInterface $document, array $viewNames): MappingInterface
36
    {
37
        $views = array_map([$document, 'getView'], $viewNames);
38
        array_unshift($views, $document->getDefaultView());
39
        $view = DocumentMappingView::merge(...array_filter($views));
40
        $mapping = new Mapping();
41
        if ($view->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getType() 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...
42
            $mapping->setType($view->getType());
43
        }
44
        $properties = [];
45
        foreach ($document->getProperties() as $property => $definition) {
46
            $properties[$property] = $this->renderPropertyMapping($definition, $viewNames);
47
        }
48
        $mapping->setProperties($properties);
49
        $mapping->setParameters($view->getParameters());
50
        return $mapping;
51
    }
52
53
    private function renderPropertyMapping(PropertyMappingInterface $property, array $viewNames): MappingInterface
54
    {
55
        $views = array_map([$property, 'getView'], $viewNames);
56
        array_unshift($views, $property->getDefaultView());
57
        $view = PropertyMappingView::merge(...array_filter($views));
58
        $mapping = new Mapping();
59
        if ($view->getTargetClass()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getTargetClass() 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...
60
            $localViewNames = $viewNames;
61
            $source = $this->provider->getMapping($view->getTargetClass());
62
            $localViewNames = $source->getForcedViewName() ? [$source->getForcedViewName()] : $localViewNames;
63
            $localViewNames = $property->getForcedViewName() ? [$property->getForcedViewName()] : $localViewNames;
64
            $mapping = $this->renderDocumentMapping($source, $localViewNames);
65
        }
66
        foreach ($view->getParameters() as $parameter => $value) {
67
            $mapping->setParameter($parameter, $value);
68
        }
69
        if ($view->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getType() 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...
70
            $mapping->setType($view->getType());
71
        }
72
        return $mapping;
73
    }
74
}
75