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