1414use InvalidArgumentException ;
1515use League \Fractal \Pagination \PaginatorInterface ;
1616use League \Fractal \Resource \ResourceInterface ;
17+ use UnexpectedValueException ;
1718
1819class 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
0 commit comments