1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Venta\Adr; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
use Venta\Contracts\Adr\Responder as ResponderContract; |
8
|
|
|
use Venta\Contracts\Http\ResponseFactory; |
9
|
|
|
use Venta\Contracts\Http\ResponseFactoryAware; |
10
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractResponder |
14
|
|
|
* |
15
|
|
|
* @package Venta\Adr |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractResponder implements ResponderContract, ResponseFactoryAware |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ResponseFactory |
21
|
|
|
*/ |
22
|
|
|
private $responseFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
*/ |
27
|
|
|
public function setResponseFactory(ResponseFactory $factory) |
28
|
|
|
{ |
29
|
|
|
$this->responseFactory = $factory; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function empty($status = 204, array $headers = []) |
33
|
|
|
{ |
34
|
|
|
return $this->responseFactory->createEmptyResponse($status, $headers); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates html response. |
39
|
|
|
* |
40
|
|
|
* @param string $html |
41
|
|
|
* @param int $status |
42
|
|
|
* @param array $headers |
43
|
|
|
* @return ResponseInterface |
44
|
|
|
*/ |
45
|
|
|
protected function html(string $html, int $status = 200, array $headers = []): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
return $this->responseFactory->createHtmlResponse($html, $status, $headers); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates HTTP response with JSON content type header and JSON encoded $data. |
52
|
|
|
* |
53
|
|
|
* @param mixed $data Data to convert to JSON. |
54
|
|
|
* @param int $status Integer status code for the response; 200 by default. |
55
|
|
|
* @param array $headers Array of headers to use at initialization. |
56
|
|
|
* @param int $encodingOptions |
57
|
|
|
* @return ResponseInterface |
58
|
|
|
*/ |
59
|
|
|
protected function json( |
60
|
|
|
$data, |
61
|
|
|
int $status = 200, |
62
|
|
|
array $headers = [], |
63
|
|
|
int $encodingOptions = JsonResponse::DEFAULT_JSON_FLAGS |
64
|
|
|
): ResponseInterface { |
65
|
|
|
return $this->responseFactory->createJsonResponse($data, $status, $headers, $encodingOptions); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates HTTP redirect response with $uri used as Location header. |
70
|
|
|
* |
71
|
|
|
* @param string|UriInterface $uri |
72
|
|
|
* @param int $status |
73
|
|
|
* @param array $headers |
74
|
|
|
* @return ResponseInterface |
75
|
|
|
*/ |
76
|
|
|
protected function redirect($uri, $status = 302, array $headers = []): ResponseInterface |
77
|
|
|
{ |
78
|
|
|
return $this->responseFactory->createRedirectResponse($uri, $status, $headers); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Creates HTTP response. |
83
|
|
|
* |
84
|
|
|
* @param string $bodyStream Stream to use as response body. |
85
|
|
|
* @param int $status |
86
|
|
|
* @param array $headers |
87
|
|
|
* @return ResponseInterface |
88
|
|
|
*/ |
89
|
|
|
protected function response($bodyStream = 'php://memory', int $status = 200, array $headers = []): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
return $this->responseFactory->createResponse($bodyStream, $status, $headers); |
92
|
|
|
} |
93
|
|
|
} |