1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Sunrise\Stream\StreamFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Import functions |
23
|
|
|
*/ |
24
|
|
|
use function json_encode; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ResponseFactory |
28
|
|
|
* |
29
|
|
|
* @link https://www.php-fig.org/psr/psr-17/ |
30
|
|
|
*/ |
31
|
|
|
class ResponseFactory implements ResponseFactoryInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
1 |
|
public function createResponse(int $code = 200, string $reasonPhrase = '') : ResponseInterface |
38
|
|
|
{ |
39
|
1 |
|
$body = (new StreamFactory)->createStream(); |
40
|
|
|
|
41
|
1 |
|
return (new Response) |
42
|
1 |
|
->withStatus($code, $reasonPhrase) |
43
|
1 |
|
->withBody($body); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates a HTML response instance |
48
|
|
|
* |
49
|
|
|
* @param int $status |
50
|
|
|
* @param mixed $content |
51
|
|
|
* |
52
|
|
|
* @return ResponseInterface |
53
|
|
|
*/ |
54
|
1 |
|
public function createHtmlResponse(int $status, $content) : ResponseInterface |
55
|
|
|
{ |
56
|
1 |
|
$content = (string) $content; |
57
|
|
|
|
58
|
1 |
|
$body = (new StreamFactory)->createStream(); |
59
|
1 |
|
$body->write($content); |
60
|
|
|
|
61
|
1 |
|
return (new Response) |
62
|
1 |
|
->withStatus($status) |
63
|
1 |
|
->withHeader('Content-Type', 'text/html; charset=utf-8') |
64
|
1 |
|
->withBody($body); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates a JSON response instance |
69
|
|
|
* |
70
|
|
|
* @param int $status |
71
|
|
|
* @param mixed $payload |
72
|
|
|
* @param int $options |
73
|
|
|
* @param int $depth |
74
|
|
|
* |
75
|
|
|
* @return ResponseInterface |
76
|
|
|
* |
77
|
|
|
* @throws Exception\JsonException |
78
|
|
|
*/ |
79
|
2 |
|
public function createJsonResponse(int $status, $payload, int $options = 0, int $depth = 512) : ResponseInterface |
80
|
|
|
{ |
81
|
|
|
// clears a previous error... |
82
|
2 |
|
json_encode(null); |
83
|
|
|
|
84
|
2 |
|
$content = json_encode($payload, $options, $depth); |
85
|
|
|
|
86
|
2 |
|
Exception\JsonException::assert(); |
87
|
|
|
|
88
|
1 |
|
$body = (new StreamFactory)->createStream(); |
89
|
1 |
|
$body->write($content); |
90
|
|
|
|
91
|
1 |
|
return (new Response) |
92
|
1 |
|
->withStatus($status) |
93
|
1 |
|
->withHeader('Content-Type', 'application/json; charset=utf-8') |
94
|
1 |
|
->withBody($body); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|