Skip to content

Commit a814fba

Browse files
committed
Merge pull request #40 from mekras/response_builder_refactoring
Single header line parsing extracted to a separate method in ResponseBuilder
2 parents a97bd29 + 0d565b2 commit a814fba

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

src/Builder/ResponseBuilder.php

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,16 @@ public function getResponse()
4848
*/
4949
public function setHeadersFromArray(array $headers)
5050
{
51-
$statusLine = trim(array_shift($headers));
52-
$parts = explode(' ', $statusLine, 3);
53-
if (count($parts) < 2 || substr(strtolower($parts[0]), 0, 5) !== 'http/') {
54-
throw new \UnexpectedValueException(
55-
sprintf('"%s" is not a valid HTTP status line', $statusLine)
56-
);
57-
}
58-
59-
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
60-
$this->response = $this->response
61-
->withStatus((int) $parts[1], $reasonPhrase)
62-
->withProtocolVersion(substr($parts[0], 5));
51+
$status = array_shift($headers);
52+
$this->setStatus($status);
6353

6454
foreach ($headers as $headerLine) {
6555
$headerLine = trim($headerLine);
6656
if ('' === $headerLine) {
6757
continue;
6858
}
6959

70-
$parts = explode(':', $headerLine, 2);
71-
if (count($parts) !== 2) {
72-
throw new \UnexpectedValueException(
73-
sprintf('"%s" is not a valid HTTP header line', $headerLine)
74-
);
75-
}
76-
$name = trim(urldecode($parts[0]));
77-
$value = trim(urldecode($parts[1]));
78-
if ($this->response->hasHeader($name)) {
79-
$this->response = $this->response->withAddedHeader($name, $value);
80-
} else {
81-
$this->response = $this->response->withHeader($name, $value);
82-
}
60+
$this->addHeader($headerLine);
8361
}
8462

8563
return $this;
@@ -113,4 +91,58 @@ public function setHeadersFromString($headers)
11391

11492
return $this;
11593
}
94+
95+
/**
96+
* Set response status from a status string.
97+
*
98+
* @param string $statusLine Response status as a string.
99+
*
100+
* @return $this
101+
*
102+
* @throws \InvalidArgumentException For invalid status line.
103+
*/
104+
public function setStatus($statusLine)
105+
{
106+
$parts = explode(' ', $statusLine, 3);
107+
if (count($parts) < 2 || strpos(strtolower($parts[0]), 'http/') !== 0) {
108+
throw new \InvalidArgumentException(
109+
sprintf('"%s" is not a valid HTTP status line', $statusLine)
110+
);
111+
}
112+
113+
$reasonPhrase = count($parts) > 2 ? $parts[2] : '';
114+
$this->response = $this->response
115+
->withStatus((int) $parts[1], $reasonPhrase)
116+
->withProtocolVersion(substr($parts[0], 5));
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* Add header represented by a string.
123+
*
124+
* @param string $headerLine Response header as a string.
125+
*
126+
* @return $this
127+
*
128+
* @throws \InvalidArgumentException For invalid header names or values.
129+
*/
130+
public function addHeader($headerLine)
131+
{
132+
$parts = explode(':', $headerLine, 2);
133+
if (count($parts) !== 2) {
134+
throw new \InvalidArgumentException(
135+
sprintf('"%s" is not a valid HTTP header line', $headerLine)
136+
);
137+
}
138+
$name = trim(urldecode($parts[0]));
139+
$value = trim(urldecode($parts[1]));
140+
if ($this->response->hasHeader($name)) {
141+
$this->response = $this->response->withAddedHeader($name, $value);
142+
} else {
143+
$this->response = $this->response->withHeader($name, $value);
144+
}
145+
146+
return $this;
147+
}
116148
}

0 commit comments

Comments
 (0)