|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mosaic\Http\Adapters\Psr7; |
|
4
|
|
|
|
|
5
|
|
|
use Mosaic\Common\Arrayable; |
|
6
|
|
|
use Mosaic\Http\Response as ResponseContract; |
|
7
|
|
|
use Mosaic\Http\ResponseFactory as ResponseFactoryContract; |
|
8
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
|
9
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
10
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
11
|
|
|
|
|
12
|
|
|
class ResponseFactory implements ResponseFactoryContract |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $content |
|
16
|
|
|
* @param int $status |
|
17
|
|
|
* @param array $headers |
|
18
|
|
|
* |
|
19
|
|
|
* @return ResponseContract |
|
20
|
|
|
*/ |
|
21
|
6 |
|
public function html(string $content = null, int $status = 200, array $headers = []) |
|
22
|
|
|
{ |
|
23
|
6 |
|
if ($content === null || $content == '') { |
|
24
|
1 |
|
return $this->emptyResponse($status, $headers); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
5 |
|
return new Response( |
|
28
|
5 |
|
new HtmlResponse($content, $status, $headers) |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param mixed $content |
|
34
|
|
|
* @param int $status |
|
35
|
|
|
* @param array $headers |
|
36
|
|
|
* |
|
37
|
|
|
* @return ResponseContract |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function make($content = '', int $status = 200, array $headers = []) |
|
40
|
|
|
{ |
|
41
|
6 |
|
if ($content instanceof ResponseContract) { |
|
42
|
1 |
|
return $content->toPsr7(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
if (is_array($content) || $content instanceof Arrayable) { |
|
46
|
2 |
|
return $this->json($content, $status, $headers); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
return $this->html((string) $content, $status, $headers); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array|Arrayable $content |
|
54
|
|
|
* @param int $status |
|
55
|
|
|
* @param array $headers |
|
56
|
|
|
* @param int $option |
|
57
|
|
|
* |
|
58
|
|
|
* @return ResponseContract |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function json($content = [], int $status = 200, array $headers = [], int $option = 79) |
|
61
|
|
|
{ |
|
62
|
3 |
|
if ($content instanceof Arrayable) { |
|
63
|
1 |
|
$content = $content->toArray(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
return new Response( |
|
67
|
3 |
|
new JsonResponse($content, $status, $headers, $option) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $status |
|
73
|
|
|
* @param $headers |
|
74
|
|
|
* |
|
75
|
|
|
* @return ResponseContract |
|
76
|
|
|
*/ |
|
77
|
1 |
|
private function emptyResponse(int $status = 200, array $headers = []) |
|
78
|
|
|
{ |
|
79
|
1 |
|
return new Response( |
|
80
|
1 |
|
new EmptyResponse($status, $headers) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|