@@ -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}
0 commit comments