From 20012dc67137a27194c235ab572771f7af9caeab Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 16 Aug 2020 08:49:47 -0400 Subject: [PATCH] Message constructor accepts $headers values as string or string[] --- src/Message/Message.php | 22 ++++++++++------------ src/Message/Request.php | 4 ++-- tests/Message/MessageTest.php | 9 ++++++++- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Message/Message.php b/src/Message/Message.php index c5f9758..8490c4e 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -22,13 +22,13 @@ abstract class Message implements MessageInterface * Create a new Message, optionally with headers and a body. * * $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 * a NullStream instance for the message body. * * @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 * entity body */ @@ -37,19 +37,17 @@ abstract class Message implements MessageInterface ?StreamInterface $body = null ) { $this->headers = new HeaderCollection(); - if ($headers) { - foreach ($headers as $name => $values) { - foreach ($values as $value) { - $this->headers[$name] = $value; - } + + foreach ($headers as $name => $values) { + if (is_string($values)) { + $values = [$values]; + } + foreach ($values as $value) { + $this->headers[$name] = $value; } } - if ($body !== null) { - $this->body = $body; - } else { - $this->body = new Stream(''); - } + $this->body = $body ?? new Stream(''); } public function __clone() diff --git a/src/Message/Request.php b/src/Message/Request.php index ca22f56..55eeafb 100644 --- a/src/Message/Request.php +++ b/src/Message/Request.php @@ -34,7 +34,7 @@ class Request extends Message implements RequestInterface * Create a new Request. * * $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 * a NullStream instance for the message body. @@ -42,7 +42,7 @@ class Request extends Message implements RequestInterface * @param string $method * @param string|UriInterface $uri * @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 * entity body */ diff --git a/tests/Message/MessageTest.php b/tests/Message/MessageTest.php index 11d5565..5b84cbd 100644 --- a/tests/Message/MessageTest.php +++ b/tests/Message/MessageTest.php @@ -7,7 +7,14 @@ use WellRESTed\Test\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']]; $message = new Response(200, $headers);