-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
53 lines (49 loc) · 1.53 KB
/
helpers.php
File metadata and controls
53 lines (49 loc) · 1.53 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
<?php
use Illuminate\Container\Container;
use Illuminate\Http\Response;
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
if (!function_exists('illuminated_json_api_encoder')) {
/**
* @return EncoderInterface
*/
function illuminated_json_api_encoder(): EncoderInterface
{
return Container::getInstance()->get(EncoderInterface::class);
}
}
if (!function_exists('illuminated_json_api_response')) {
/**
* @param object|array|Iterator|null $resource
* @param int $status
* @return Response
*/
function illuminated_json_api_response($resource, int $status = null): Response
{
return new Response(
illuminated_json_api_encoder()->encodeData($resource),
$status ?? Response::HTTP_OK,
[
'Content-Type' => MediaTypeInterface::JSON_API_MEDIA_TYPE,
]
);
}
}
if (!function_exists('illuminated_json_api_error_response')) {
/**
* @param ErrorInterface $error
* @param int $status
* @return Response
*/
function illuminated_json_api_error_response(ErrorInterface $error, int $status = null): Response
{
return new Response(
illuminated_json_api_encoder()->encodeError($error),
$status ?? Response::HTTP_BAD_REQUEST,
[
'Content-Type' => MediaTypeInterface::JSON_API_MEDIA_TYPE,
]
);
}
}