Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Include the node ID in MalformedNodeIdException messages #36

Merged
merged 4 commits into from Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/Fields/NodeIdResolver.php
Expand Up @@ -7,33 +7,38 @@
class NodeIdResolver
{

const INDEX_TYPE = 0;
const INDEX_ID = 1;
private const INDEX_TYPE = 0;
private const INDEX_ID = 1;

/**
* @param string $globalId
*
* @return string
* @throws MalformedNodeIdException
*/
public static function typeFromGlobalId($globalId)
public static function typeFromGlobalId($globalId): string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we type-hint for the parameter(s) also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

{
return self::fromGlobalId($globalId)[self::INDEX_TYPE];
}

/**
* @param string $globalId
*
* @return string
* @throws MalformedNodeIdException
*/
public static function idFromGlobalId($globalId)
public static function idFromGlobalId($globalId): string
{
return self::fromGlobalId($globalId)[self::INDEX_ID];
}

/**
* @param string $typeName
* @param string $id
*
* @return string
*/
public static function toGlobalId($typeName, $id)
public static function toGlobalId($typeName, $id): string
{
return base64_encode(implode(':', [$typeName, $id]));
}
Expand All @@ -44,12 +49,12 @@ public static function toGlobalId($typeName, $id)
* @return array
* @throws MalformedNodeIdException
*/
protected static function fromGlobalId($globalId)
protected static function fromGlobalId($globalId): array
{
$decodedGlobalId = base64_decode($globalId);

if (strpos($decodedGlobalId, ':') === false) {
throw new MalformedNodeIdException('Node ID is malformed.');
throw new MalformedNodeIdException(sprintf('Node ID "%s" is malformed.', $globalId));
}

return explode(':', $decodedGlobalId);
Expand Down
1 change: 1 addition & 0 deletions tests/Fields/NodeIdResolverTest.php
Expand Up @@ -22,6 +22,7 @@ public function testIdFromGlobalId()

/**
* @expectedException \Digia\Lumen\GraphQL\Exceptions\MalformedNodeIdException
* @expectedExceptionMessage Node ID "Test1" is malformed.
*/
public function testIdFromGlobalIdWithMalformedId()
{
Expand Down