Skip to content

Added ability to get headers as associative array #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 5, 2017
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));
}
}