Skip to content

Commit ffc7d7f

Browse files
authored
feat: conditional linkage (#110)
* feat: conditional linkage * Run Prettier --------- Co-authored-by: SychO9 <[email protected]>
1 parent a30aa59 commit ffc7d7f

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Diff for: src/Schema/Field/Relationship.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tobyz\JsonApiServer\Schema\Field;
44

5+
use Closure;
56
use Tobyz\JsonApiServer\Context;
67
use Tobyz\JsonApiServer\Endpoint\Concerns\FindsResources;
78
use Tobyz\JsonApiServer\Exception\BadRequestException;
@@ -14,7 +15,7 @@ abstract class Relationship extends Field
1415

1516
public array $collections;
1617
public bool $includable = false;
17-
public bool $linkage = false;
18+
public bool|Closure $linkage = false;
1819

1920
/**
2021
* Set the collection(s) that this relationship is to.
@@ -47,9 +48,9 @@ public function includable(): static
4748
/**
4849
* Include linkage for this relationship.
4950
*/
50-
public function withLinkage(): static
51+
public function withLinkage(bool|Closure $condition = true): static
5152
{
52-
$this->linkage = true;
53+
$this->linkage = $condition;
5354

5455
return $this;
5556
}
@@ -59,20 +60,27 @@ public function withLinkage(): static
5960
*/
6061
public function withoutLinkage(): static
6162
{
62-
$this->linkage = false;
63-
64-
return $this;
63+
return $this->withLinkage(false);
6564
}
6665

6766
public function getValue(Context $context): mixed
6867
{
69-
if ($context->include === null && !$this->linkage) {
68+
if ($context->include === null && !$this->hasLinkage($context)) {
7069
return null;
7170
}
7271

7372
return parent::getValue($context);
7473
}
7574

75+
public function hasLinkage(Context $context): mixed
76+
{
77+
if ($this->linkage instanceof Closure) {
78+
return ($this->linkage)($context);
79+
}
80+
81+
return $this->linkage;
82+
}
83+
7684
protected function findResourceForIdentifier(array $identifier, Context $context): mixed
7785
{
7886
if (!isset($identifier['type'])) {

Diff for: src/Schema/Field/ToMany.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public function serializeValue($value, Context $context): mixed
2020
{
2121
$meta = $this->serializeMeta($context);
2222

23-
if ((($context->include === null && !$this->linkage) || $value === null) && !$meta) {
23+
if (
24+
(($context->include === null && !$this->hasLinkage($context)) || $value === null) &&
25+
!$meta
26+
) {
2427
return null;
2528
}
2629

Diff for: src/Schema/Field/ToOne.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function serializeValue($value, Context $context): mixed
2626
{
2727
$meta = $this->serializeMeta($context);
2828

29-
if ($context->include === null && !$this->linkage && !$meta) {
29+
if ($context->include === null && !$this->hasLinkage($context) && !$meta) {
3030
return null;
3131
}
3232

0 commit comments

Comments
 (0)