4
4
5
5
use ReflectionException ;
6
6
use ReflectionFunction ;
7
- use RuntimeException ;
8
7
use Tobyz \JsonApiServer \JsonApi ;
9
8
use Tobyz \JsonApiServer \Resource \Collection ;
9
+ use Tobyz \JsonApiServer \Resource \Listable ;
10
10
use Tobyz \JsonApiServer \Resource \Resource ;
11
11
use Tobyz \JsonApiServer \Schema \Field \Field ;
12
12
use Tobyz \JsonApiServer \Schema \Field \Relationship ;
13
+ use Tobyz \JsonApiServer \Schema \Filter ;
13
14
14
15
trait BuildsOpenApiPaths
15
16
{
@@ -77,19 +78,6 @@ private function buildLinksObject(string $name): array
77
78
];
78
79
}
79
80
80
- private function findResourceFromItem (array $ item ): string
81
- {
82
- $ value = $ item ['$ref ' ] ?? null ;
83
-
84
- if (empty ($ value )) {
85
- throw new RuntimeException ('Unhandled. ' );
86
- }
87
-
88
- $ parts = explode ('/ ' , $ value );
89
-
90
- return end ($ parts );
91
- }
92
-
93
81
/**
94
82
* @throws ReflectionException
95
83
*/
@@ -98,16 +86,11 @@ private function buildOpenApiParameters(Collection $collection): array
98
86
// @todo: fix this
99
87
assert ($ collection instanceof Resource);
100
88
101
- $ parameters = [$ this ->buildIncludeParameter ($ collection )];
102
-
103
- if (property_exists ($ this , 'paginationResolver ' )) {
104
- $ resolver = $ this ->paginationResolver ;
105
- $ reflection = new ReflectionFunction ($ resolver );
106
-
107
- if ($ reflection ->getNumberOfRequiredParameters () > 0 ) {
108
- $ parameters = array_merge_recursive ($ parameters , $ this ->buildPaginatableParameters ());
109
- }
110
- }
89
+ $ parameters = [
90
+ $ this ->buildIncludeParameter ($ collection ),
91
+ ...$ this ->buildFilterParameters ($ collection ),
92
+ ...$ this ->buildPaginatableParameters (),
93
+ ];
111
94
112
95
return array_values (array_filter ($ parameters ));
113
96
}
@@ -138,26 +121,57 @@ private function buildIncludeParameter(Resource $resource): array
138
121
];
139
122
}
140
123
141
- private function buildPaginatableParameters (): array
124
+
125
+ private function buildFilterParameters (Resource $ resource ): array
142
126
{
143
- return [
144
- [
145
- 'name ' => 'page[limit] ' ,
146
- 'in ' => 'query ' ,
147
- 'description ' => "The limit pagination field. " ,
148
- 'schema ' => [
149
- 'type ' => 'number ' ,
150
- ],
151
- ],
152
- [
153
- 'name ' => 'page[offset] ' ,
127
+ if (!$ this instanceof Listable) {
128
+ return [];
129
+ }
130
+
131
+ return array_map (function (Filter $ filter ) {
132
+ return [
133
+ 'name ' => "filter[ {$ filter ->name }] " ,
154
134
'in ' => 'query ' ,
155
- 'description ' => " The offset pagination field. " ,
135
+ 'description ' => $ filter -> getDescription () ,
156
136
'schema ' => [
157
- 'type ' => 'number ' ,
137
+ 'type ' => 'string ' ,
158
138
],
159
- ],
160
- ];
139
+ ];
140
+ }, $ resource ->filters ());
141
+ }
142
+
143
+ /**
144
+ * @throws ReflectionException
145
+ */
146
+ private function buildPaginatableParameters (): array
147
+ {
148
+ if (property_exists ($ this , 'paginationResolver ' )) {
149
+ $ resolver = $ this ->paginationResolver ;
150
+ $ reflection = new ReflectionFunction ($ resolver );
151
+
152
+ if ($ reflection ->getNumberOfRequiredParameters () > 0 ) {
153
+ return [
154
+ [
155
+ 'name ' => 'page[limit] ' ,
156
+ 'in ' => 'query ' ,
157
+ 'description ' => "The limit pagination field. " ,
158
+ 'schema ' => [
159
+ 'type ' => 'number ' ,
160
+ ],
161
+ ],
162
+ [
163
+ 'name ' => 'page[offset] ' ,
164
+ 'in ' => 'query ' ,
165
+ 'description ' => "The offset pagination field. " ,
166
+ 'schema ' => [
167
+ 'type ' => 'number ' ,
168
+ ],
169
+ ],
170
+ ];
171
+ }
172
+ }
173
+
174
+ return [];
161
175
}
162
176
163
177
public function buildBadRequestErrorResponse (): array
0 commit comments