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

ClassMapping::setClassName()   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\API\Entity\Mapping\PropertyMapping as PropertyDescriptor;
8
use AmaTeam\ElasticSearch\Utility\Classes;
9
10
class ClassMapping implements ClassMappingInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $className;
16
    /**
17
     * @var PropertyDescriptor[]
18
     */
19
    private $properties = [];
20
    /**
21
     * @var ClassMappingView
22
     */
23
    private $defaultView;
24
    /**
25
     * @var ClassMappingView[]
26
     */
27
    private $views = [];
28
29
    /**
30
     * @param string $className
31
     */
32
    public function __construct(string $className = null)
33
    {
34
        if ($className) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $className 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...
35
            $this->setClassName($className);
36
        }
37
        $this->defaultView = new ClassMappingView();
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getClassName(): string
44
    {
45
        return $this->className;
46
    }
47
48
    /**
49
     * @param string $className
50
     * @return $this
51
     */
52
    public function setClassName(string $className)
53
    {
54
        $this->className = Classes::normalizeAbsoluteName($className);
55
        return $this;
56
    }
57
58
    /**
59
     * @return PropertyDescriptor[]
60
     */
61
    public function getProperties(): array
62
    {
63
        return $this->properties;
64
    }
65
66
    /**
67
     * @param PropertyDescriptor[] $properties
68
     * @return $this
69
     */
70
    public function setProperties(array $properties)
71
    {
72
        $this->properties = $properties;
73
        return $this;
74
    }
75
76
    /**
77
     * @return ClassMappingViewInterface
78
     */
79
    public function getDefaultView(): ClassMappingViewInterface
80
    {
81
        return $this->defaultView;
82
    }
83
84
    /**
85
     * @param ClassMappingView $defaultView
86
     * @return $this
87
     */
88
    public function setDefaultView(ClassMappingView $defaultView)
89
    {
90
        $this->defaultView = $defaultView;
91
        return $this;
92
    }
93
94
    /**
95
     * @return ClassMappingView[]
96
     */
97
    public function getViews(): array
98
    {
99
        return $this->views;
100
    }
101
102
    public function getView(string $name): ?ClassMappingViewInterface
103
    {
104
        return $this->views[$name] ?? null;
105
    }
106
107
    public function requestView(string $name): ClassMappingViewInterface
108
    {
109
        if (!isset($this->views[$name])) {
110
            $this->views[$name] = new ClassMappingView();
111
        }
112
        return $this->views[$name];
113
    }
114
115
    /**
116
     * @param string[] ...$names
117
     * @return ClassMappingView[]
118
     */
119
    public function requestViews(string ...$names): array
120
    {
121
        return array_map([$this, 'requestView'], $names);
122
    }
123
124
    /**
125
     * @param ClassMappingView[] $views
126
     * @return $this
127
     */
128
    public function setViews(array $views)
129
    {
130
        $this->views = $views;
131
        return $this;
132
    }
133
134
    public function getProperty(string $name): PropertyMappingInterface
135
    {
136
        return $this->properties[$name] ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->properties[$name] ?? null could return the type null which is incompatible with the type-hinted return AmaTeam\ElasticSearch\AP...ropertyMappingInterface. Consider adding an additional type-check to rule them out.
Loading history...
137
    }
138
139
    public function requestProperty(string $name): PropertyMappingInterface
140
    {
141
        if (!isset($this->properties[$name])) {
142
            $this->properties[$name] = new PropertyMapping($this->className, $name);
143
        }
144
        return $this->properties[$name];
145
    }
146
}
147