Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enhancement: allow the ability to define meta on each object in collection #358

Merged
merged 1 commit into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Serializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
108 changes: 108 additions & 0 deletions test/Serializer/JsonApiSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 = [
Expand Down