Skip to content

Commit c97646e

Browse files
committed
Close #209
1 parent d8aeae9 commit c97646e

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/Document/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public function getLinks(): ?array
133133
* @param LinkInterface|null $link
134134
*
135135
* @return self
136+
*
137+
* @SuppressWarnings(PHPMD.ElseExpression)
136138
*/
137139
public function setLink(string $name, ?LinkInterface $link): self
138140
{

src/Encoder/Parser/Parser.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,32 @@ class Parser implements ParserInterface, LoggerAwareInterface
7070
{
7171
use LoggerAwareTrait;
7272

73+
/** @deprecated Use `MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_XXX` instead
74+
* Message code.
75+
*/
76+
const MSG_SCHEME_NOT_REGISTERED = self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH;
77+
7378
/**
7479
* Message code.
7580
*/
76-
const MSG_SCHEME_NOT_REGISTERED = 0;
81+
const MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT = 0;
82+
83+
/**
84+
* Message code.
85+
*/
86+
const MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH = self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT + 1;
7787

7888
/**
7989
* Default messages.
8090
*/
8191
const MESSAGES = [
82-
self::MSG_SCHEME_NOT_REGISTERED => 'Schema is not registered for a resource at path \'%s\'.',
92+
self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT =>
93+
'Getting Schema for a top-level resource of type `%s` failed. ' .
94+
'Please check you have added a Schema for this type.',
95+
96+
self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH =>
97+
'Getting Schema for a resource of type `%s` at path `%s` failed. ' .
98+
'Please check you have added a Schema for this type.',
8399
];
84100

85101
/**
@@ -291,13 +307,21 @@ protected function analyzeData($data): array
291307
* @return SchemaInterface
292308
*
293309
* @SuppressWarnings(PHPMD.StaticAccess)
310+
* @SuppressWarnings(PHPMD.ElseExpression)
294311
*/
295312
private function getSchema($resource, StackFrameReadOnlyInterface $frame): SchemaInterface
296313
{
297314
try {
298315
$schema = $this->container->getSchema($resource);
299316
} catch (InvalidArgumentException $exception) {
300-
$message = _($this->messages[self::MSG_SCHEME_NOT_REGISTERED], $frame->getPath());
317+
$path = $frame->getPath();
318+
$typeName = is_object($resource) === true ? get_class($resource) : gettype($resource);
319+
if ($path === null) {
320+
$message = _($this->messages[static::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT], $typeName);
321+
} else {
322+
$message = _($this->messages[static::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH], $typeName, $path);
323+
}
324+
301325
throw new InvalidArgumentException($message, 0, $exception);
302326
}
303327

tests/Encoder/EncoderTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,29 @@ public function testEncodeWithRelationshipRelatedLink()
858858
/**
859859
* Test encode unrecognized resource (no registered Schema).
860860
*/
861-
public function testEncodeUnrecognizedResource()
861+
public function testEncodeUnrecognizedResourceAtRoot()
862+
{
863+
$author = Author::instance(9, 'Dan', 'Gebhardt');
864+
865+
/** @var InvalidArgumentException $catch */
866+
$catch = null;
867+
try {
868+
Encoder::instance([
869+
Post::class => PostSchema::class,
870+
], $this->encoderOptions)->encodeData($author);
871+
} catch (InvalidArgumentException $exception) {
872+
$catch = $exception;
873+
}
874+
875+
$this->assertNotNull($catch);
876+
$this->assertContains('top-level', $catch->getMessage());
877+
$this->assertNotNull($catch->getPrevious());
878+
}
879+
880+
/**
881+
* Test encode unrecognized resource (no registered Schema).
882+
*/
883+
public function testEncodeUnrecognizedResourceInRelationship()
862884
{
863885
$author = Author::instance(9, 'Dan', 'Gebhardt');
864886
$post = Post::instance(1, 'Title', 'Body', null, [Comment::instance(5, 'First!', $author)]);

0 commit comments

Comments
 (0)