Skip to content

Commit e8078fb

Browse files
committed
feat: allow merge values on relationships
1 parent 981d794 commit e8078fb

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/Resources/Concerns/ConditionallyLoadsAttributes.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Ark4ne\JsonApi\Resources\Concerns;
44

5+
use Ark4ne\JsonApi\Descriptors\Relations\Relation;
6+
use Ark4ne\JsonApi\Resources\Relationship;
57
use Ark4ne\JsonApi\Support\Fields;
68
use Ark4ne\JsonApi\Support\Includes;
79
use Illuminate\Http\Request;
@@ -50,9 +52,27 @@ protected function whenIncluded(Request $request, string $type, mixed $value)
5052
*/
5153
protected function applyWhen(bool $condition, iterable $data): MergeValue
5254
{
53-
return new MergeValue($condition
54-
? $data
55-
: collect($data)->map(fn() => new MissingValue)
56-
);
55+
if ($condition) {
56+
return new MergeValue($data);
57+
}
58+
59+
return new MergeValue(collect($data)->map(function ($raw) {
60+
if ($raw instanceof Relationship) {
61+
$relation = new class ($raw->getResource(), fn () => $raw) extends Relation {
62+
protected function value(\Closure $value): Relationship
63+
{
64+
return ($this->relation)();
65+
}
66+
};
67+
68+
return $relation->when(false);
69+
}
70+
71+
if ($raw instanceof Relation) {
72+
return $raw->when(false);
73+
}
74+
75+
return new MissingValue();
76+
}));
5777
}
5878
}

src/Resources/Concerns/Relationships.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
trait Relationships
1212
{
13-
use Resolver;
13+
use PrepareData, Resolver;
1414

1515
/**
1616
* @see https://jsonapi.org/format/#document-resource-object-relationships
@@ -43,6 +43,7 @@ private function requestedRelationships(Request $request): array
4343
{
4444
$relations = [];
4545
$relationships = $this->toRelationships($request);
46+
$relationships = $this->mergeValues($relationships);
4647
$relationships = $this->resolveValues($request, $relationships);
4748
$relationships = $this->filter($relationships);
4849

tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Test\Unit\Resources\Concerns;
44

5+
use Ark4ne\JsonApi\Descriptors\Relations\RelationOne;
6+
use Ark4ne\JsonApi\Descriptors\Values\ValueMixed;
57
use Ark4ne\JsonApi\Resources\Concerns\ConditionallyLoadsAttributes;
68
use Illuminate\Http\Request;
79
use Illuminate\Http\Resources\Json\JsonResource;
@@ -80,5 +82,17 @@ public function testApplyWhen()
8082
'present.1' => 'abc',
8183
'present.2' => 123,
8284
]), $actual);
85+
$actual = Reflect::invoke($stub, 'applyWhen', true, [
86+
'present.1' => (new ValueMixed(fn() => 'abc')),
87+
'present.2' => (new ValueMixed(fn() => 123)),
88+
'present.3' => (new RelationOne('present', fn() => 'abc')),
89+
'present.4' => (new RelationOne('present', fn() => 123)),
90+
]);
91+
$this->assertEquals(new MergeValue([
92+
'present.1' => (new ValueMixed(fn() => 'abc')),
93+
'present.2' => (new ValueMixed(fn() => 123)),
94+
'present.3' => (new RelationOne('present', fn() => 'abc')),
95+
'present.4' => (new RelationOne('present', fn() => 123)),
96+
]), $actual);
8397
}
8498
}

0 commit comments

Comments
 (0)