18
18
* @Target("METHOD")
19
19
* @Attributes({
20
20
* @Attribute("status",type="integer"),
21
+ * @Attribute("isSuccessful",type="boolean"),
21
22
* @Attribute("contentType",type="string"),
22
23
* @Attribute("description",type="string"),
23
24
* @Attribute("asList",type="boolean"),
@@ -30,6 +31,7 @@ class Response extends BaseAnnotation
30
31
public mixed $ content = null ;
31
32
public string $ contentType = 'application/json ' ;
32
33
public int $ status = 200 ;
34
+ public ?bool $ isSuccessful = null ;
33
35
public ?string $ description = null ;
34
36
public bool $ asList = false ;
35
37
public bool $ asPagedList = false ;
@@ -179,12 +181,12 @@ public function __toString()
179
181
protected function wrapInDefaultResponse (mixed $ content = null ): mixed
180
182
{
181
183
$ content = $ content ?? $ this ->content ;
182
- $ responseData = static ::getDefaultResponse ($ this ->contentType , $ this ->status );
184
+ $ responseData = static ::getDefaultResponse ($ this ->contentType , $ this ->status , $ this -> isSuccessful );
183
185
if ($ responseData === null ) {
184
186
return $ content ;
185
187
}
186
188
[$ responseRaw , $ resultKey ] = array_values ($ responseData );
187
- if (($ this ->asPagedList || $ this ->asCursorPagedList ) && static ::isSuccessStatus ($ this ->status )) {
189
+ if (($ this ->asPagedList || $ this ->asCursorPagedList ) && ( $ this -> isSuccessful ?? static ::isSuccessStatus ($ this ->status ) )) {
188
190
if ($ this ->asPagedList ) {
189
191
$ responseRaw ['pagination ' ] = static ::getPagerExample ();
190
192
} elseif ($ this ->asCursorPagedList ) {
@@ -200,13 +202,14 @@ protected function wrapInDefaultResponse(mixed $content = null): mixed
200
202
/**
201
203
* Get default response by content type [response, result_array_key].
202
204
*
203
- * @param string $contentType
204
- * @param int $status
205
+ * @param string $contentType
206
+ * @param int $status
207
+ * @param bool|null $isSuccessful Override check by status for success/error response
205
208
* @return mixed|null
206
209
*/
207
- protected static function getDefaultResponse (string $ contentType , int $ status = 200 ): mixed
210
+ protected static function getDefaultResponse (string $ contentType , int $ status = 200 , ? bool $ isSuccessful = null ): mixed
208
211
{
209
- $ key = static ::isSuccessStatus ($ status ) ? 'ok ' : 'error ' ;
212
+ $ key = ( $ isSuccessful ?? static ::isSuccessStatus ($ status) ) ? 'ok ' : 'error ' ;
210
213
$ responses = [
211
214
'application/json ' => [
212
215
'ok ' => [
0 commit comments