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
|
|
|
/** |
11
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
12
|
|
|
*/ |
13
|
|
|
class ClassMapping implements ClassMappingInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $entityName; |
19
|
|
|
/** |
20
|
|
|
* @var PropertyDescriptor[] |
21
|
|
|
*/ |
22
|
|
|
private $properties = []; |
23
|
|
|
/** |
24
|
|
|
* @var ClassMappingView |
25
|
|
|
*/ |
26
|
|
|
private $defaultView; |
27
|
|
|
/** |
28
|
|
|
* @var ClassMappingView[] |
29
|
|
|
*/ |
30
|
|
|
private $views = []; |
31
|
|
|
/** |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private $ignoredParentProperties; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $className |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $className = null) |
40
|
|
|
{ |
41
|
|
|
if ($className) { |
|
|
|
|
42
|
|
|
$this->setEntityName($className); |
43
|
|
|
} |
44
|
|
|
$this->defaultView = new ClassMappingView(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getEntityName(): string |
51
|
|
|
{ |
52
|
|
|
return $this->entityName; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $entityName |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function setEntityName(string $entityName) |
60
|
|
|
{ |
61
|
|
|
$this->entityName = Classes::normalizeAbsoluteName($entityName); |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return PropertyDescriptor[] |
67
|
|
|
*/ |
68
|
|
|
public function getProperties(): array |
69
|
|
|
{ |
70
|
|
|
return $this->properties; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param PropertyDescriptor[] $properties |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setProperties(array $properties) |
78
|
|
|
{ |
79
|
|
|
$this->properties = $properties; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getProperty(string $name): PropertyMappingInterface |
84
|
|
|
{ |
85
|
|
|
return $this->properties[$name] ?? null; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function requestProperty(string $name): PropertyMappingInterface |
89
|
|
|
{ |
90
|
|
|
if (!isset($this->properties[$name])) { |
91
|
|
|
$this->properties[$name] = new PropertyMapping($this->entityName, $name); |
92
|
|
|
} |
93
|
|
|
return $this->properties[$name]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setProperty(string $name, PropertyMappingInterface $property): ClassMapping |
97
|
|
|
{ |
98
|
|
|
$this->properties[$name] = $property; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return ClassMappingViewInterface |
104
|
|
|
*/ |
105
|
|
|
public function getDefaultView(): ClassMappingViewInterface |
106
|
|
|
{ |
107
|
|
|
return $this->defaultView; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ClassMappingView $defaultView |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setDefaultView(ClassMappingView $defaultView) |
115
|
|
|
{ |
116
|
|
|
$this->defaultView = $defaultView; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return ClassMappingView[] |
122
|
|
|
*/ |
123
|
|
|
public function getViews(): array |
124
|
|
|
{ |
125
|
|
|
return $this->views; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getView(string $name): ?ClassMappingViewInterface |
129
|
|
|
{ |
130
|
|
|
return $this->views[$name] ?? null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function requestView(string $name): ClassMappingViewInterface |
134
|
|
|
{ |
135
|
|
|
if (!isset($this->views[$name])) { |
136
|
|
|
$this->views[$name] = new ClassMappingView(); |
137
|
|
|
} |
138
|
|
|
return $this->views[$name]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string[] ...$names |
143
|
|
|
* @return ClassMappingView[] |
144
|
|
|
*/ |
145
|
|
|
public function requestViews(string ...$names): array |
146
|
|
|
{ |
147
|
|
|
return array_map([$this, 'requestView'], $names); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param ClassMappingView[] $views |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setViews(array $views) |
155
|
|
|
{ |
156
|
|
|
$this->views = $views; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setView(string $name, ClassMappingViewInterface $view): ClassMapping |
161
|
|
|
{ |
162
|
|
|
$this->views[$name] = $view; |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string[] |
168
|
|
|
*/ |
169
|
|
|
public function getIgnoredParentProperties(): ?array |
170
|
|
|
{ |
171
|
|
|
return $this->ignoredParentProperties; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string[] $ignoredParentProperties |
176
|
|
|
* @return ClassMappingInterface |
177
|
|
|
*/ |
178
|
|
|
public function setIgnoredParentProperties(string ...$ignoredParentProperties): ClassMappingInterface |
179
|
|
|
{ |
180
|
|
|
$this->ignoredParentProperties = $ignoredParentProperties; |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public static function merge(ClassMappingInterface ...$sources) |
185
|
|
|
{ |
186
|
|
|
$target = new ClassMapping(); |
187
|
|
|
foreach ($sources as $source) { |
188
|
|
|
$target->setEntityName($source->getEntityName()); |
189
|
|
|
if ($source->getIgnoredParentProperties() !== null) { |
190
|
|
|
$target->setIgnoredParentProperties(...$source->getIgnoredParentProperties()); |
191
|
|
|
} |
192
|
|
|
$defaultView = ClassMappingView::merge($target->getDefaultView(), $source->getDefaultView()); |
193
|
|
|
$target->setDefaultView($defaultView); |
194
|
|
|
foreach ($source->getViews() as $name => $view) { |
195
|
|
|
$target->setView($name, ClassMappingView::merge($target->requestView($name), $view)); |
196
|
|
|
} |
197
|
|
|
foreach ($source->getProperties() as $name => $property) { |
198
|
|
|
$target->setProperty($name, PropertyMapping::merge($target->requestProperty($name), $property)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: