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

DocumentMappingView::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping;
6
7
use AmaTeam\ElasticSearch\API\Mapping\DocumentMappingViewInterface;
8
9
class DocumentMappingView implements DocumentMappingViewInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $type;
15
    /**
16
     * @var array
17
     */
18
    private $parameters = [];
19
20
    /**
21
     * @return string
22
     */
23
    public function getType(): ?string
24
    {
25
        return $this->type;
26
    }
27
28
    /**
29
     * @param string $type
30
     * @return $this
31
     */
32
    public function setType(string $type)
33
    {
34
        $this->type = $type;
35
        return $this;
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getParameters(): array
42
    {
43
        return $this->parameters;
44
    }
45
46
    public function setParameter(string $parameter, $value): DocumentMappingView
47
    {
48
        $this->parameters[$parameter] = $value;
49
        return $this;
50
    }
51
52
    /**
53
     * @param array $parameters
54
     * @return $this
55
     */
56
    public function setParameters(array $parameters)
57
    {
58
        $this->parameters = $parameters;
59
        return $this;
60
    }
61
62
    public static function merge(DocumentMappingViewInterface ...$mappings): DocumentMappingView
63
    {
64
        $target = new DocumentMappingView();
65
        $parameters = [];
66
        foreach ($mappings as $mapping) {
67
            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...
68
                $target->setType($mapping->getType());
69
            }
70
            foreach ($mapping->getParameters() as $parameter => $value) {
71
                $parameters[$parameter] = $value;
72
            }
73
        }
74
        $target->setParameters($parameters);
75
        return $target;
76
    }
77
}
78