1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<?php
namespace Http\Message\Builder;
use Psr\Http\Message\ResponseInterface;
/**
* Fills response object with values.
*/
class ResponseBuilder
{
/**
* The response to be built.
*
* @var ResponseInterface
*/
protected $response;
/**
* Create builder for the given response.
*
* @param ResponseInterface $response
*/
public function __construct(ResponseInterface $response)
{
$this->response = $response;
}
/**
* Return response.
*
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
/**
* Add headers represented by an array of header lines.
*
* @param string[] $headers Response headers as array of header lines.
*
* @return $this
*
* @throws \UnexpectedValueException For invalid header values.
* @throws \InvalidArgumentException For invalid status code arguments.
*/
public function setHeadersFromArray(array $headers)
{
$status = array_shift($headers);
$this->setStatus($status);
foreach ($headers as $headerLine) {
$headerLine = trim($headerLine);
if ('' === $headerLine) {
continue;
}
$this->addHeader($headerLine);
}
return $this;
}
/**
* Add headers represented by a single string.
*
* @param string $headers Response headers as single string.
*
* @return $this
*
* @throws \InvalidArgumentException if $headers is not a string on object with __toString()
* @throws \UnexpectedValueException For invalid header values.
*/
public function setHeadersFromString($headers)
{
if (!(is_string($headers)
|| (is_object($headers) && method_exists($headers, '__toString')))
) {
throw new \InvalidArgumentException(
sprintf(
'%s expects parameter 1 to be a string, %s given',
__METHOD__,
is_object($headers) ? get_class($headers) : gettype($headers)
)
);
}
$this->setHeadersFromArray(explode("\r\n", $headers));
return $this;
}
/**
* Set response status from a status string.
*
* @param string $statusLine Response status as a string.
*
* @return $this
*
* @throws \InvalidArgumentException For invalid status line.
*/
public function setStatus($statusLine)
{
$parts = explode(' ', $statusLine, 3);
if (count($parts) < 2 || strpos(strtolower($parts[0]), 'http/') !== 0) {
throw new \InvalidArgumentException(
sprintf('"%s" is not a valid HTTP status line', $statusLine)
);
}
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
$this->response = $this->response
->withStatus((int) $parts[1], $reasonPhrase)
->withProtocolVersion(substr($parts[0], 5));
return $this;
}
/**
* Add header represented by a string.
*
* @param string $headerLine Response header as a string.
*
* @return $this
*
* @throws \InvalidArgumentException For invalid header names or values.
*/
public function addHeader($headerLine)
{
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new \InvalidArgumentException(
sprintf('"%s" is not a valid HTTP header line', $headerLine)
);
}
$name = trim($parts[0]);
$value = trim($parts[1]);
if ($this->response->hasHeader($name)) {
$this->response = $this->response->withAddedHeader($name, $value);
} else {
$this->response = $this->response->withHeader($name, $value);
}
return $this;
}
}
|