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