Skip to content

Commit d6bd86d

Browse files
authored
Made headers as associative array
1 parent 59fa31c commit d6bd86d

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

lib/Response.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,49 @@ public function body()
6464
/**
6565
* The response headers
6666
*
67+
* @param bool $assoc
68+
*
6769
* @return array
6870
*/
69-
public function headers()
71+
public function headers($assoc = false)
72+
{
73+
if (!$assoc) {
74+
return $this->headers;
75+
}
76+
77+
return $this->prettifyHeaders($this->headers):
78+
}
79+
80+
/**
81+
* Returns response headers as associative array
82+
*
83+
* @param array $headers
84+
*
85+
* @return array
86+
*
87+
* @throws \InvalidArgumentException
88+
*/
89+
private function prettifyHeaders($headers)
7090
{
71-
return $this->headers;
91+
if (!is_array($headers)) {
92+
throw new \InvalidArgumentException('$headers should be array');
93+
}
94+
95+
return array_reduce(
96+
$headers,
97+
function ($result, $header) {
98+
if (false === strpos(':', $header) {
99+
$result['status'] = $header;
100+
101+
return $result;
102+
}
103+
104+
list ($key, $value) = explode(':', $header);
105+
$result[$key] = $value;
106+
107+
return $result;
108+
},
109+
[]
110+
);
72111
}
73-
}
112+
}

0 commit comments

Comments
 (0)