From e33ad29940f58a2e729b9a55bc79a7b9f1389c29 Mon Sep 17 00:00:00 2001 From: EK Date: Thu, 25 Jun 2015 19:35:35 +0300 Subject: [PATCH 1/4] Closes #32 --- src/Contracts/Document/DocumentInterface.php | 22 +++++---- src/Document/Document.php | 28 ++++++----- src/Document/Presenters/ElementPresenter.php | 26 +++++----- tests/Encoder/EncoderTest.php | 50 ++++++++++++++++++++ 4 files changed, 95 insertions(+), 31 deletions(-) diff --git a/src/Contracts/Document/DocumentInterface.php b/src/Contracts/Document/DocumentInterface.php index 64ef7643..e3f07eb1 100644 --- a/src/Contracts/Document/DocumentInterface.php +++ b/src/Contracts/Document/DocumentInterface.php @@ -143,21 +143,27 @@ public function addRelationshipToData( * Add an empty relationship to resource in 'data' section. * * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $current + * @param RelationshipObjectInterface $relationship * * @return void */ - public function addEmptyRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current); + public function addEmptyRelationshipToData( + ResourceObjectInterface $parent, + RelationshipObjectInterface $relationship + ); /** * Add a null relationship to resource in 'data' section. * * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $current + * @param RelationshipObjectInterface $relationship * * @return void */ - public function addNullRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current); + public function addNullRelationshipToData( + ResourceObjectInterface $parent, + RelationshipObjectInterface $relationship + ); /** * Add resource to 'included' section. @@ -187,26 +193,26 @@ public function addRelationshipToIncluded( * Add an empty relationship to resource in 'included' section. * * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $current + * @param RelationshipObjectInterface $relationship * * @return void */ public function addEmptyRelationshipToIncluded( ResourceObjectInterface $parent, - RelationshipObjectInterface $current + RelationshipObjectInterface $relationship ); /** * Add a null relationship to resource in 'included' section. * * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $current + * @param RelationshipObjectInterface $relationship * * @return void */ public function addNullRelationshipToIncluded( ResourceObjectInterface $parent, - RelationshipObjectInterface $current + RelationshipObjectInterface $relationship ); /** diff --git a/src/Document/Document.php b/src/Document/Document.php index ea67f24c..32f94358 100644 --- a/src/Document/Document.php +++ b/src/Document/Document.php @@ -192,17 +192,21 @@ public function addRelationshipToIncluded( /** * @inheritdoc */ - public function addEmptyRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current) - { - $this->presenter->setRelationshipTo($this->bufferForData, $parent, $current, []); + public function addEmptyRelationshipToData( + ResourceObjectInterface $parent, + RelationshipObjectInterface $relationship + ) { + $this->presenter->setRelationshipTo($this->bufferForData, $parent, $relationship, []); } /** * @inheritdoc */ - public function addNullRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current) - { - $this->presenter->setRelationshipTo($this->bufferForData, $parent, $current, null); + public function addNullRelationshipToData( + ResourceObjectInterface $parent, + RelationshipObjectInterface $relationship + ) { + $this->presenter->setRelationshipTo($this->bufferForData, $parent, $relationship, null); } /** @@ -210,17 +214,19 @@ public function addNullRelationshipToData(ResourceObjectInterface $parent, Relat */ public function addEmptyRelationshipToIncluded( ResourceObjectInterface $parent, - RelationshipObjectInterface $current + RelationshipObjectInterface $relationship ) { - $this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $current, []); + $this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $relationship, []); } /** * @inheritdoc */ - public function addNullRelationshipToIncluded(ResourceObjectInterface $parent, RelationshipObjectInterface $current) - { - $this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $current, null); + public function addNullRelationshipToIncluded( + ResourceObjectInterface $parent, + RelationshipObjectInterface $relationship + ) { + $this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $relationship, null); } /** diff --git a/src/Document/Presenters/ElementPresenter.php b/src/Document/Presenters/ElementPresenter.php index d6bfcb43..89b0428d 100644 --- a/src/Document/Presenters/ElementPresenter.php +++ b/src/Document/Presenters/ElementPresenter.php @@ -44,34 +44,34 @@ public function __construct(Document $document) /** * @param array $target * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $current - * @param mixed $url + * @param RelationshipObjectInterface $relationship + * @param mixed $value * * @return void */ public function setRelationshipTo( array &$target, ResourceObjectInterface $parent, - RelationshipObjectInterface $current, - $url + RelationshipObjectInterface $relationship, + $value ) { $parentId = $parent->getId(); $parentType = $parent->getType(); - $name = $current->getName(); + $name = $relationship->getName(); $parentExists = isset($target[$parentType][$parentId]); assert('$parentExists === true'); assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false'); if ($parentExists === true) { - $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $url; + $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $value; } } /** * @param array $target * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $relationship + * @param RelationshipObjectInterface $relation * @param ResourceObjectInterface $resource * * @return void @@ -79,7 +79,7 @@ public function setRelationshipTo( public function addRelationshipTo( array &$target, ResourceObjectInterface $parent, - RelationshipObjectInterface $relationship, + RelationshipObjectInterface $relation, ResourceObjectInterface $resource ) { $parentId = $parent->getId(); @@ -88,16 +88,18 @@ public function addRelationshipTo( // parent might be already added to included to it won't be in 'target' buffer if ($parentExists === true) { - $name = $relationship->getName(); + $name = $relation->getName(); $alreadyGotData = isset($target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name]); if ($alreadyGotData === false) { // ... add the first one $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = - $this->getRelationRepresentation($parent, $relationship, $resource); + $this->getRelationRepresentation($parent, $relation, $resource); } else { // ... or add another relation - $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS] - [$name][Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); + if ($relation->isShowData() === true) { + $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS] + [$name][Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); + } } } } diff --git a/tests/Encoder/EncoderTest.php b/tests/Encoder/EncoderTest.php index 98ca69a6..99587e0f 100644 --- a/tests/Encoder/EncoderTest.php +++ b/tests/Encoder/EncoderTest.php @@ -479,6 +479,56 @@ public function testGetEncoderOptions() $this->assertSame($this->encoderOptions, $endoder->getEncoderOptions()); } + /** + * Test add links to empty relationship. + */ + public function testAddLinksToEmptyRelationship() + { + $actual = Encoder::instance([ + Author::class => AuthorSchema::class, + Comment::class => CommentSchema::class, + Post::class => function ($factory, $container) { + $schema = new PostSchema($factory, $container); + //$schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::DATA, null); + $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::SHOW_DATA, false); + $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::SHOW_RELATED, true); + $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::LINKS, ['foo' => new Link('/your/link', null, true)]); + //$schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::DATA, []); + $schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::SHOW_DATA, false); + $schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::LINKS, ['boo' => new Link('another/link')]); + return $schema; + }, + ], $this->encoderOptions)->encode($this->getStandardPost()); + + $expected = <<assertEquals($expected, $actual); + } + /** * @return Post */ From ef4cbfae91c1cc6438f3821071dd005f5b47463d Mon Sep 17 00:00:00 2001 From: EK Date: Thu, 25 Jun 2015 20:04:20 +0300 Subject: [PATCH 2/4] refactor --- src/Document/Presenters/ElementPresenter.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Document/Presenters/ElementPresenter.php b/src/Document/Presenters/ElementPresenter.php index 89b0428d..9112dac5 100644 --- a/src/Document/Presenters/ElementPresenter.php +++ b/src/Document/Presenters/ElementPresenter.php @@ -92,8 +92,14 @@ public function addRelationshipTo( $alreadyGotData = isset($target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name]); if ($alreadyGotData === false) { // ... add the first one - $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = - $this->getRelationRepresentation($parent, $relation, $resource); + $representation = []; + if ($relation->isShowData() === true) { + $representation[Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); + } + + $representation += $this->getRelationRepresentation($parent, $relation, $resource); + + $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $representation; } else { // ... or add another relation if ($relation->isShowData() === true) { @@ -237,14 +243,12 @@ private function getLinkRepresentation($prefix = null, LinkInterface $link = nul /** * @param ResourceObjectInterface $parent * @param RelationshipObjectInterface $relation - * @param ResourceObjectInterface $resource * * @return array */ private function getRelationRepresentation( ResourceObjectInterface $parent, - RelationshipObjectInterface $relation, - ResourceObjectInterface $resource + RelationshipObjectInterface $relation ) { assert( '$relation->getName() !== \''.Document::KEYWORD_SELF.'\'', @@ -253,10 +257,6 @@ private function getRelationRepresentation( $representation = []; - if ($relation->isShowData() === true) { - $representation[Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); - } - if (($meta = $relation->getMeta()) !== null) { $representation[Document::KEYWORD_META] = $meta; } From be0609126787bdbcbe42d23f787f9ee4f007eab7 Mon Sep 17 00:00:00 2001 From: EK Date: Thu, 25 Jun 2015 20:33:23 +0300 Subject: [PATCH 3/4] Fix encoding for empty data --- src/Document/Presenters/ElementPresenter.php | 18 +++++++++---- tests/Document/DocumentTest.php | 27 +++++++++++++++----- tests/Encoder/EncodeIncludedObjectsTest.php | 6 ++--- tests/Encoder/EncoderTest.php | 8 +++--- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Document/Presenters/ElementPresenter.php b/src/Document/Presenters/ElementPresenter.php index 9112dac5..276c6afa 100644 --- a/src/Document/Presenters/ElementPresenter.php +++ b/src/Document/Presenters/ElementPresenter.php @@ -44,7 +44,7 @@ public function __construct(Document $document) /** * @param array $target * @param ResourceObjectInterface $parent - * @param RelationshipObjectInterface $relationship + * @param RelationshipObjectInterface $relation * @param mixed $value * * @return void @@ -52,19 +52,27 @@ public function __construct(Document $document) public function setRelationshipTo( array &$target, ResourceObjectInterface $parent, - RelationshipObjectInterface $relationship, + RelationshipObjectInterface $relation, $value ) { $parentId = $parent->getId(); $parentType = $parent->getType(); - $name = $relationship->getName(); + $name = $relation->getName(); $parentExists = isset($target[$parentType][$parentId]); assert('$parentExists === true'); assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false'); if ($parentExists === true) { - $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $value; + $representation = []; + + if ($relation->isShowData() === true) { + $representation[Document::KEYWORD_LINKAGE_DATA][] = $value; + } + + $representation += $this->getRelationRepresentation($parent, $relation); + + $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $representation; } } @@ -97,7 +105,7 @@ public function addRelationshipTo( $representation[Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); } - $representation += $this->getRelationRepresentation($parent, $relation, $resource); + $representation += $this->getRelationRepresentation($parent, $relation); $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $representation; } else { diff --git a/tests/Document/DocumentTest.php b/tests/Document/DocumentTest.php index e7025cc2..e74fba4e 100644 --- a/tests/Document/DocumentTest.php +++ b/tests/Document/DocumentTest.php @@ -477,7 +477,7 @@ public function testAddEmptyLinkToData() 'relationship-name', new stdClass(), // in reality it will be a Comment class instance where $resource properties were taken from [], // links - ['this meta' => 'wont be shown'], // relationship meta + ['some' => 'relationship meta'], // relationship meta true // show data ); @@ -494,7 +494,10 @@ public function testAddEmptyLinkToData() "lastName" : "Dow" }, "relationships" : { - "relationship-name" : [] + "relationship-name" : { + "data" : [], + "meta" : { "some" : "relationship meta"} + } } } } @@ -537,7 +540,9 @@ public function testAddNullLinkToData() "lastName" : "Dow" }, "relationships" : { - "relationship-name" : null + "relationship-name" : { + "data" : null + } } } } @@ -805,7 +810,9 @@ public function testAddEmptyLinkToIncluded() "lastName" : "Dow" }, "relationships" : { - "comments-relationship" : [] + "comments-relationship" : { + "data" : [] + } }, "links" : { "self" : "peopleSelfUrl/" @@ -858,7 +865,9 @@ public function testAddNullLinkToIncluded() "lastName" : "Dow" }, "relationships" : { - "comments-relationship" : null + "comments-relationship" : { + "data" : null + } }, "links" : { "self" : "peopleSelfUrl/" @@ -1024,7 +1033,9 @@ public function testRelationshipsPrimaryMeta() "lastName" : "Dow" }, "relationships" : { - "relationship-name" : null, + "relationship-name" : { + "data" : null + }, "meta" : { "some" : "relationships meta" } } } @@ -1077,7 +1088,9 @@ public function testRelationshipsInclusionMeta() "lastName" : "Dow" }, "relationships" : { - "relationship-name" : null, + "relationship-name" : { + "data" : null + }, "meta" : { "some" : "relationships meta" } } }] diff --git a/tests/Encoder/EncodeIncludedObjectsTest.php b/tests/Encoder/EncodeIncludedObjectsTest.php index 3530afdb..c1dfde9c 100644 --- a/tests/Encoder/EncodeIncludedObjectsTest.php +++ b/tests/Encoder/EncodeIncludedObjectsTest.php @@ -275,8 +275,8 @@ public function testEncodeWithNullAndEmptyLinks() "body" : "Outside every fat man there was an even fatter man trying to close in" }, "relationships" : { - "author" : null, - "comments" : [] + "author" : {"data" : null}, + "comments" : {"data" : []} } }] } @@ -335,7 +335,7 @@ public function testEncodeDuplicatesWithCyclicDeps() "author" : { "data" : { "type" : "posts", "id" : "1" } }, - "comments" : [] + "comments" : {"data" : []} } }] } diff --git a/tests/Encoder/EncoderTest.php b/tests/Encoder/EncoderTest.php index 99587e0f..131c337e 100644 --- a/tests/Encoder/EncoderTest.php +++ b/tests/Encoder/EncoderTest.php @@ -288,8 +288,8 @@ public function testEncodeEmptyLinks() "body" : "Outside every fat man there was an even fatter man trying to close in" }, "relationships" : { - "author" : null, - "comments" : [] + "author" : {"data" : null}, + "comments" : {"data" : []} }, "links" : { "self" : "http://example.com/posts/1" @@ -489,11 +489,11 @@ public function testAddLinksToEmptyRelationship() Comment::class => CommentSchema::class, Post::class => function ($factory, $container) { $schema = new PostSchema($factory, $container); - //$schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::DATA, null); + $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::DATA, null); $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::SHOW_DATA, false); $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::SHOW_RELATED, true); $schema->linkAddTo(Post::LINK_AUTHOR, PostSchema::LINKS, ['foo' => new Link('/your/link', null, true)]); - //$schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::DATA, []); + $schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::DATA, []); $schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::SHOW_DATA, false); $schema->linkAddTo(Post::LINK_COMMENTS, PostSchema::LINKS, ['boo' => new Link('another/link')]); return $schema; From 3146bb9eff65fee43bcef25184832b9bf1813922 Mon Sep 17 00:00:00 2001 From: EK Date: Thu, 25 Jun 2015 21:09:12 +0300 Subject: [PATCH 4/4] Refactoring --- src/Document/Presenters/ElementPresenter.php | 30 +++++++++++--------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Document/Presenters/ElementPresenter.php b/src/Document/Presenters/ElementPresenter.php index 276c6afa..d68a4dd9 100644 --- a/src/Document/Presenters/ElementPresenter.php +++ b/src/Document/Presenters/ElementPresenter.php @@ -96,24 +96,28 @@ public function addRelationshipTo( // parent might be already added to included to it won't be in 'target' buffer if ($parentExists === true) { + $parentAlias = &$target[$parentType][$parentId]; + $name = $relation->getName(); - $alreadyGotData = isset($target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name]); - if ($alreadyGotData === false) { - // ... add the first one + $alreadyGotRelation = isset($parentAlias[Document::KEYWORD_RELATIONSHIPS][$name]); + + $linkage = null; + if ($relation->isShowData() === true) { + $linkage = $this->getLinkageRepresentation($resource); + } + + if ($alreadyGotRelation === false) { + // ... add the first linkage $representation = []; - if ($relation->isShowData() === true) { - $representation[Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); + if ($linkage !== null) { + $representation[Document::KEYWORD_LINKAGE_DATA][] = $linkage; } - $representation += $this->getRelationRepresentation($parent, $relation); - $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $representation; - } else { - // ... or add another relation - if ($relation->isShowData() === true) { - $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS] - [$name][Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); - } + $parentAlias[Document::KEYWORD_RELATIONSHIPS][$name] = $representation; + } elseif ($alreadyGotRelation === true && $linkage !== null) { + // ... or add another linkage + $parentAlias[Document::KEYWORD_RELATIONSHIPS][$name][Document::KEYWORD_LINKAGE_DATA][] = $linkage; } } }