Passed
Push — feature/initial-implementation ( 70fbec...2d1ee1 )
by Fike
02:35
created

Combiner::combine()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 24
nop 2
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Entity\Mapping\Inheritance;
6
7
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMapping;
8
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingInterface;
9
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingView;
10
use AmaTeam\ElasticSearch\API\Entity\Mapping\PropertyMapping;
11
12
class Combiner
13
{
14
    public static function combine(ClassMappingInterface $parent, ClassMappingInterface $child): ClassMappingInterface
15
    {
16
        $target = new ClassMapping();
17
        $target->setEntityName($child->getEntityName());
18
        $defaultView = ClassMappingView::merge($parent->getDefaultView(), $child->getDefaultView());
19
        $target->setDefaultView($defaultView);
20
        foreach ($parent->getViews() as $name => $view) {
21
            $target->setView($name, ClassMappingView::merge($target->requestView($name), $view));
22
        }
23
        foreach ($child->getViews() as $name => $view) {
24
            $target->setView($name, ClassMappingView::merge($target->requestView($name), $view));
25
        }
26
        $ignoredProperties = $child->getIgnoredParentProperties() ?? [];
27
        foreach ($parent->getProperties() as $name => $property) {
28
            if (!in_array($name, $ignoredProperties)) {
29
                $target->setProperty($name, $property);
30
            }
31
        }
32
        foreach ($child->getProperties() as $name => $property) {
33
            $target->setProperty($name, PropertyMapping::merge($target->requestProperty($name), $property));
34
        }
35
        return $target;
36
    }
37
}
38