From c157d5f3f9ca3597eed205ba734558d9b1ea135a Mon Sep 17 00:00:00 2001 From: Kieryn <93209746+KierynAnnette@users.noreply.github.com> Date: Sat, 10 Feb 2024 14:51:44 +0000 Subject: [PATCH] fix: add missing resource meta functionality (#642) Closes #638 --- docs/basics/schemas.md | 15 ++++----------- docs/upgrade.md | 24 +++++++++++++++++++++++- src/Schema/Schema.php | 22 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/docs/basics/schemas.md b/docs/basics/schemas.md index ee219d32..103bffbe 100644 --- a/docs/basics/schemas.md +++ b/docs/basics/schemas.md @@ -428,28 +428,21 @@ document, overload the `getIncludedResourceLinks()` method instead. ## Meta -You can add top-level `meta` to your resource object using the `getPrimaryMeta()` or `getInclusionMeta()` methods -on your schema. These are called depending on whether your resource is appearing in either the primary `data` -member of the JSON API document or the `included` member. +You can add top-level `meta` to your resource object using the `getResourceMeta()` method +on your schema. -For example, the following would add meta to your resource object regardless of whether it is primary data or -included in the document: +For example: ```php class Schema extends SchemaProvider { // ... - public function getPrimaryMeta($resource) + public function getResourceMeta($resource) { return ['foo' => 'bar']; } - public function getInclusionMeta($resource) - { - return $this->getPrimaryMeta($resource); - } - } ``` diff --git a/docs/upgrade.md b/docs/upgrade.md index 4e361da6..8653ec8f 100644 --- a/docs/upgrade.md +++ b/docs/upgrade.md @@ -103,6 +103,28 @@ public function getId(object $resource): string return (string) $resource->getRouteKey(); } ``` +The functions that are used to call meta data has also been changed. Before there were these 2 functions: + +```php +public function getPrimaryMeta($resource) +{ + return ['foo => 'bar']; +} +public function getInclusionMeta($resource) +{ + return $this->getPrimaryMeta($resource); +} +``` + +These have now been replaced with 1 function: + +```php + public function getResourceMeta($resource): ?array + { + return ['foo => 'bar']; + } +``` +This method will be used in place of the other 2. In the rare event that your inclution meta was different from primary, you may need to amalgemate. ### Errors @@ -116,4 +138,4 @@ your use against the new constructor arguments by inspecting the class directly. ## 2.x to 3.0 -[Use this link to view the 3.0 upgrade guide.](https://github.com/cloudcreativity/laravel-json-api/blob/v3.3.0/docs/upgrade.md) \ No newline at end of file +[Use this link to view the 3.0 upgrade guide.](https://github.com/cloudcreativity/laravel-json-api/blob/v3.3.0/docs/upgrade.md) diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index 8fa7ff6e..f7775003 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -133,4 +133,24 @@ public function getRelationshipRelatedLink($resource, string $name): LinkInterfa { return $this->provider->getRelationshipRelatedLink($resource, $name); } -} \ No newline at end of file + + /** + * @inheritDoc + */ + public function getResourceMeta($resource): ?array + { + if($this->hasResourceMeta($resource)){ + return $this->provider->getResourceMeta($resource); + } + + return null; + } + + /** + * @inheritDoc + */ + public function hasResourceMeta($resource): bool + { + return method_exists($this->provider, 'getResourceMeta'); + } +}