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

ClassMappingView   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setIgnoredProperties() 0 4 1
A getParameters() 0 3 1
A getType() 0 3 1
A addIgnoredProperty() 0 4 1
A getIgnoredProperties() 0 3 1
A hasIgnoredProperty() 0 3 1
A setType() 0 4 1
A setParameter() 0 4 1
A setParameters() 0 4 1
A removeIgnoredProperty() 0 4 1
A merge() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\API\Entity\Mapping;
6
7
class ClassMappingView implements ClassMappingViewInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $type;
13
    /**
14
     * @var array
15
     */
16
    private $parameters = [];
17
    /**
18
     * @var string[]
19
     */
20
    private $ignoredProperties = [];
21
22
    /**
23
     * @return string
24
     */
25
    public function getType(): ?string
26
    {
27
        return $this->type;
28
    }
29
30
    /**
31
     * @param string $type
32
     * @return $this
33
     */
34
    public function setType(string $type)
35
    {
36
        $this->type = $type;
37
        return $this;
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getParameters(): array
44
    {
45
        return $this->parameters;
46
    }
47
48
    public function setParameter(string $parameter, $value): ClassMappingViewInterface
49
    {
50
        $this->parameters[$parameter] = $value;
51
        return $this;
52
    }
53
54
    /**
55
     * @param array $parameters
56
     * @return $this
57
     */
58
    public function setParameters(array $parameters)
59
    {
60
        $this->parameters = $parameters;
61
        return $this;
62
    }
63
64
    /**
65
     * @return string[]
66
     */
67
    public function getIgnoredProperties(): array
68
    {
69
        return $this->ignoredProperties;
70
    }
71
72
    /**
73
     * @param string[] $ignoredProperties
74
     * @return $this
75
     */
76
    public function setIgnoredProperties(array $ignoredProperties)
77
    {
78
        $this->ignoredProperties = $ignoredProperties;
79
        return $this;
80
    }
81
82
    public function addIgnoredProperty(string $property): ClassMappingViewInterface
83
    {
84
        $this->ignoredProperties[$property] = $property;
85
        return $this;
86
    }
87
88
    public function removeIgnoredProperty(string $property): ClassMappingView
89
    {
90
        unset($this->ignoredProperties[$property]);
91
        return $this;
92
    }
93
94
    public function hasIgnoredProperty(string $property): bool
95
    {
96
        return isset($this->ignoredProperties[$property]);
97
    }
98
99
    public static function merge(ClassMappingViewInterface ...$mappings): ClassMappingView
100
    {
101
        $target = new ClassMappingView();
102
        $parameters = [];
103
        foreach ($mappings as $mapping) {
104
            if ($mapping->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mapping->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...
105
                $target->setType($mapping->getType());
106
            }
107
            foreach ($mapping->getParameters() as $parameter => $value) {
108
                $parameters[$parameter] = $value;
109
            }
110
            $target->setIgnoredProperties($mapping->getIgnoredProperties());
111
        }
112
        $target->setParameters($parameters);
113
        return $target;
114
    }
115
}
116