Skip to content

Commit c157d5f

Browse files
fix: add missing resource meta functionality (#642)
Closes #638
1 parent 75b73cf commit c157d5f

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

docs/basics/schemas.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,28 +428,21 @@ document, overload the `getIncludedResourceLinks()` method instead.
428428

429429
## Meta
430430

431-
You can add top-level `meta` to your resource object using the `getPrimaryMeta()` or `getInclusionMeta()` methods
432-
on your schema. These are called depending on whether your resource is appearing in either the primary `data`
433-
member of the JSON API document or the `included` member.
431+
You can add top-level `meta` to your resource object using the `getResourceMeta()` method
432+
on your schema.
434433

435-
For example, the following would add meta to your resource object regardless of whether it is primary data or
436-
included in the document:
434+
For example:
437435

438436
```php
439437
class Schema extends SchemaProvider
440438
{
441439
// ...
442440

443-
public function getPrimaryMeta($resource)
441+
public function getResourceMeta($resource)
444442
{
445443
return ['foo' => 'bar'];
446444
}
447445

448-
public function getInclusionMeta($resource)
449-
{
450-
return $this->getPrimaryMeta($resource);
451-
}
452-
453446
}
454447
```
455448

docs/upgrade.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ public function getId(object $resource): string
103103
return (string) $resource->getRouteKey();
104104
}
105105
```
106+
The functions that are used to call meta data has also been changed. Before there were these 2 functions:
107+
108+
```php
109+
public function getPrimaryMeta($resource)
110+
{
111+
return ['foo => 'bar'];
112+
}
113+
public function getInclusionMeta($resource)
114+
{
115+
return $this->getPrimaryMeta($resource);
116+
}
117+
```
118+
119+
These have now been replaced with 1 function:
120+
121+
```php
122+
public function getResourceMeta($resource): ?array
123+
{
124+
return ['foo => 'bar'];
125+
}
126+
```
127+
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.
106128

107129
### Errors
108130

@@ -116,4 +138,4 @@ your use against the new constructor arguments by inspecting the class directly.
116138

117139
## 2.x to 3.0
118140

119-
[Use this link to view the 3.0 upgrade guide.](https://github.com/cloudcreativity/laravel-json-api/blob/v3.3.0/docs/upgrade.md)
141+
[Use this link to view the 3.0 upgrade guide.](https://github.com/cloudcreativity/laravel-json-api/blob/v3.3.0/docs/upgrade.md)

src/Schema/Schema.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,24 @@ public function getRelationshipRelatedLink($resource, string $name): LinkInterfa
133133
{
134134
return $this->provider->getRelationshipRelatedLink($resource, $name);
135135
}
136-
}
136+
137+
/**
138+
* @inheritDoc
139+
*/
140+
public function getResourceMeta($resource): ?array
141+
{
142+
if($this->hasResourceMeta($resource)){
143+
return $this->provider->getResourceMeta($resource);
144+
}
145+
146+
return null;
147+
}
148+
149+
/**
150+
* @inheritDoc
151+
*/
152+
public function hasResourceMeta($resource): bool
153+
{
154+
return method_exists($this->provider, 'getResourceMeta');
155+
}
156+
}

0 commit comments

Comments
 (0)