From 8b339ea0ac1c91f46e277a5583da8beac6f94673 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 23 Oct 2020 18:29:32 +0300 Subject: [PATCH] allow usage object properties to get/set instance in ObjectManipulator --- src/Manipulator/ObjectManipulator.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Manipulator/ObjectManipulator.php b/src/Manipulator/ObjectManipulator.php index 05289bf7b9d..2ae281f0089 100644 --- a/src/Manipulator/ObjectManipulator.php +++ b/src/Manipulator/ObjectManipulator.php @@ -76,6 +76,18 @@ private static function callGetter(object $object, string $fieldName): object $method = sprintf('get%s', $inflector->classify($fieldName)); if (!(\is_callable([$object, $method]) && method_exists($object, $method))) { + if (property_exists($object, $fieldName)) { + $ref = new \ReflectionProperty($object, $fieldName); + + if ($ref->isPublic()) { + return $object->$fieldName; + } + + throw new \RuntimeException( + sprintf('Property %s::$%s does not accessible.', ClassUtils::getClass($object), $fieldName) + ); + } + /* * NEXT_MAJOR: Use BadMethodCallException instead */ @@ -100,6 +112,20 @@ private static function callSetter(object $instance, object $object, string $map $method = sprintf('set%s', $inflector->classify($mappedBy)); if (!(\is_callable([$instance, $method]) && method_exists($instance, $method))) { + if (property_exists($instance, $mappedBy)) { + $ref = new \ReflectionProperty($instance, $mappedBy); + + if ($ref->isPublic()) { + $instance->$mappedBy = $object; + + return $instance; + } + + throw new \RuntimeException( + sprintf('Property %s::$%s does not accessible.', ClassUtils::getClass($object), $mappedBy) + ); + } + /* * NEXT_MAJOR: Use BadMethodCallException instead */