2
2
3
3
namespace Tobyz \JsonApiServer \Schema \Field ;
4
4
5
+ use Closure ;
5
6
use Tobyz \JsonApiServer \Context ;
6
7
use Tobyz \JsonApiServer \Endpoint \Concerns \FindsResources ;
7
8
use Tobyz \JsonApiServer \Exception \BadRequestException ;
@@ -14,7 +15,7 @@ abstract class Relationship extends Field
14
15
15
16
public array $ collections ;
16
17
public bool $ includable = false ;
17
- public bool $ linkage = false ;
18
+ public bool | Closure $ linkage = false ;
18
19
19
20
/**
20
21
* Set the collection(s) that this relationship is to.
@@ -47,9 +48,9 @@ public function includable(): static
47
48
/**
48
49
* Include linkage for this relationship.
49
50
*/
50
- public function withLinkage (): static
51
+ public function withLinkage (bool | Closure $ condition = true ): static
51
52
{
52
- $ this ->linkage = true ;
53
+ $ this ->linkage = $ condition ;
53
54
54
55
return $ this ;
55
56
}
@@ -59,20 +60,27 @@ public function withLinkage(): static
59
60
*/
60
61
public function withoutLinkage (): static
61
62
{
62
- $ this ->linkage = false ;
63
-
64
- return $ this ;
63
+ return $ this ->withLinkage (false );
65
64
}
66
65
67
66
public function getValue (Context $ context ): mixed
68
67
{
69
- if ($ context ->include === null && !$ this ->linkage ) {
68
+ if ($ context ->include === null && !$ this ->hasLinkage ( $ context ) ) {
70
69
return null ;
71
70
}
72
71
73
72
return parent ::getValue ($ context );
74
73
}
75
74
75
+ public function hasLinkage (Context $ context ): mixed
76
+ {
77
+ if ($ this ->linkage instanceof Closure) {
78
+ return ($ this ->linkage )($ context );
79
+ }
80
+
81
+ return $ this ->linkage ;
82
+ }
83
+
76
84
protected function findResourceForIdentifier (array $ identifier , Context $ context ): mixed
77
85
{
78
86
if (!isset ($ identifier ['type ' ])) {
0 commit comments