forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiController.php
More file actions
60 lines (52 loc) · 1.3 KB
/
ApiController.php
File metadata and controls
60 lines (52 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace App\Http\Controllers\Api\V1;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Log;
abstract class ApiController
{
/**
* Send a response.
*
* @param mixed $data
* @param array $filters
* @param string $message
* @param int $code
* @return \Illuminate\Http\JsonResponse
*/
public static function sendResponse($data, $filters = [], $message = 'ok', $code = 200)
{
$response = array_filter([
'data' => $data,
'filters' => $filters,
'message' => $message,
]);
if (! empty($message)) {
$response['message'] = $message;
}
return response()->json(
data: $response,
status: $code,
);
}
/**
* Throw an exception.
*
* @param \Exception $e
* @param int $code
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
public static function throw($e, $code = 500)
{
Log::info($e);
$response = [
'message' => $e->getMessage(),
];
throw new HttpResponseException(
response: response()->json(
data: $response,
status: $code,
),
);
}
}