Skip to content

Commit

Permalink
allow usage object properties to get/set instance in ObjectManipulator
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Oct 23, 2020
1 parent 09468b9 commit 8b339ea
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Manipulator/ObjectManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down

0 comments on commit 8b339ea

Please sign in to comment.