1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ilex\CustomResponse; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\UriInterface; |
10
|
|
|
|
11
|
|
|
final class CustomResponseFactory implements CustomResponseFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ResponseFactoryInterface |
15
|
|
|
*/ |
16
|
|
|
private $responseFactory; |
17
|
|
|
|
18
|
1 |
|
public function __construct(ResponseFactoryInterface $responseFactory) |
19
|
|
|
{ |
20
|
1 |
|
$this->responseFactory = $responseFactory; |
21
|
1 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a JSON response with the given data. |
25
|
|
|
* |
26
|
|
|
* Default JSON encoding is performed with the following options, which |
27
|
|
|
* produces RFC4627-compliant JSON, capable of embedding into HTML. |
28
|
|
|
* |
29
|
|
|
* - JSON_HEX_TAG |
30
|
|
|
* - JSON_HEX_APOS |
31
|
|
|
* - JSON_HEX_AMP |
32
|
|
|
* - JSON_HEX_QUOT |
33
|
|
|
* - JSON_UNESCAPED_SLASHES |
34
|
|
|
* |
35
|
|
|
* @param array $data Data to convert to JSON. |
36
|
|
|
* @param int $status Integer status code for the response; 200 by default. |
37
|
|
|
* @param int $encodingOptions JSON encoding options to use. |
38
|
|
|
* |
39
|
|
|
* @return ResponseInterface |
40
|
|
|
*/ |
41
|
4 |
|
public function createJsonResponseFromArray( |
42
|
|
|
array $data, |
43
|
|
|
int $status = 200, |
44
|
|
|
int $encodingOptions = CustomResponseFactoryInterface::DEFAULT_JSON_FLAGS |
45
|
|
|
): ResponseInterface { |
46
|
4 |
|
$response = $this->responseFactory->createResponse($status) |
47
|
4 |
|
->withHeader('content-type', 'application/json'); |
48
|
4 |
|
$json = \json_encode($data, $encodingOptions); |
49
|
4 |
|
if (false === $json) { |
50
|
2 |
|
throw new \RuntimeException('json_encode have error'); |
51
|
|
|
} |
52
|
2 |
|
$response->getBody()->write($json); |
53
|
2 |
|
return $response; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 201 Created responses are often empty, and only include a Link or |
58
|
|
|
* Location header pointing to the newly created resource. |
59
|
|
|
* 202 Accepted responses are typically empty, indicating that the new |
60
|
|
|
* entity has been received, but not yet processed. |
61
|
|
|
* 204 No Content responses are, by definition, empty, and often used as a |
62
|
|
|
* success response when deleting an entity. |
63
|
|
|
* |
64
|
|
|
* @param int $status |
65
|
|
|
* |
66
|
|
|
* @return ResponseInterface |
67
|
|
|
*/ |
68
|
2 |
|
public function createEmptyResponse( |
69
|
|
|
int $status = CustomResponseFactoryInterface::DEFAULT_STATUS_EMPTY |
70
|
|
|
): ResponseInterface { |
71
|
2 |
|
return $this->responseFactory->createResponse($status); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $uri |
76
|
|
|
* @param int $status |
77
|
|
|
* |
78
|
|
|
* @return ResponseInterface |
79
|
|
|
*/ |
80
|
4 |
|
public function createRedirectResponseFromString( |
81
|
|
|
string $uri, |
82
|
|
|
int $status = CustomResponseFactoryInterface::DEFAULT_STATUS_REDIRECT |
83
|
|
|
): ResponseInterface { |
84
|
4 |
|
return $this->responseFactory->createResponse($status) |
85
|
4 |
|
->withHeader('location', $uri); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param UriInterface $uri |
90
|
|
|
* @param int $status |
91
|
|
|
* |
92
|
|
|
* @return ResponseInterface |
93
|
|
|
*/ |
94
|
2 |
|
public function createRedirectResponseFromUri( |
95
|
|
|
UriInterface $uri, |
96
|
|
|
int $status = CustomResponseFactoryInterface::DEFAULT_STATUS_REDIRECT |
97
|
|
|
): ResponseInterface { |
98
|
2 |
|
return $this->createRedirectResponseFromString((string)$uri, $status); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|