Skip to content

Commit 087fbec

Browse files
author
Jeremiah VALERIE
committed
Try to get property before using PropertyAccessor
1 parent b96c2d8 commit 087fbec

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

Resolver/Resolver.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,39 @@ public static function defaultResolveFn($source, $args, ResolveInfo $info)
2727
$fieldName = $info->fieldName;
2828
$property = null;
2929

30-
if (null === self::$accessor) {
31-
self::$accessor = PropertyAccess::createPropertyAccessor();
32-
}
33-
3430
$index = sprintf('[%s]', $fieldName);
3531

36-
if (self::$accessor->isReadable($source, $index)) {
37-
$property = self::$accessor->getValue($source, $index);
38-
} elseif (self::$accessor->isReadable($source, $fieldName)) {
39-
$property = self::$accessor->getValue($source, $fieldName);
32+
if (self::getAccessor()->isReadable($source, $index)) {
33+
$property = self::getAccessor()->getValue($source, $index);
34+
} elseif (is_object($source)) {
35+
$property = self::propertyValueFromObject($source, $fieldName);
4036
}
4137

4238
return $property instanceof \Closure ? $property($source, $args, $info) : $property;
4339
}
40+
41+
private static function propertyValueFromObject($object, $fieldName)
42+
{
43+
$property = null;
44+
45+
// accessor try to access the value using methods
46+
// first before using public property directly
47+
// not what we wont here!
48+
if (isset($object->{$fieldName})) {
49+
$property = $object->{$fieldName};
50+
} elseif (self::getAccessor()->isReadable($object, $fieldName)) {
51+
$property = self::getAccessor()->getValue($object, $fieldName);
52+
}
53+
54+
return $property;
55+
}
56+
57+
private static function getAccessor()
58+
{
59+
if (null === self::$accessor) {
60+
self::$accessor = PropertyAccess::createPropertyAccessor();
61+
}
62+
63+
return self::$accessor;
64+
}
4465
}

Tests/Resolver/ResolverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ResolverTest extends \PHPUnit_Framework_TestCase
2222

2323
private $private_property_with_getter2 = 'IfYouWantMeUseMyGetter2';
2424

25-
public $public = 'public';
25+
public $name = 'public';
2626

2727
public $closureProperty;
2828

@@ -56,7 +56,7 @@ public function resolverProvider()
5656
['privatePropertyWithGetter', $this, $this->privatePropertyWithGetter],
5757
['private_property_with_getter2', $this, $this->private_property_with_getter2],
5858
['not_object_or_array', 'String', null],
59-
['public', $this, $this->public],
59+
['name', $this, $this->name],
6060
];
6161
}
6262

0 commit comments

Comments
 (0)