Skip to content

Commit 5b6a177

Browse files
simondotwhiteSimon White
and
Simon White
authored
Auto exclude (#253)
Co-authored-by: Simon White <simon@icon-creative.com>
1 parent 5e6cf9f commit 5b6a177

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ return [
133133
*/
134134
'request_key' => 'include',
135135
],
136+
137+
'auto_excludes' => [
138+
139+
/*
140+
* If enabled Fractal will automatically add the excludes who's
141+
* names are present in the `include` request parameter.
142+
*/
143+
'enabled' => true,
144+
145+
/*
146+
* The name of key in the request to where we should look for the excludes to exclude.
147+
*/
148+
'request_key' => 'exclude',
149+
],
136150
```
137151

138152
## Usage

config/fractal.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,18 @@
3939
*/
4040
'request_key' => 'include',
4141
],
42+
43+
'auto_excludes' => [
44+
45+
/*
46+
* If enabled Fractal will automatically add the excludes who's
47+
* names are present in the `exclude` request parameter.
48+
*/
49+
'enabled' => true,
50+
51+
/*
52+
* The name of key in the request to where we should look for the excludes to exclude.
53+
*/
54+
'request_key' => 'exclude',
55+
],
4256
];

src/Fractal.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public static function create($data = null, $transformer = null, $serializer = n
4343
}
4444
}
4545

46+
if (config('fractal.auto_excludes.enabled')) {
47+
$requestKey = config('fractal.auto_excludes.request_key');
48+
49+
if ($exclude = app('request')->get($requestKey)) {
50+
$fractal->parseExcludes($exclude);
51+
}
52+
}
53+
4654
if (empty($serializer)) {
4755
$serializer = config('fractal.default_serializer');
4856
}

tests/AutoExcludeTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
it('excludes can be passed as array', function () {
4+
$response = $this->call('GET', '/auto-includes', [
5+
'include' => [
6+
'characters',
7+
'publisher',
8+
],
9+
'exclude' => [
10+
'publisher',
11+
],
12+
]);
13+
$response->assertStatus(200);
14+
$response->assertJsonStructure([
15+
'data' => [
16+
['id', 'author', 'characters'],
17+
],
18+
]);
19+
});
20+
21+
it('excludes can be passed as string', function () {
22+
$response = $this->call('GET', '/auto-includes', [
23+
'include' => 'characters,publisher',
24+
'exclude' => 'publisher',
25+
]);
26+
$response->assertStatus(200);
27+
$response->assertJsonStructure([
28+
'data' => [
29+
['id', 'author', 'characters'],
30+
],
31+
]);
32+
});
33+
34+
it('excludes are missing when parameter is not passed', function () {
35+
$response = $this->call('GET', '/auto-includes');
36+
$response->assertStatus(200);
37+
$response->assertJsonStructure([
38+
'data' => [
39+
['id', 'author'],
40+
],
41+
]);
42+
43+
foreach ($response->json()['data'] as $book) {
44+
expect($book)->not->toHaveKeys(['characters', 'publisher']);
45+
}
46+
});

0 commit comments

Comments
 (0)