1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Venta\Http; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Venta\Contracts\Http\ResponseFactory as ResponseFactoryContract; |
7
|
|
|
use Zend\Diactoros\Response; |
8
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
9
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
10
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
11
|
|
|
use Zend\Diactoros\Response\RedirectResponse; |
12
|
|
|
use Zend\Diactoros\Response\TextResponse; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ResponseFactory |
16
|
|
|
* |
17
|
|
|
* @package Venta\Http |
18
|
|
|
*/ |
19
|
|
|
final class ResponseFactory implements ResponseFactoryContract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritDoc |
23
|
|
|
*/ |
24
|
2 |
|
public function createEmptyResponse($status = 204, array $headers = []): ResponseInterface |
25
|
|
|
{ |
26
|
2 |
|
return new EmptyResponse($status, $headers); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
1 |
|
public function createHtmlResponse(string $html, int $status = 200, array $headers = []): ResponseInterface |
33
|
|
|
{ |
34
|
1 |
|
return new HtmlResponse($html, $status, $headers); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function createJsonResponse( |
41
|
|
|
$data, |
42
|
|
|
int $status = 200, |
43
|
|
|
array $headers = [], |
44
|
|
|
int $encodingOptions = ResponseFactoryContract::JSON_FLAG |
45
|
|
|
): ResponseInterface { |
46
|
|
|
|
47
|
1 |
|
return new JsonResponse($data, $status, $headers, $encodingOptions); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
2 |
|
public function createRedirectResponse($uri, int $status = 302, array $headers = []): ResponseInterface |
54
|
|
|
{ |
55
|
2 |
|
return new RedirectResponse($uri, $status, $headers); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
|
public function createResponse($body = 'php://memory', int $status = 200, array $headers = []): ResponseInterface |
62
|
|
|
{ |
63
|
1 |
|
return new Response($body, $status, $headers); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
1 |
|
public function createTextResponse($text, $status = 200, array $headers = []): ResponseInterface |
70
|
|
|
{ |
71
|
1 |
|
return new TextResponse($text, $status, $headers); |
72
|
|
|
} |
73
|
|
|
} |