From 73fa4f314f77796a8751cfdbf482b3f3823104bd Mon Sep 17 00:00:00 2001 From: Graham Daniels Date: Wed, 8 Feb 2017 14:03:11 -0500 Subject: [PATCH] allow the ability to define meta on each object in collection --- src/Serializer/JsonApiSerializer.php | 5 + test/Serializer/JsonApiSerializerTest.php | 108 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/src/Serializer/JsonApiSerializer.php b/src/Serializer/JsonApiSerializer.php index 0f5781ff..e09bbb04 100644 --- a/src/Serializer/JsonApiSerializer.php +++ b/src/Serializer/JsonApiSerializer.php @@ -77,6 +77,11 @@ public function item($resourceKey, array $data) unset($resource['data']['attributes']['links']); } + if (isset($resource['data']['attributes']['meta'])){ + $resource['data']['meta'] = $data['meta']; + unset($resource['data']['attributes']['meta']); + } + if ($this->shouldIncludeLinks()) { $resource['data']['links'] = [ 'self' => "{$this->baseUrl}/$resourceKey/$id", diff --git a/test/Serializer/JsonApiSerializerTest.php b/test/Serializer/JsonApiSerializerTest.php index 6db52524..bafcce7d 100644 --- a/test/Serializer/JsonApiSerializerTest.php +++ b/test/Serializer/JsonApiSerializerTest.php @@ -18,6 +18,71 @@ public function setUp() $this->manager->setSerializer(new JsonApiSerializer()); } + public function testSerializeCollectionWithExtraMeta() + { + $booksData = [ + [ + 'id' => 1, + 'title' => 'Foo', + 'year' => '1991', + '_author' => [ + 'id' => 1, + 'name' => 'Dave', + ], + 'meta' => [ + 'foo' => 'bar' + ] + ], + [ + 'id' => 2, + 'title' => 'Bar', + 'year' => '1997', + '_author' => [ + 'id' => 2, + 'name' => 'Bob', + ], + 'meta' => [ + 'bar' => 'baz' + ] + ], + ]; + + $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); + $scope = new Scope($this->manager, $resource); + + $expected = [ + 'data' => [ + [ + 'type' => 'books', + 'id' => '1', + 'attributes' => [ + 'title' => 'Foo', + 'year' => 1991, + ], + 'meta' => [ + 'foo' => 'bar' + ] + ], + [ + 'type' => 'books', + 'id' => '2', + 'attributes' => [ + 'title' => 'Bar', + 'year' => 1997, + ], + 'meta' => [ + 'bar' => 'baz' + ] + ], + ], + ]; + + $this->assertSame($expected, $scope->toArray()); + + $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"foo":"bar"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"meta":{"bar":"baz"}}]}'; + $this->assertSame($expectedJson, $scope->toJson()); + } + public function testSerializingItemResourceWithHasOneInclude() { $this->manager->parseIncludes('author'); @@ -341,6 +406,49 @@ public function testSerializingItemResourceWithMeta() $this->assertSame($expectedJson, $scope->toJson()); } + public function testSerializingItemResourceWithMetaInBody() + { + $bookData = [ + 'id' => 1, + 'title' => 'Foo', + 'year' => '1991', + '_author' => [ + 'id' => 1, + 'name' => 'Dave', + ], + 'meta' => [ + 'something' => 'something' + ] + ]; + + $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); + $resource->setMetaValue('foo', 'bar'); + + $scope = new Scope($this->manager, $resource); + + $expected = [ + 'data' => [ + 'type' => 'books', + 'id' => '1', + 'attributes' => [ + 'title' => 'Foo', + 'year' => 1991, + ], + 'meta' => [ + 'something' => 'something' + ] + ], + 'meta' => [ + 'foo' => 'bar' + ], + ]; + + $this->assertSame($expected, $scope->toArray()); + + $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"something":"something"}},"meta":{"foo":"bar"}}'; + $this->assertSame($expectedJson, $scope->toJson()); + } + public function testSerializingCollectionResourceWithoutIncludes() { $booksData = [