Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Including the resource once if an 'id' is present. #126

Merged
merged 1 commit into from
Nov 19, 2014
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
17 changes: 15 additions & 2 deletions src/Serializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,23 @@ public function item($resourceKey, array $data)
public function includedData($resourceKey, array $data)
{
$serializedData = array();

$linkedIds = array();
foreach ($data as $value) {
foreach ($value as $includeKey => $includeValue) {
$serializedData = array_merge_recursive($serializedData, $includeValue);
foreach ($includeValue[$includeKey] as $itemValue) {
if (!array_key_exists('id', $itemValue)) {
$serializedData[$includeKey][] = $itemValue;
continue;
}

$itemId = $itemValue['id'];
if (!empty($linkedIds[$includeKey]) && in_array($itemId, $linkedIds[$includeKey], true)) {
continue;
}

$serializedData[$includeKey][] = $itemValue;
$linkedIds[$includeKey][] = $itemId;
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions test/Serializer/JsonApiSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,58 @@ public function testSerializingCollectionResourceWithMeta()
$this->assertEquals($expectedJson, $scope->toJson());
}

public function testSerializingCollectionResourceWithDuplicatedIncludeData() {
$this->manager->parseIncludes('author');

$booksData = array(
array(
'title' => 'Foo',
'year' => '1991',
'_author' => array(
'id' => 1,
'name' => 'Dave',
),
),
array(
'title' => 'Bar',
'year' => '1997',
'_author' => array(
'id' => 1,
'name' => 'Dave',
),
),
);

$resource = new Collection($booksData, new GenericBookTransformer(), 'book');
$scope = new Scope($this->manager, $resource);

$expected = array(
'book' => array(
array(
'title' => 'Foo',
'year' => 1991,
),
array(
'title' => 'Bar',
'year' => 1997,
),
),
'linked' => array(
'author' => array(
array(
'id' => 1,
'name' => 'Dave',
),
),
),
);

$this->assertEquals($expected, $scope->toArray());

$expectedJson = '{"book":[{"title":"Foo","year":1991},{"title":"Bar","year":1997}],"linked":{"author":[{"id":1,"name":"Dave"}]}}';
$this->assertEquals($expectedJson, $scope->toJson());
}

public function testResourceKeyMissing()
{
$this->manager->setSerializer(new JsonApiSerializer());
Expand Down
8 changes: 4 additions & 4 deletions test/Stub/Transformer/GenericBookTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class GenericBookTransformer extends TransformerAbstract

public function transform(array $book)
{
return array(
'title' => $book['title'],
'year' => (int) $book['year'],
);
$book['year'] = (int) $book['year'];
unset($book['_author']);

return $book;
}

public function includeAuthor(array $book)
Expand Down