Passed
Push — feature/initial-implementation ( fb0c51...553b70 )
by Fike
02:25
created

ClassMappingView   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 3 1
A getType() 0 3 1
A setType() 0 4 1
A setParameter() 0 4 1
A setParameters() 0 4 1
A from() 0 3 1
A merge() 0 14 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
    /**
19
     * @return string
20
     */
21
    public function getType(): ?string
22
    {
23
        return $this->type;
24
    }
25
26
    /**
27
     * @param string $type
28
     * @return $this
29
     */
30
    public function setType(string $type)
31
    {
32
        $this->type = $type;
33
        return $this;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getParameters(): array
40
    {
41
        return $this->parameters;
42
    }
43
44
    public function setParameter(string $parameter, $value): ClassMappingViewInterface
45
    {
46
        $this->parameters[$parameter] = $value;
47
        return $this;
48
    }
49
50
    /**
51
     * @param array $parameters
52
     * @return $this
53
     */
54
    public function setParameters(array $parameters)
55
    {
56
        $this->parameters = $parameters;
57
        return $this;
58
    }
59
60
    public static function merge(ClassMappingViewInterface ...$mappings): ClassMappingView
61
    {
62
        $target = new ClassMappingView();
63
        $parameters = [];
64
        foreach ($mappings as $mapping) {
65
            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...
66
                $target->setType($mapping->getType());
67
            }
68
            foreach ($mapping->getParameters() as $parameter => $value) {
69
                $parameters[$parameter] = $value;
70
            }
71
        }
72
        $target->setParameters($parameters);
73
        return $target;
74
    }
75
76
    public static function from(ClassMappingViewInterface $mapping): ClassMappingView
77
    {
78
        return static::merge($mapping);
79
    }
80
}
81