Skip to content

Commit e2ded29

Browse files
author
kordum
committed
Added the ability to specify the resource type in the resource data
1 parent 573ca2e commit e2ded29

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/Serializer/JsonApiSerializer.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use InvalidArgumentException;
1515
use League\Fractal\Pagination\PaginatorInterface;
1616
use League\Fractal\Resource\ResourceInterface;
17+
use UnexpectedValueException;
1718

1819
class JsonApiSerializer extends ArraySerializer
1920
{
@@ -46,6 +47,10 @@ public function item(?string $resourceKey, array $data): array
4647
{
4748
$id = $this->getIdFromData($data);
4849

50+
$resourceKey = $resourceKey
51+
?? $data['type']
52+
?? throw new UnexpectedValueException('The resource must have a key specified.');
53+
4954
$resource = [
5055
'data' => [
5156
'type' => $resourceKey,
@@ -54,7 +59,10 @@ public function item(?string $resourceKey, array $data): array
5459
],
5560
];
5661

57-
unset($resource['data']['attributes']['id']);
62+
unset(
63+
$resource['data']['attributes']['id'],
64+
$resource['data']['attributes']['type'],
65+
);
5866

5967
if (isset($resource['data']['attributes']['links'])) {
6068
$custom_links = $data['links'];
@@ -150,7 +158,7 @@ public function null(): ?array
150158
*/
151159
public function includedData(ResourceInterface $resource, array $data): array
152160
{
153-
list($serializedData, $linkedIds) = $this->pullOutNestedIncludedData($data);
161+
[$serializedData, $linkedIds] = $this->pullOutNestedIncludedData($data);
154162

155163
foreach ($data as $value) {
156164
foreach ($value as $includeObject) {
@@ -160,7 +168,7 @@ public function includedData(ResourceInterface $resource, array $data): array
160168

161169
$includeObjects = $this->createIncludeObjects($includeObject);
162170

163-
list($serializedData, $linkedIds) = $this->serializeIncludedObjectsWithCacheKey(
171+
[$serializedData, $linkedIds] = $this->serializeIncludedObjectsWithCacheKey(
164172
$includeObjects,
165173
$linkedIds,
166174
$serializedData
@@ -329,7 +337,7 @@ protected function pullOutNestedIncludedData(array $data): array
329337
foreach ($data as $value) {
330338
foreach ($value as $includeObject) {
331339
if (isset($includeObject['included'])) {
332-
list($includedData, $linkedIds) = $this->serializeIncludedObjectsWithCacheKey(
340+
[$includedData, $linkedIds] = $this->serializeIncludedObjectsWithCacheKey(
333341
$includeObject['included'],
334342
$linkedIds,
335343
$includedData

test/Serializer/JsonApiSerializerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ public function testSerializeCollectionWithExtraMeta()
8787
$this->assertSame($expectedJson, $scope->toJson());
8888
}
8989

90+
public function testSerializingItemResourceWithNestedType()
91+
{
92+
$this->manager->parseIncludes('author');
93+
94+
$bookData = [
95+
'id' => 1,
96+
'type' => 'books',
97+
'title' => 'Foo',
98+
'year' => '1991',
99+
];
100+
101+
$resource = new Item($bookData, new JsonApiBookTransformer());
102+
$scope = new Scope($this->manager, $resource);
103+
104+
$expected = [
105+
'data' => [
106+
'type' => 'books',
107+
'id' => '1',
108+
'attributes' => [
109+
'title' => 'Foo',
110+
'year' => 1991,
111+
],
112+
],
113+
];
114+
115+
$this->assertSame($expected, $scope->toArray());
116+
117+
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}';
118+
$this->assertSame($expectedJson, $scope->toJson());
119+
}
120+
90121
public function testSerializingItemResourceWithHasOneInclude()
91122
{
92123
$this->manager->parseIncludes('author');

0 commit comments

Comments
 (0)