From 5f06435e1cb37e3fe4cb66ea8fa16f4f82032fda Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Tue, 12 Apr 2016 16:38:06 +0200 Subject: [PATCH] Fix error when not base64 string is pass to GlobalId::fromGlobalId method --- Relay/Node/GlobalId.php | 18 ++++++++++++------ Tests/Relay/Node/GlobalIdTest.php | 7 +++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Relay/Node/GlobalId.php b/Relay/Node/GlobalId.php index f91b28327..03a096575 100644 --- a/Relay/Node/GlobalId.php +++ b/Relay/Node/GlobalId.php @@ -22,13 +22,19 @@ public static function toGlobalId($type, $id) public static function fromGlobalId($globalId) { - $unBasedGlobalId = base64_decode($globalId); + $unBasedGlobalId = base64_decode($globalId, true); - list($type, $id) = array_merge(explode(static::SEPARATOR, $unBasedGlobalId), [true]); - - return [ - 'type' => $type, - 'id' => $id, + $decodeGlobalId = [ + 'type' => null, + 'id' => null, ]; + + if (!$unBasedGlobalId) { + return $decodeGlobalId; + } + + list($decodeGlobalId['type'], $decodeGlobalId['id']) = array_pad(explode(static::SEPARATOR, $unBasedGlobalId), 2, null); + + return $decodeGlobalId; } } diff --git a/Tests/Relay/Node/GlobalIdTest.php b/Tests/Relay/Node/GlobalIdTest.php index e0dcb5342..52c2ac7bb 100644 --- a/Tests/Relay/Node/GlobalIdTest.php +++ b/Tests/Relay/Node/GlobalIdTest.php @@ -70,4 +70,11 @@ public function testFromGlobalIdWithTypeAndIdEmpty() $this->assertEquals(['type' => null, 'id' => null], $params); } + + public function testFromGlobalIdWithNotBase64Entry() + { + $params = GlobalId::fromGlobalId(1); + + $this->assertEquals(['type' => null, 'id' => null], $params); + } }