18
18
19
19
use \Psr \Log \LoggerAwareTrait ;
20
20
use \Psr \Log \LoggerAwareInterface ;
21
+ use \Neomerx \JsonApi \Document \Error ;
22
+ use \Neomerx \JsonApi \I18n \Translator as T ;
21
23
use \Psr \Http \Message \ServerRequestInterface ;
22
24
use \Neomerx \JsonApi \Exceptions \JsonApiException as E ;
23
25
use \Neomerx \JsonApi \Contracts \Http \HttpFactoryInterface ;
@@ -89,7 +91,10 @@ private function getFieldSets(array $parameters)
89
91
if (empty ($ fieldSets ) === false && is_array ($ fieldSets )) {
90
92
foreach ($ fieldSets as $ type => $ fields ) {
91
93
// We expect fields to be comma separated or empty strings. Multi-dimension arrays are not allowed.
92
- is_string ($ fields ) ?: E::throwException (new E ([], E::HTTP_CODE_BAD_REQUEST ));
94
+ if (is_string ($ fields ) === false ) {
95
+ $ detail = T::t (self ::PARAM_FIELDS . ' parameter values should be comma separated strings. ' );
96
+ throw new E ($ this ->createInvalidQueryErrors ($ detail ), E::HTTP_CODE_BAD_REQUEST );
97
+ }
93
98
$ result [$ type ] = (empty ($ fields ) === true ? [] : explode (', ' , $ fields ));
94
99
}
95
100
} else {
@@ -112,11 +117,16 @@ protected function getSortParameters(array $parameters)
112
117
$ sortParam = $ this ->getStringParamOrNull ($ parameters , self ::PARAM_SORT );
113
118
if ($ sortParam !== null ) {
114
119
foreach (explode (', ' , $ sortParam ) as $ param ) {
115
- $ isDesc = false ;
116
- empty ($ param ) === false ?
117
- $ isDesc = ($ param [0 ] === '- ' ) : E::throwException (new E ([], E::HTTP_CODE_BAD_REQUEST ));
120
+ if (empty ($ param ) === true ) {
121
+ $ detail = T::t ('Parameter ' . self ::PARAM_SORT . ' should have valid value specified. ' );
122
+ throw new E ($ this ->createInvalidQueryErrors ($ detail ), E::HTTP_CODE_BAD_REQUEST );
123
+ }
124
+ $ isDesc = $ isDesc = ($ param [0 ] === '- ' );
118
125
$ sortField = ltrim ($ param , '+- ' );
119
- empty ($ sortField ) === false ?: E::throwException (new E ([], E::HTTP_CODE_BAD_REQUEST ));
126
+ if (empty ($ sortField ) === true ) {
127
+ $ detail = T::t ('Parameter ' . self ::PARAM_SORT . ' should have valid name specified. ' );
128
+ throw new E ($ this ->createInvalidQueryErrors ($ detail ), E::HTTP_CODE_BAD_REQUEST );
129
+ }
120
130
$ sortParams [] = $ this ->factory ->createSortParam ($ sortField , $ isDesc === false );
121
131
}
122
132
}
@@ -173,8 +183,10 @@ private function getArrayParamOrNull(array $parameters, $name)
173
183
{
174
184
$ value = $ this ->getParamOrNull ($ parameters , $ name );
175
185
176
- $ isArrayOrNull = ($ value === null || is_array ($ value ) === true );
177
- $ isArrayOrNull === true ?: E::throwException (new E ([], E::HTTP_CODE_BAD_REQUEST ));
186
+ if ($ value !== null && is_array ($ value ) === false ) {
187
+ $ detail = T::t ('Value should be either an array or null. ' );
188
+ throw new E ($ this ->createParamErrors ($ name , $ detail ), E::HTTP_CODE_BAD_REQUEST );
189
+ }
178
190
179
191
return $ value ;
180
192
}
@@ -191,8 +203,10 @@ private function getStringParamOrNull(array $parameters, $name)
191
203
{
192
204
$ value = $ this ->getParamOrNull ($ parameters , $ name );
193
205
194
- $ isStringOrNull = ($ value === null || is_string ($ value ) === true );
195
- $ isStringOrNull === true ?: E::throwException (new E ([], E::HTTP_CODE_BAD_REQUEST ));
206
+ if ($ value !== null && is_string ($ value ) === false ) {
207
+ $ detail = T::t ('Value should be either a string or null. ' );
208
+ throw new E ($ this ->createParamErrors ($ name , $ detail ), E::HTTP_CODE_BAD_REQUEST );
209
+ }
196
210
197
211
return $ value ;
198
212
}
@@ -207,4 +221,46 @@ private function getParamOrNull(array $parameters, $name)
207
221
{
208
222
return isset ($ parameters [$ name ]) === true ? $ parameters [$ name ] : null ;
209
223
}
224
+
225
+ /**
226
+ * @param string $detail
227
+ *
228
+ * @return Error[]
229
+ *
230
+ * @SuppressWarnings(PHPMD.StaticAccess)
231
+ */
232
+ protected function createInvalidQueryErrors ($ detail )
233
+ {
234
+ // NOTE: external libraries might expect this method to exist and have certain signature
235
+ // @see https://github.com/neomerx/json-api/issues/185#issuecomment-329135390
236
+
237
+ $ title = T::t ('Invalid query. ' );
238
+
239
+ return [
240
+ new Error (null , null , null , null , $ title , $ detail ),
241
+ ];
242
+ }
243
+
244
+ /**
245
+ * @param string $name
246
+ * @param string $detail
247
+ *
248
+ * @return Error[]
249
+ *
250
+ * @SuppressWarnings(PHPMD.StaticAccess)
251
+ */
252
+ protected function createParamErrors ($ name , $ detail )
253
+ {
254
+ // NOTE: external libraries might expect this method to exist and have certain signature
255
+ // @see https://github.com/neomerx/json-api/issues/185#issuecomment-329135390
256
+
257
+ $ title = T::t ('Invalid query parameter. ' );
258
+ $ source = [
259
+ Error::SOURCE_PARAMETER => $ name ,
260
+ ];
261
+
262
+ return [
263
+ new Error (null , null , null , null , $ title , $ detail , $ source ),
264
+ ];
265
+ }
210
266
}
0 commit comments