-
Notifications
You must be signed in to change notification settings - Fork 66
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
Changes from all commits
d6bd86d
812f2f1
73f72eb
3c554f5
39f164a
f8f2208
431f333
7df167b
cc25fa9
257fede
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
if (!is_array($headers)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove array_filter, you can filter in reduce function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You already filter it in reduce |
||
function ($result, $header) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, ':')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why header not name? Can you provider description this case? |
||
$result['Status'] = trim($header); | ||
|
||
return $result; | ||
} | ||
|
||
list ($key, $value) = explode(':', $header, 2); | ||
$result[trim($key)] = trim($value); | ||
|
||
return $result; | ||
}, | ||
[] | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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)