1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 7/28/15 |
6
|
|
|
* Time: 1:13 AM. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Api\Http\Message; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\StreamInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractResponse. |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractResponse implements ResponseInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $httpCode = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Zend\Diactoros\Response |
28
|
|
|
*/ |
29
|
|
|
protected $response; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $headers = [ |
35
|
|
|
'Content-type' => 'application/json; charset=utf-8', |
36
|
|
|
'Cache-Control' => 'private, max-age=0, must-revalidate', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $json |
41
|
|
|
*/ |
42
|
|
|
public function __construct($json) |
43
|
|
|
{ |
44
|
|
|
$this->response = self::instance($json, $this->httpCode, $this->headers); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $body |
49
|
|
|
* @param int $status |
50
|
|
|
* @param array $headers |
51
|
|
|
* |
52
|
|
|
* @return AbstractResponse |
53
|
|
|
*/ |
54
|
|
|
protected function instance($body, $status = 200, array $headers = []) |
55
|
|
|
{ |
56
|
|
|
$response = new \Zend\Diactoros\Response('php://memory', $status, $headers); |
57
|
|
|
$response->getBody()->write($body); |
58
|
|
|
|
59
|
|
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getStatusCode() |
66
|
|
|
{ |
67
|
|
|
return $this->response->getStatusCode(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getReasonPhrase() |
74
|
|
|
{ |
75
|
|
|
return $this->response->getReasonPhrase(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getProtocolVersion() |
82
|
|
|
{ |
83
|
|
|
return $this->response->getProtocolVersion(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function withProtocolVersion($version) |
90
|
|
|
{ |
91
|
|
|
return $this->response->withProtocolVersion($version); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getHeaders() |
98
|
|
|
{ |
99
|
|
|
return $this->response->getHeaders(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function hasHeader($header) |
106
|
|
|
{ |
107
|
|
|
return $this->response->hasHeader($header); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getHeader($header) |
114
|
|
|
{ |
115
|
|
|
return $this->response->getHeader($header); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getHeaderLine($name) |
122
|
|
|
{ |
123
|
|
|
return $this->response->getHeaderLine($name); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function withHeader($header, $value) |
130
|
|
|
{ |
131
|
|
|
return $this->response->withHeader($header, $value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function withAddedHeader($header, $value) |
138
|
|
|
{ |
139
|
|
|
return $this->response->withAddedHeader($header, $value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function withoutHeader($header) |
146
|
|
|
{ |
147
|
|
|
return $this->response->withoutHeader($header); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getBody() |
154
|
|
|
{ |
155
|
|
|
return $this->response->getBody(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function withBody(StreamInterface $body) |
162
|
|
|
{ |
163
|
|
|
return $this->response->withBody($body); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function withStatus($code, $reasonPhrase = '') |
170
|
|
|
{ |
171
|
|
|
return $this->response->withStatus($code, $reasonPhrase); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|