Skip to content

Commit

Permalink
fix: add missing resource meta functionality (#642)
Browse files Browse the repository at this point in the history
Closes #638
  • Loading branch information
KierynAnnette authored Feb 10, 2024
1 parent 75b73cf commit c157d5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
15 changes: 4 additions & 11 deletions docs/basics/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
```

Expand Down
24 changes: 23 additions & 1 deletion docs/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
[Use this link to view the 3.0 upgrade guide.](https://github.com/cloudcreativity/laravel-json-api/blob/v3.3.0/docs/upgrade.md)
22 changes: 21 additions & 1 deletion src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,24 @@ public function getRelationshipRelatedLink($resource, string $name): LinkInterfa
{
return $this->provider->getRelationshipRelatedLink($resource, $name);
}
}

/**
* @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');
}
}

0 comments on commit c157d5f

Please sign in to comment.