33namespace Binaryk \LaravelRestify ;
44
55use Binaryk \LaravelRestify \Http \Requests \RestifyRequest ;
6+ use Binaryk \LaravelRestify \Repositories \Repository ;
67use Binaryk \LaravelRestify \Traits \Make ;
78use Closure ;
89use Illuminate \Http \Request ;
@@ -15,12 +16,18 @@ abstract class Filter implements JsonSerializable
1516
1617 public $ type = 'value ' ;
1718
19+ public $ column ;
20+
1821 public $ value ;
1922
2023 public $ canSeeCallback ;
2124
2225 public static $ uriKey ;
2326
27+ public $ relatedRepositoryKey ;
28+
29+ public Repository $ repository ;
30+
2431 public function __construct ()
2532 {
2633 $ this ->booted ();
@@ -55,6 +62,25 @@ protected function getType()
5562 return $ this ->type ;
5663 }
5764
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+
5884 public function options (Request $ request )
5985 {
6086 // noop
@@ -76,6 +102,36 @@ public function resolve(RestifyRequest $request, $filter)
76102 $ this ->value = $ filter ;
77103 }
78104
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+
79135 /**
80136 * Get the URI key for the filter.
81137 *
@@ -87,24 +143,28 @@ public static function uriKey()
87143 return static ::$ uriKey ;
88144 }
89145
90- $ kebabWithoutRepository = Str::kebab (Str::replaceLast ('Filter ' , '' , class_basename (get_called_class ())));
146+ $ kebabWithoutFilter = Str::kebab (Str::replaceLast ('Filter ' , '' , class_basename (get_called_class ())));
91147
92- /**
93- * e.g. UserRepository => users
94- * e.g. LaravelEntityRepository => laravel-entities.
95- */
96- return Str::plural ($ kebabWithoutRepository );
148+ return Str::plural ($ kebabWithoutFilter );
97149 }
98150
99151 public function jsonSerialize ()
100152 {
101- return [
153+ return with ( [
102154 'class ' => static ::class,
103155 'key ' => static ::uriKey (),
104156 'type ' => $ this ->getType (),
157+ 'column ' => $ this ->getColumn (),
105158 'options ' => collect ($ this ->options (app (Request::class)))->map (function ($ value , $ key ) {
106159 return is_array ($ value ) ? ($ value + ['property ' => $ key ]) : ['label ' => $ key , 'property ' => $ value ];
107160 })->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+ });
109169 }
110170}
0 commit comments