Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public function makeRequest($method, $url, $body = null, $headers = null)
$responseHeaders = substr($response, 0, $headerSize);

$responseHeaders = explode("\n", $responseHeaders);
$responseHeaders = array_map('trim', $responseHeaders);

curl_close($curl);

Expand Down
49 changes: 46 additions & 3 deletions lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,53 @@ public function body()
/**
* The response headers
*
* @param bool $assoc
*
* @return array
*/
public function headers()
public function headers($assoc = false)
{
return $this->headers;
if (!$assoc) {
return $this->headers;
}

return $this->prettifyHeaders($this->headers);
}

/**
* Returns response headers as associative array
*
* @param array $headers
*
* @return array
*
* @throws \InvalidArgumentException
*/
private function prettifyHeaders($headers)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private function prettifyHeaders(array $headers)

{
if (!is_array($headers)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove condition and exception, you can pass only array type for first argument

throw new \InvalidArgumentException('$headers should be array');
}

return array_reduce(
array_filter($headers),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove array_filter, you can filter in reduce function

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already filter it in reduce

function ($result, $header) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function (array $result, $header) {

if (empty($header)) {
return $result;
}

if (false === strpos($header, ':')) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why header not name? Can you provider description this case?
I think is dirty hack

$result['Status'] = trim($header);

return $result;
}

list ($key, $value) = explode(':', $header, 2);
$result[trim($key)] = trim($value);

return $result;
},
[]
);
}
}
}
9 changes: 8 additions & 1 deletion test/unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ public function testHeaders()

$this->assertEquals(['Content-Type: text/html'], $response->headers());
}
}

public function testAssociativeHeaders()
{
$response = new Response(null, null, ['Content-Type: text/html', 'HTTP/1.1 200 OK']);

$this->assertEquals(['Content-Type' => 'text/html', 'Status' => 'HTTP/1.1 200 OK'], $response->headers(true));
}
}