Skip to content

Commit b74192c

Browse files
committed
feat: Includes add method "includes" which return the remaining includes for current resource
1 parent d22f466 commit b74192c

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

src/Support/Includes.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ class Includes
1212

1313
private static array $stack = [];
1414

15-
public static function through(string $type, callable $callable)
15+
/**
16+
* Defined current resource-type/relation through callback
17+
*
18+
* @param string $type
19+
* @param callable $callable
20+
*
21+
* @return mixed
22+
*/
23+
public static function through(string $type, callable $callable): mixed
1624
{
1725
try {
1826
self::$stack[] = $type;
@@ -23,13 +31,41 @@ public static function through(string $type, callable $callable)
2331
}
2432
}
2533

34+
/**
35+
* Return remaining includes for current resource
36+
*
37+
* @param \Illuminate\Http\Request $request
38+
*
39+
* @return array
40+
*/
2641
public static function get(Request $request): array
2742
{
28-
return array_keys(Arr::get(
29-
self::parse($request->input('include', '')),
30-
implode('.', self::$stack) ?: null,
31-
[]
32-
));
43+
return array_keys(self::currentStack($request));
44+
}
45+
46+
/**
47+
* Return if a resource-type/relation is included
48+
*
49+
* @param \Illuminate\Http\Request $request
50+
* @param string $type
51+
*
52+
* @return bool
53+
*/
54+
public static function include(Request $request, string $type): bool
55+
{
56+
return in_array($type, self::get($request), true);
57+
}
58+
59+
/**
60+
* Return remaining includes
61+
*
62+
* @param \Illuminate\Http\Request $request
63+
*
64+
* @return array
65+
*/
66+
public static function includes(Request $request): array
67+
{
68+
return array_keys(Arr::dot(self::currentStack($request)));
3369
}
3470

3571
public static function parse(string $include): array
@@ -39,8 +75,12 @@ public static function parse(string $include): array
3975
);
4076
}
4177

42-
public static function include(Request $request, string $type): bool
78+
private static function currentStack(Request $request): array
4379
{
44-
return in_array($type, self::get($request), true);
80+
return Arr::get(
81+
self::parse($request->input('include', '')),
82+
implode('.', self::$stack) ?: null,
83+
[]
84+
);
4585
}
4686
}

tests/Unit/Support/IncludesTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class IncludesTest extends TestCase
1010
{
1111
public $count = 0;
1212

13-
public function testIncludes()
13+
public function testCyclelife()
1414
{
1515
$request = new Request([
1616
'include' => implode(',', [
@@ -97,4 +97,35 @@ public function testParse()
9797
],
9898
], $parsed);
9999
}
100+
101+
public function testIncludes()
102+
{
103+
$request = new Request([
104+
'include' => implode(',', [
105+
'user',
106+
'posts',
107+
'posts.user',
108+
'posts.user.comment',
109+
'posts',
110+
'posts.user',
111+
'posts.user.posts',
112+
'posts.test',
113+
])
114+
]);
115+
116+
$this->assertEquals([
117+
'user',
118+
'posts.user.comment',
119+
'posts.user.posts',
120+
'posts.test',
121+
], Includes::includes($request));
122+
123+
Includes::through('posts', function () use ($request) {
124+
$this->assertEquals([
125+
'user.comment',
126+
'user.posts',
127+
'test',
128+
], Includes::includes($request));
129+
});
130+
}
100131
}

0 commit comments

Comments
 (0)