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

Strip empty properties #40

Merged
merged 7 commits into from
Oct 11, 2023
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
11 changes: 6 additions & 5 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
->setRules([
'@PSR12' => true,
'@PSR12:risky' => true,
'no_unused_imports' => true,
'array_indentation' => true,
'declare_strict_types' => true,
'explicit_string_variable' => true,
'global_namespace_import' => ['import_classes' => true, 'import_constants' => true, 'import_functions' => true],
'native_function_invocation' => true,
'declare_strict_types' => true,
'no_unused_imports' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
'single_quote' => true,
'strict_comparison' => true,
'strict_param' => true,
'explicit_string_variable' => true,
'trailing_comma_in_multiline' => true,
'array_indentation' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
])
->setRiskyAllowed(true)
->setFinder($finder);
42 changes: 7 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ use TiMacDonald\JsonApi\JsonApiResource;

class UserResource extends JsonApiResource
{
/**
* @var string[]
*/
public $attributes = [
'name',
'website',
Expand Down Expand Up @@ -163,18 +160,12 @@ use TiMacDonald\JsonApi\JsonApiResource;

class UserResource extends JsonApiResource
{
/**
* @var string[]
*/
public $attributes = [
'name',
'website',
'twitter_handle',
];

/**
* @var array<string, class-string<JsonApiResource>>
*/
public $relationships = [
'team' => TeamResource::class,
'posts' => PostResource::class,
Expand All @@ -193,18 +184,12 @@ use TiMacDonald\JsonApi\JsonApiResource;

class UserResource extends JsonApiResource
{
/**
* @var string[]
*/
public $attributes = [
'name',
'website',
'twitter_handle',
];

/**
* @var string[]
*/
public $relationships = [
'team',
'posts',
Expand Down Expand Up @@ -415,15 +400,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 +416,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 +428,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),
];
}

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
5 changes: 3 additions & 2 deletions src/JsonApiResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ private function resolveResourceIdentifiers(Request $request)
public function with($request)
{
return [
'included' => $this->collection
...($included = $this->collection
->map(fn (JsonApiResource $resource): Collection => $resource->included($request))
->flatten()
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
->values(),
->values()
->all()) ? ['included' => $included] : [],
'jsonapi' => JsonApiResource::serverImplementationResolver()($request),
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiServerImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public function __construct(string $version, array $meta = [])
/**
* @internal
*
* @return array{version: string, meta: stdClass}
* @return array{version: string, meta?: stdClass}
*/
public function jsonSerialize(): array
{
return [
'version' => $this->version,
'meta' => (object) $this->meta,
...$this->meta ? ['meta' => (object) $this->meta] : [],
];
}
}
4 changes: 2 additions & 2 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public function __construct(string $key, string $href, array $meta = [])
/**
* @internal
*
* @return array{href: string, meta: stdClass}
* @return array{href: string, meta?: stdClass}
*/
public function jsonSerialize(): array
{
return [
'href' => $this->href,
'meta' => (object) $this->meta,
...$this->meta ? ['meta' => (object) $this->meta] : [],
];
}
}
6 changes: 3 additions & 3 deletions src/RelationshipObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ private function __construct(ResourceIdentifier|array|null $data, array $links =
/**
* @internal
*
* @return array{data: ResourceIdentifier|null|array<int, ResourceIdentifier>, meta: stdClass, links: stdClass}
* @return array{data: ResourceIdentifier|null|array<int, ResourceIdentifier>, meta?: stdClass, links?: stdClass}
*/
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)] : [],
];
}
}
4 changes: 2 additions & 2 deletions src/ResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public function __construct(string $type, string $id, array $meta = [])
/**
* @internal
*
* @return array{type: string, id: string, meta: stdClass}
* @return array{type: string, id: string, meta?: stdClass}
*/
public function jsonSerialize(): array
{
return [
'type' => $this->type,
'id' => $this->id,
'meta' => (object) $this->meta,
...$this->meta ? ['meta' => (object) $this->meta] : [],
];
}
}
Loading