|
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) { |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: