Message constructor accepts $headers values as string or string[]

This commit is contained in:
PJ Dietz 2020-08-16 08:49:47 -04:00
parent 79d23e37a4
commit 20012dc671
3 changed files with 20 additions and 15 deletions

View File

@ -22,13 +22,13 @@ abstract class Message implements MessageInterface
* Create a new Message, optionally with headers and a body. * Create a new Message, optionally with headers and a body.
* *
* $headers is an optional associative array with header field names as * $headers is an optional associative array with header field names as
* (string) keys and lists of header field values (string[]) as values. * string keys and values as either string or string[].
* *
* If no StreamInterface is provided for $body, the instance will create * If no StreamInterface is provided for $body, the instance will create
* a NullStream instance for the message body. * a NullStream instance for the message body.
* *
* @param array $headers Associative array with header field names as * @param array $headers Associative array with header field names as
* (string) keys and lists of header field values (string[]) as values. * keys and values as string|string[]
* @param StreamInterface|null $body A stream representation of the message * @param StreamInterface|null $body A stream representation of the message
* entity body * entity body
*/ */
@ -37,19 +37,17 @@ abstract class Message implements MessageInterface
?StreamInterface $body = null ?StreamInterface $body = null
) { ) {
$this->headers = new HeaderCollection(); $this->headers = new HeaderCollection();
if ($headers) {
foreach ($headers as $name => $values) { foreach ($headers as $name => $values) {
if (is_string($values)) {
$values = [$values];
}
foreach ($values as $value) { foreach ($values as $value) {
$this->headers[$name] = $value; $this->headers[$name] = $value;
} }
} }
}
if ($body !== null) { $this->body = $body ?? new Stream('');
$this->body = $body;
} else {
$this->body = new Stream('');
}
} }
public function __clone() public function __clone()

View File

@ -34,7 +34,7 @@ class Request extends Message implements RequestInterface
* Create a new Request. * Create a new Request.
* *
* $headers is an optional associative array with header field names as * $headers is an optional associative array with header field names as
* (string) keys and lists of header field values (string[]) as values. * string keys and values as either string or string[].
* *
* If no StreamInterface is provided for $body, the instance will create * If no StreamInterface is provided for $body, the instance will create
* a NullStream instance for the message body. * a NullStream instance for the message body.
@ -42,7 +42,7 @@ class Request extends Message implements RequestInterface
* @param string $method * @param string $method
* @param string|UriInterface $uri * @param string|UriInterface $uri
* @param array $headers Associative array with header field names as * @param array $headers Associative array with header field names as
* (string) keys and lists of header field values (string[]) as values. * keys and values as string|string[]
* @param StreamInterface|null $body A stream representation of the message * @param StreamInterface|null $body A stream representation of the message
* entity body * entity body
*/ */

View File

@ -7,7 +7,14 @@ use WellRESTed\Test\TestCase;
class MessageTest extends TestCase class MessageTest extends TestCase
{ {
public function testSetsHeadersOnConstruction(): void public function testSetsHeadersWithStringValueOnConstruction(): void
{
$headers = ['X-foo' => 'bar'];
$message = new Response(200, $headers);
$this->assertEquals(['bar'], $message->getHeader('X-foo'));
}
public function testSetsHeadersWithArrayValueOnConstruction(): void
{ {
$headers = ['X-foo' => ['bar', 'baz']]; $headers = ['X-foo' => ['bar', 'baz']];
$message = new Response(200, $headers); $message = new Response(200, $headers);