1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace CCT\Component\ODMElasticsearch\Metadata; |
||
6 | |||
7 | use Metadata\PropertyMetadata as BasePropertyMetadata; |
||
8 | |||
9 | class PropertyMetadata extends BasePropertyMetadata implements PropertyMetadataInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var boolean |
||
13 | */ |
||
14 | protected $isExposed; |
||
15 | |||
16 | /** |
||
17 | * @var string|null |
||
18 | */ |
||
19 | protected $fieldName; |
||
20 | |||
21 | /** |
||
22 | * Type of the property |
||
23 | * |
||
24 | * @var string|null |
||
25 | */ |
||
26 | protected $type; |
||
27 | |||
28 | /** |
||
29 | * The object class name. Only set when type set to object |
||
30 | * |
||
31 | * @var string|null |
||
32 | */ |
||
33 | protected $typeClass; |
||
34 | |||
35 | /** |
||
36 | * Getter function name |
||
37 | * |
||
38 | * @var string | null |
||
39 | */ |
||
40 | protected $getter; |
||
41 | |||
42 | /** |
||
43 | * Setter function name |
||
44 | * |
||
45 | * @var string | null |
||
46 | */ |
||
47 | protected $setter; |
||
48 | |||
49 | /** |
||
50 | * Elasticsearch mapping config for property e.g. |
||
51 | * [ |
||
52 | * 'type' => 'integer', |
||
53 | * 'normalizer' => 'lowercasing' |
||
54 | * ] |
||
55 | * |
||
56 | * @var array | null |
||
57 | */ |
||
58 | protected $mapping; |
||
59 | |||
60 | /** |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function isExposed(): bool |
||
64 | { |
||
65 | return $this->isExposed; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param bool $isExposed |
||
70 | */ |
||
71 | public function setIsExposed(bool $isExposed): void |
||
72 | { |
||
73 | $this->isExposed = $isExposed; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return string|null |
||
78 | */ |
||
79 | 7 | public function getFieldName(): ?string |
|
80 | { |
||
81 | 7 | return $this->fieldName; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string|null $fieldName |
||
86 | */ |
||
87 | public function setFieldName(string $fieldName = null): void |
||
88 | { |
||
89 | $this->fieldName = $fieldName; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return null|string |
||
94 | */ |
||
95 | 14 | public function getType(): ?string |
|
96 | { |
||
97 | 14 | return $this->type; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param null|string $type |
||
102 | */ |
||
103 | 14 | public function setType(?string $type): void |
|
104 | { |
||
105 | 14 | $this->type = $type; |
|
106 | 14 | } |
|
107 | |||
108 | /** |
||
109 | * Set property setter function |
||
110 | * |
||
111 | * @param null $setter |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
112 | */ |
||
113 | public function setSetterAccessor($setter = null): void |
||
114 | { |
||
115 | $this->setter = $setter; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Set property getter function |
||
120 | * |
||
121 | * @param null $getter |
||
0 ignored issues
–
show
|
|||
122 | */ |
||
123 | public function setGetterAccessor($getter = null): void |
||
124 | { |
||
125 | $this->getter = $getter; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Sets default setter based on property name appended with "set" eg setName |
||
130 | * |
||
131 | * @throws \ReflectionException |
||
132 | */ |
||
133 | 3 | public function setDefaultSetterAccessor() |
|
134 | { |
||
135 | 3 | $class = $this->reflection->getDeclaringClass(); |
|
136 | |||
137 | 3 | $methodName = ucfirst($this->name); |
|
138 | |||
139 | 3 | if ($class->hasMethod('set' . $methodName) && $class->getMethod('set' . $methodName)->isPublic()) { |
|
140 | 2 | $this->setter = 'set' . $methodName; |
|
141 | } |
||
142 | 3 | } |
|
143 | |||
144 | /** |
||
145 | * Sets default getter based on property name appended with "get", "has", or "is" eg getName, hasName, isName |
||
146 | */ |
||
147 | 4 | public function setDefaultGetterAccessor() |
|
148 | { |
||
149 | 4 | $class = $this->reflection->getDeclaringClass(); |
|
150 | |||
151 | 4 | $methodName = ucfirst($this->name); |
|
152 | |||
153 | 4 | if ($class->hasMethod('get' . $methodName) && $class->getMethod('get' . $methodName)->isPublic()) { |
|
154 | 2 | $this->getter = 'get' . $methodName; |
|
155 | |||
156 | 2 | return; |
|
157 | } |
||
158 | 2 | if ($class->hasMethod('is' . $methodName) && $class->getMethod('is' . $methodName)->isPublic()) { |
|
159 | 1 | $this->getter = 'is' . $methodName; |
|
160 | |||
161 | 1 | return; |
|
162 | } |
||
163 | 1 | if ($class->hasMethod('has' . $methodName) && $class->getMethod('has' . $methodName)->isPublic()) { |
|
164 | $this->getter = 'has' . $methodName; |
||
165 | |||
166 | return; |
||
167 | } |
||
168 | 1 | } |
|
169 | |||
170 | /** |
||
171 | * @param object $obj |
||
172 | * |
||
173 | * @return mixed |
||
174 | */ |
||
175 | 8 | public function getValue($obj) |
|
176 | { |
||
177 | 8 | if (null === $this->getter) { |
|
178 | 5 | return parent::getValue($obj); |
|
179 | } |
||
180 | |||
181 | 3 | return $obj->{$this->getter}(); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param object $obj |
||
186 | * @param string $value |
||
187 | */ |
||
188 | 4 | public function setValue($obj, $value) |
|
189 | { |
||
190 | 4 | if (null === $this->setter) { |
|
191 | 2 | parent::setValue($obj, $value); |
|
192 | |||
193 | 2 | return; |
|
194 | } |
||
195 | |||
196 | 2 | $obj->{$this->setter}($value); |
|
197 | 2 | } |
|
198 | |||
199 | /** |
||
200 | * @return null|string |
||
201 | */ |
||
202 | 5 | public function getTypeClass(): ?string |
|
203 | { |
||
204 | 5 | return $this->typeClass; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param null|string $typeClass |
||
209 | */ |
||
210 | 5 | public function setTypeClass(?string $typeClass): void |
|
211 | { |
||
212 | 5 | $this->typeClass = $typeClass; |
|
213 | 5 | } |
|
214 | |||
215 | /** |
||
216 | * @return array|null |
||
217 | */ |
||
218 | 3 | public function getMapping(): ?array |
|
219 | { |
||
220 | 3 | return $this->mapping; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param array|null $mapping |
||
225 | */ |
||
226 | public function setMapping(?array $mapping): void |
||
227 | { |
||
228 | $this->mapping = $mapping; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getName(): string |
||
235 | { |
||
236 | return $this->name; |
||
237 | } |
||
238 | } |
||
239 |