Skip to content

Commit 1f080b5

Browse files
committed
feat: conditionally loads fields
1 parent beacaf0 commit 1f080b5

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/Resources/Concerns/ConditionallyLoadsAttributes.php

+17
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
namespace Ark4ne\JsonApi\Resources\Concerns;
44

5+
use Ark4ne\JsonApi\Support\Fields;
56
use Ark4ne\JsonApi\Support\Includes;
67
use Illuminate\Http\Request;
78

89
trait ConditionallyLoadsAttributes
910
{
11+
/**
12+
* Retrieve a relationship if it has been present in fields.
13+
*
14+
* @template T
15+
*
16+
* @param \Illuminate\Http\Request $request
17+
* @param string $attribute
18+
* @param T $value
19+
*
20+
* @return \Illuminate\Http\Resources\MissingValue|T
21+
*/
22+
protected function whenInFields(Request $request, string $attribute, mixed $value)
23+
{
24+
return $this->when(in_array($attribute, Fields::get($request, $this->toType($request)) ?? [], true), $value);
25+
}
26+
1027
/**
1128
* Retrieve a relationship if it has been included.
1229
*

tests/Feature/SchemaTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SchemaTest extends FeatureTestCase
1212
{
1313
public function testSchema()
1414
{
15-
$user = new Skeleton(UserResource::class, 'user', ['name', 'email']);
15+
$user = new Skeleton(UserResource::class, 'user', ['name', 'email', 'only-with-fields']);
1616
$post = new Skeleton(PostResource::class, 'post', ['title', 'content']);
1717
$comment = new Skeleton(CommentResource::class, 'comment', ['content']);
1818

tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,26 @@ public function testWhenInclude($expected, $property, $query)
3535
Reflect::invoke($stub, 'whenIncluded', $request, $property, true)
3636
);
3737
}
38+
39+
/**
40+
* @dataProvider dataWhenInclude
41+
*/
42+
public function testWhenInFields($expected, $property, $query)
43+
{
44+
$request = new Request(['fields' => ['type' => implode(',', $query)]]);
45+
46+
$stub = new class(null) extends JsonResource {
47+
use ConditionallyLoadsAttributes;
48+
49+
protected function toType()
50+
{
51+
return 'type';
52+
}
53+
};
54+
55+
$this->assertEquals(
56+
$expected ?: new MissingValue,
57+
Reflect::invoke($stub, 'whenInFields', $request, $property, true)
58+
);
59+
}
3860
}

tests/app/Http/Resources/UserResource.php

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

33
namespace Test\app\Http\Resources;
44

5+
use Ark4ne\JsonApi\Resources\Concerns\ConditionallyLoadsAttributes;
56
use Ark4ne\JsonApi\Resources\JsonApiResource;
67
use DateTimeInterface;
78
use Illuminate\Http\Request;
@@ -11,6 +12,8 @@
1112
*/
1213
class UserResource extends JsonApiResource
1314
{
15+
use ConditionallyLoadsAttributes;
16+
1417
protected function toType(Request $request): string
1518
{
1619
return 'user';
@@ -21,6 +24,7 @@ protected function toAttributes(Request $request): iterable
2124
return [
2225
'name' => $this->resource->name,
2326
'email' => $this->resource->email,
27+
'only-with-fields' => $this->whenInFields($request, 'only-with-fields', fn() => 'huge-data-set'),
2428
];
2529
}
2630

0 commit comments

Comments
 (0)