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

PropertyMapping::setForcedViewNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\API\Entity\Mapping;
6
7
use AmaTeam\ElasticSearch\Utility\Classes;
8
9
class PropertyMapping implements PropertyMappingInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $parent;
15
    /**
16
     * @var string
17
     */
18
    private $property;
19
    /**
20
     * @var PropertyMappingView[]
21
     */
22
    private $views = [];
23
    /**
24
     * @var PropertyMappingView
25
     */
26
    private $defaultView;
27
    /**
28
     * @var string[]|null
29
     */
30
    private $forcedViewNames;
31
    /**
32
     * @var bool
33
     */
34
    private $appendForcedViews = true;
35
36
    public function __construct(string $parent = null, string $property = null)
37
    {
38
        if ($parent) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parent 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...
39
            $this->setParent($parent);
40
        }
41
        $this->property = $property;
42
        $this->defaultView = new PropertyMappingView();
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getParent(): string
49
    {
50
        return $this->parent;
51
    }
52
53
    /**
54
     * @param string $parent
55
     * @return $this
56
     */
57
    public function setParent(string $parent)
58
    {
59
        $this->parent = Classes::normalizeAbsoluteName($parent);
60
        return $this;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getProperty(): string
67
    {
68
        return $this->property;
69
    }
70
71
    /**
72
     * @param string $property
73
     * @return $this
74
     */
75
    public function setProperty(string $property)
76
    {
77
        $this->property = $property;
78
        return $this;
79
    }
80
81
    /**
82
     * @return PropertyMappingView[]
83
     */
84
    public function getViews(): array
85
    {
86
        return $this->views;
87
    }
88
89
    /**
90
     * @param PropertyMappingView[] $views
91
     * @return $this
92
     */
93
    public function setViews(array $views)
94
    {
95
        $this->views = $views;
96
        return $this;
97
    }
98
99
    public function getView(string $name): ?PropertyMappingViewInterface
100
    {
101
        return $this->views[$name] ?? null;
102
    }
103
104
    public function requestView(string $name): PropertyMappingViewInterface
105
    {
106
        if (!isset($this->views[$name])) {
107
            $this->views[$name] = new PropertyMappingView();
108
        }
109
        return $this->views[$name];
110
    }
111
112
    /**
113
     * @param string[] ...$views
114
     * @return PropertyMappingViewInterface[]
115
     */
116
    public function requestViews(string ...$views): array
117
    {
118
        return array_map([$this, 'requestView'], $views);
119
    }
120
121
    public function setView(string $name, PropertyMappingView $view): PropertyMapping
122
    {
123
        $this->views[$name] = $view;
124
        return $this;
125
    }
126
127
    public function getDefaultView(): PropertyMappingViewInterface
128
    {
129
        return $this->defaultView;
130
    }
131
132
    /**
133
     * @param PropertyMappingView $defaultView
134
     * @return $this
135
     */
136
    public function setDefaultView(PropertyMappingView $defaultView)
137
    {
138
        $this->defaultView = $defaultView;
139
        return $this;
140
    }
141
142
    /**
143
     * @return null|string[]
144
     */
145
    public function getForcedViewNames(): ?array
146
    {
147
        return $this->forcedViewNames;
148
    }
149
150
    /**
151
     * @param string[] $forcedViewNames
152
     * @return $this
153
     */
154
    public function setForcedViewNames(array $forcedViewNames)
155
    {
156
        $this->forcedViewNames = $forcedViewNames;
157
        return $this;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function shouldAppendForcedViews(): bool
164
    {
165
        return $this->appendForcedViews;
166
    }
167
168
    /**
169
     * @param bool $appendForcedViews
170
     * @return $this
171
     */
172
    public function setAppendForcedViews(bool $appendForcedViews)
173
    {
174
        $this->appendForcedViews = $appendForcedViews;
175
        return $this;
176
    }
177
178
    public static function merge(PropertyMappingInterface ...$sources): PropertyMapping
179
    {
180
        $target = new PropertyMapping();
181
        foreach ($sources as $source) {
182
            $target->setParent($source->getParent());
183
            $target->setProperty($source->getProperty());
184
            if ($source->getForcedViewNames() !== null) {
185
                $target->setForcedViewNames($source->getForcedViewNames());
186
            }
187
            $target->setAppendForcedViews($source->shouldAppendForcedViews());
188
            $defaultView = PropertyMappingView::merge($target->getDefaultView(), $source->getDefaultView());
189
            $target->setDefaultView($defaultView);
190
            foreach ($source->getViews() as $name => $view) {
191
                $target->setView($name, PropertyMappingView::merge($target->requestView($name), $view));
192
            }
193
        }
194
        return $target;
195
    }
196
}
197