Skip to content

Commit fc118b3

Browse files
committed
Support public property hooks.
1 parent 15420de commit fc118b3

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/DoctrineEntityNormalizer.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ protected function isAllowedAttribute(
204204
*/
205205
private function hasGetter(ReflectionClass $reflectionClass, string $attribute): bool
206206
{
207+
if ($reflectionClass->hasProperty($attribute) && $reflectionClass->getProperty($attribute)->isPublic()) {
208+
return true;
209+
}
210+
207211
// Default to "getStatus", "getConfig", etc...
208212
$getterMethod = $this->getMethodName($attribute, 'get');
209213
if ($reflectionClass->hasMethod($getterMethod)) {
@@ -276,6 +280,14 @@ private function supportsDeepNormalization(ReflectionClass $reflectionClass, str
276280

277281
private function getProperty(object $entity, string $key): mixed
278282
{
283+
// Public item hook.
284+
if (property_exists($entity, $key)) {
285+
$reflProp = new ReflectionProperty($entity, $key);
286+
if ($reflProp->isPublic()) {
287+
return $reflProp->getValue($entity);
288+
}
289+
}
290+
279291
// Default to "getStatus", "getConfig", etc...
280292
$getterMethod = $this->getMethodName($key, 'get');
281293
if (method_exists($entity, $getterMethod)) {
@@ -380,12 +392,19 @@ private function setProperty(
380392
string $attribute,
381393
mixed $value
382394
): void {
383-
$methodName = $this->getMethodName($attribute, 'set');
384-
if (!method_exists($entity, $methodName)) {
385-
return;
395+
// Public item hook.
396+
if (property_exists($entity, $attribute)) {
397+
$reflProp = new ReflectionProperty($entity, $attribute);
398+
if ($reflProp->isPublic()) {
399+
$reflProp->setValue($entity, $value);
400+
return;
401+
}
386402
}
387403

388-
$entity->$methodName($value);
404+
$methodName = $this->getMethodName($attribute, 'set');
405+
if (method_exists($entity, $methodName)) {
406+
$entity->$methodName($value);
407+
}
389408
}
390409

391410
private function isEntity(mixed $class): bool

0 commit comments

Comments
 (0)