Skip to content

Commit 441fd2d

Browse files
authored
Merge pull request #103 from clue-labs/headers
Add request header accessors (à la PSR-7)
2 parents 04794ae + 0a0f683 commit 441fd2d

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,53 @@ $loop->run();
3636

3737
See also the [examples](examples).
3838

39+
## Usage
40+
41+
### Server
42+
43+
See the above usage example and the class outline for details.
44+
45+
### Request
46+
47+
See the above usage example and the class outline for details.
48+
49+
#### getHeaders()
50+
51+
The `getHeaders(): array` method can be used to
52+
return an array with ALL headers.
53+
54+
The keys represent the header name in the exact case in which they were
55+
originally specified. The values will be a string if there's only a single
56+
value for the respective header name or an array of strings if this header
57+
has multiple values.
58+
59+
> Note that this differs from the PSR-7 implementation of this method,
60+
which always returns an array for each header name, even if it only has a
61+
single value.
62+
63+
#### getHeader()
64+
65+
The `getHeader(string $name): string[]` method can be used to
66+
retrieve a message header value by the given case-insensitive name.
67+
68+
Returns a list of all values for this header name or an empty array if header was not found
69+
70+
#### getHeaderLine()
71+
72+
The `getHeaderLine(string $name): string` method can be used to
73+
retrieve a comma-separated string of the values for a single header.
74+
75+
Returns a comma-separated list of all values for this header name or an empty string if header was not found
76+
77+
#### hasHeader()
78+
79+
The `hasHeader(string $name): bool` method can be used to
80+
check if a header exists by the given case-insensitive name.
81+
82+
### Response
83+
84+
See the above usage example and the class outline for details.
85+
3986
## Install
4087

4188
The recommended way to install this library is [through Composer](http://getcomposer.org).

src/Request.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,69 @@ public function getHttpVersion()
4848
return $this->httpVersion;
4949
}
5050

51+
/**
52+
* Returns an array with ALL headers
53+
*
54+
* The keys represent the header name in the exact case in which they were
55+
* originally specified. The values will be a string if there's only a single
56+
* value for the respective header name or an array of strings if this header
57+
* has multiple values.
58+
*
59+
* Note that this differs from the PSR-7 implementation of this method,
60+
* which always returns an array for each header name, even if it only has a
61+
* single value.
62+
*
63+
* @return array
64+
*/
5165
public function getHeaders()
5266
{
5367
return $this->headers;
5468
}
5569

70+
/**
71+
* Retrieves a message header value by the given case-insensitive name.
72+
*
73+
* @param string $name
74+
* @return string[] a list of all values for this header name or an empty array if header was not found
75+
*/
76+
public function getHeader($name)
77+
{
78+
$found = array();
79+
80+
$name = strtolower($name);
81+
foreach ($this->headers as $key => $value) {
82+
if (strtolower($key) === $name) {
83+
foreach((array)$value as $one) {
84+
$found[] = $one;
85+
}
86+
}
87+
}
88+
89+
return $found;
90+
}
91+
92+
/**
93+
* Retrieves a comma-separated string of the values for a single header.
94+
*
95+
* @param string $name
96+
* @return string a comma-separated list of all values for this header name or an empty string if header was not found
97+
*/
98+
public function getHeaderLine($name)
99+
{
100+
return implode(', ', $this->getHeader($name));
101+
}
102+
103+
/**
104+
* Checks if a header exists by the given case-insensitive name.
105+
*
106+
* @param string $name
107+
* @return bool
108+
*/
109+
public function hasHeader($name)
110+
{
111+
return !!$this->getHeader($name);
112+
}
113+
56114
public function expectsContinue()
57115
{
58116
return isset($this->headers['Expect']) && '100-continue' === $this->headers['Expect'];

tests/RequestTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,38 @@ public function expectsContinueShouldBeTrueIfContinueExpected()
2323

2424
$this->assertTrue($request->expectsContinue());
2525
}
26+
27+
public function testEmptyHeader()
28+
{
29+
$request = new Request('GET', '/');
30+
31+
$this->assertEquals(array(), $request->getHeaders());
32+
$this->assertFalse($request->hasHeader('Test'));
33+
$this->assertEquals(array(), $request->getHeader('Test'));
34+
$this->assertEquals('', $request->getHeaderLine('Test'));
35+
}
36+
37+
public function testHeaderIsCaseInsensitive()
38+
{
39+
$request = new Request('GET', '/', array(), '1.1', array(
40+
'TEST' => 'Yes',
41+
));
42+
43+
$this->assertEquals(array('TEST' => 'Yes'), $request->getHeaders());
44+
$this->assertTrue($request->hasHeader('Test'));
45+
$this->assertEquals(array('Yes'), $request->getHeader('Test'));
46+
$this->assertEquals('Yes', $request->getHeaderLine('Test'));
47+
}
48+
49+
public function testHeaderWithMultipleValues()
50+
{
51+
$request = new Request('GET', '/', array(), '1.1', array(
52+
'Test' => array('a', 'b'),
53+
));
54+
55+
$this->assertEquals(array('Test' => array('a', 'b')), $request->getHeaders());
56+
$this->assertTrue($request->hasHeader('Test'));
57+
$this->assertEquals(array('a', 'b'), $request->getHeader('Test'));
58+
$this->assertEquals('a, b', $request->getHeaderLine('Test'));
59+
}
2660
}

0 commit comments

Comments
 (0)