Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 11, 2023
1 parent 4e48921 commit da975ac
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 516 deletions.
27 changes: 7 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,10 @@ GET /posts?include=author&fields[posts]=title,excerpt&fields[users]=name
"author": {
"data": {
"type": "users",
"id": "74812",
"meta": {}
},
"meta": {},
"links": {}
"id": "74812"
}
}
},
"meta": {},
"links": {}
}
},
{
"id": "39974",
Expand All @@ -436,15 +431,10 @@ GET /posts?include=author&fields[posts]=title,excerpt&fields[users]=name
"author": {
"data": {
"type": "users",
"id": "74812",
"meta": {}
},
"meta": {},
"links": {}
"id": "74812"
}
}
},
"meta": {},
"links": {}
}
}
],
"included": [
Expand All @@ -453,10 +443,7 @@ GET /posts?include=author&fields[posts]=title,excerpt&fields[users]=name
"id": "74812",
"attributes": {
"name": "Tim"
},
"relationships": {},
"meta": {},
"links": {}
}
}
]
}
Expand Down
17 changes: 10 additions & 7 deletions src/JsonApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,19 @@ public function toResourceIdentifier(Request $request)
* @api
*
* @param Request $request
* @return array{id: string, type: string, attributes: stdClass, relationships: stdClass, meta: stdClass, links: stdClass}
* @return array{id: string, type: string, attributes?: stdClass, relationships?: stdClass, meta?: stdClass, links?: stdClass}
*/
public function toArray($request)
{
return [
'id' => $this->resolveId($request),
'type' => $this->resolveType($request),
'attributes' => (object) $this->requestedAttributes($request)->all(),
'relationships' => (object) $this->requestedRelationshipsAsIdentifiers($request)->all(),
'meta' => (object) array_merge($this->toMeta($request), $this->meta),
'links' => (object) self::parseLinks(array_merge($this->toLinks($request), $this->links)),
...Collection::make([
'attributes' => $this->requestedAttributes($request)->all(),
'relationships' => $this->requestedRelationshipsAsIdentifiers($request)->all(),
'links' => self::parseLinks(array_merge($this->toLinks($request), $this->links)),
'meta' => array_merge($this->toMeta($request), $this->meta),
])->filter()->map(fn ($value) => (object) $value)->all(),
];
}

Expand All @@ -141,9 +143,10 @@ public function toArray($request)
public function with($request)
{
return [
'included' => $this->included($request)
...$included = $this->included($request)
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
->values(),
->values()
->all() ? ['included' => $included] : [],
'jsonapi' => self::serverImplementationResolver()($request),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiServerImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function jsonSerialize(): array
{
return [
'version' => $this->version,
'meta' => (object) $this->meta,
...$this->meta ? ['meta' => (object) $this->meta] : [],
];
}
}
2 changes: 1 addition & 1 deletion src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function jsonSerialize(): array
{
return [
'href' => $this->href,
'meta' => (object) $this->meta,
...$this->meta ? ['meta' => (object) $this->meta] : [],
];
}
}
4 changes: 2 additions & 2 deletions src/RelationshipObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function jsonSerialize(): array
{
return [
'data' => $this->data,
'meta' => (object) $this->meta,
'links' => (object) self::parseLinks($this->links),
...$this->meta ? ['meta' => (object) $this->meta] : [],
...$this->links ? ['links' => (object) self::parseLinks($this->links)] : [],
];
}
}
2 changes: 1 addition & 1 deletion src/ResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function jsonSerialize(): array
return [
'type' => $this->type,
'id' => $this->id,
'meta' => (object) $this->meta,
...$this->meta ? ['meta' => (object) $this->meta] : [],
];
}
}
57 changes: 0 additions & 57 deletions tests/Feature/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ public function toAttributes($request): array
'name' => 'Tim',
'email' => '[email protected]',
],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -84,13 +80,9 @@ public function toAttributes($request): array
'name' => 'Tim',
'location' => 'Melbourne',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -123,14 +115,9 @@ public function toAttributes($request): array
'data' => [
'id' => 'expected-id',
'type' => 'basicModels',
'attributes' => [],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -162,13 +149,9 @@ public function toAttributes($request): array
'attributes' => [
'location' => 'Melbourne',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -196,14 +179,9 @@ public function toAttributes($request): array
'data' => [
'id' => 'expected-id',
'type' => 'basicModels',
'attributes' => [],
'relationships' => [],
'links' => [],
'meta' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -252,14 +230,9 @@ public function testItCanSpecifyMinimalAttributes(): void
'data' => [
'type' => 'basicModels',
'id' => 'user-id',
'attributes' => [],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -287,13 +260,9 @@ public function testItCanRequestAttributesWhenUsingMinimalAttributes()
'attributes' => [
'name' => 'user-name',
],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -327,31 +296,23 @@ public function testItCanUseSparseFieldsetsWithIncludedCollections(): void
'data' => [
'id' => 'user-id',
'type' => 'basicModels',
'attributes' => [],
'relationships' => [
'posts' => [
'data' => [
[
'id' => 'post-id-1',
'meta' => [],
'type' => 'basicModels',
],
[
'id' => 'post-id-2',
'meta' => [],
'type' => 'basicModels',
],
],
'links' => [],
'meta' => [],
],
],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [
[
Expand All @@ -360,19 +321,13 @@ public function testItCanUseSparseFieldsetsWithIncludedCollections(): void
'attributes' => [
'title' => 'post-title-1',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
[
'id' => 'post-id-2',
'type' => 'basicModels',
'attributes' => [
'title' => 'post-title-2',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
],
]);
Expand Down Expand Up @@ -408,13 +363,9 @@ public function toAttributes($request): array
'attributes' => [
'email' => '[email protected]',
],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -452,13 +403,9 @@ public function toAttributes($request): array
'email' => '[email protected]',
'address' => '123 fake street',
],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down Expand Up @@ -497,13 +444,9 @@ public function toAttributes($request): array
'email' => '[email protected]',
'address' => '123 fake street',
],
'relationships' => [],
'meta' => [],
'links' => [],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [],
]);
Expand Down
7 changes: 0 additions & 7 deletions tests/Feature/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,13 @@ public function testItCanPaginate(): void
'attributes' => [
'name' => 'name-0',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
[
'id' => '2',
'type' => 'basicModels',
'attributes' => [
'name' => 'name-1',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
],
'included' => [],
Expand Down Expand Up @@ -100,7 +94,6 @@ public function testItCanPaginate(): void
],
],
'jsonapi' => [
'meta' => [],
'version' => '1.0',
],
]);
Expand Down
Loading

0 comments on commit da975ac

Please sign in to comment.