3
3
namespace Binaryk \LaravelRestify ;
4
4
5
5
use Binaryk \LaravelRestify \Http \Requests \RestifyRequest ;
6
+ use Binaryk \LaravelRestify \Repositories \Repository ;
6
7
use Binaryk \LaravelRestify \Traits \Make ;
7
8
use Closure ;
8
9
use Illuminate \Http \Request ;
@@ -15,12 +16,18 @@ abstract class Filter implements JsonSerializable
15
16
16
17
public $ type = 'value ' ;
17
18
19
+ public $ column ;
20
+
18
21
public $ value ;
19
22
20
23
public $ canSeeCallback ;
21
24
22
25
public static $ uriKey ;
23
26
27
+ public $ relatedRepositoryKey ;
28
+
29
+ public Repository $ repository ;
30
+
24
31
public function __construct ()
25
32
{
26
33
$ this ->booted ();
@@ -55,6 +62,25 @@ protected function getType()
55
62
return $ this ->type ;
56
63
}
57
64
65
+ public function getColumn (): ?string
66
+ {
67
+ return $ this ->column ;
68
+ }
69
+
70
+ public function setColumn (string $ column ): self
71
+ {
72
+ $ this ->column = $ column ;
73
+
74
+ return $ this ;
75
+ }
76
+
77
+ public function setType (string $ type ): self
78
+ {
79
+ $ this ->type = $ type ;
80
+
81
+ return $ this ;
82
+ }
83
+
58
84
public function options (Request $ request )
59
85
{
60
86
// noop
@@ -76,6 +102,36 @@ public function resolve(RestifyRequest $request, $filter)
76
102
$ this ->value = $ filter ;
77
103
}
78
104
105
+ public function getRelatedRepositoryKey (): ?string
106
+ {
107
+ return $ this ->relatedRepositoryKey ;
108
+ }
109
+
110
+ public function setRelatedRepositoryKey (string $ repositoryKey ): self
111
+ {
112
+ $ this ->relatedRepositoryKey = $ repositoryKey ;
113
+
114
+ return $ this ;
115
+ }
116
+
117
+ public function setRepository (Repository $ repository ): self
118
+ {
119
+ $ this ->repository = $ repository ;
120
+
121
+ return $ this ;
122
+ }
123
+
124
+ public function getRelatedRepositoryUrl (): ?string
125
+ {
126
+ return ($ key = $ this ->getRelatedRepositoryKey ())
127
+ ? with (Restify::repositoryForKey ($ key ), function ($ repository = null ) {
128
+ if (is_subclass_of ($ repository , Repository::class)) {
129
+ return Restify::path ($ repository ::uriKey ());
130
+ }
131
+ })
132
+ : null ;
133
+ }
134
+
79
135
/**
80
136
* Get the URI key for the filter.
81
137
*
@@ -87,24 +143,28 @@ public static function uriKey()
87
143
return static ::$ uriKey ;
88
144
}
89
145
90
- $ kebabWithoutRepository = Str::kebab (Str::replaceLast ('Filter ' , '' , class_basename (get_called_class ())));
146
+ $ kebabWithoutFilter = Str::kebab (Str::replaceLast ('Filter ' , '' , class_basename (get_called_class ())));
91
147
92
- /**
93
- * e.g. UserRepository => users
94
- * e.g. LaravelEntityRepository => laravel-entities.
95
- */
96
- return Str::plural ($ kebabWithoutRepository );
148
+ return Str::plural ($ kebabWithoutFilter );
97
149
}
98
150
99
151
public function jsonSerialize ()
100
152
{
101
- return [
153
+ return with ( [
102
154
'class ' => static ::class,
103
155
'key ' => static ::uriKey (),
104
156
'type ' => $ this ->getType (),
157
+ 'column ' => $ this ->getColumn (),
105
158
'options ' => collect ($ this ->options (app (Request::class)))->map (function ($ value , $ key ) {
106
159
return is_array ($ value ) ? ($ value + ['property ' => $ key ]) : ['label ' => $ key , 'property ' => $ value ];
107
160
})->values ()->all (),
108
- ];
161
+ ], function (array $ initial ) {
162
+ return $ this ->relatedRepositoryKey
163
+ ? array_merge ($ initial , [
164
+ 'related_repository_key ' => $ this ->getRelatedRepositoryKey (),
165
+ 'related_repository_url ' => $ this ->getRelatedRepositoryUrl (),
166
+ ])
167
+ : $ initial ;
168
+ });
109
169
}
110
170
}
0 commit comments