From d642463410610c49c8f43a1833d6321ed721d316 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 14 Jul 2016 08:28:46 -0400 Subject: [PATCH 1/3] add public property getter --- src/Type/TypeService.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Type/TypeService.php b/src/Type/TypeService.php index be85a13e..ee87b717 100644 --- a/src/Type/TypeService.php +++ b/src/Type/TypeService.php @@ -122,6 +122,9 @@ public static function isInputObjectType($type) public static function getPropertyValue($data, $path) { if (is_object($data)) { + if (isset($data->$path)) { + return $data->$path; + } $getter = $path; if (substr($path, 0, 2) != 'is') { $getter = 'get' . self::classify($path); From 79442de575db36bb22b2329720eee112d2af000e Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 14 Jul 2016 08:59:36 -0400 Subject: [PATCH 2/3] add test --- Tests/Library/Utilities/TypeUtilitiesTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/Library/Utilities/TypeUtilitiesTest.php b/Tests/Library/Utilities/TypeUtilitiesTest.php index 5200f064..08b01ea1 100644 --- a/Tests/Library/Utilities/TypeUtilitiesTest.php +++ b/Tests/Library/Utilities/TypeUtilitiesTest.php @@ -14,6 +14,7 @@ use Youshido\GraphQL\Type\TypeService; use Youshido\Tests\DataProvider\TestInterfaceType; use Youshido\Tests\DataProvider\TestObjectType; +use Youshido\Tests\Library\Type\ObjectTypeTest; class TypeUtilitiesTest extends \PHPUnit_Framework_TestCase { @@ -55,4 +56,11 @@ public function testIsAbstractType() $this->assertFalse(TypeService::isAbstractType(new StringType())); $this->assertFalse(TypeService::isAbstractType('invalid type')); } + + public function testGetPropertyValue() { + $arrayData = (new TestObjectType())->getData(); + + $this->assertEquals('John', TypeService::getPropertyValue($arrayData, 'name')); + $this->assertEquals('John', TypeService::getPropertyValue((object) $arrayData, 'name')); + } } From 5241e24d2f6cb8cef1f138cdb111d388371442ed Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 14 Jul 2016 10:17:52 -0400 Subject: [PATCH 3/3] prefer getter over public property, if available --- src/Type/TypeService.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Type/TypeService.php b/src/Type/TypeService.php index ee87b717..c3f45954 100644 --- a/src/Type/TypeService.php +++ b/src/Type/TypeService.php @@ -122,15 +122,12 @@ public static function isInputObjectType($type) public static function getPropertyValue($data, $path) { if (is_object($data)) { - if (isset($data->$path)) { - return $data->$path; - } $getter = $path; if (substr($path, 0, 2) != 'is') { $getter = 'get' . self::classify($path); } - return is_callable([$data, $getter]) ? $data->$getter() : null; + return is_callable([$data, $getter]) ? $data->$getter() : (isset($data->$path) ? $data->$path : null); } elseif (is_array($data)) { return array_key_exists($path, $data) ? $data[$path] : null; }