You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
<?php |
|
|
|
namespace App\Helpers; |
|
|
|
/** |
|
* Format response. |
|
*/ |
|
class ResponseFormatter |
|
{ |
|
/** |
|
* API Response |
|
* |
|
* @var array |
|
*/ |
|
protected static $response = [ |
|
'meta' => [ |
|
'code' => 200, |
|
'status' => 'success', |
|
'message' => null, |
|
], |
|
'data' => null, |
|
]; |
|
|
|
/** |
|
* Give success response. |
|
*/ |
|
public static function success($data = null, $message = null) |
|
{ |
|
self::$response['meta']['message'] = $message; |
|
self::$response['data'] = $data; |
|
|
|
return response()->json(self::$response, self::$response['meta']['code']); |
|
} |
|
|
|
/** |
|
* Give error response. |
|
*/ |
|
public static function error($data = null, $message = null, $code = 400) |
|
{ |
|
self::$response['meta']['status'] = 'error'; |
|
self::$response['meta']['code'] = $code; |
|
self::$response['meta']['message'] = $message; |
|
self::$response['data'] = $data; |
|
|
|
return response()->json(self::$response, self::$response['meta']['code']); |
|
} |
|
}
|
|
|