diff --git a/src/Fracture/Http/Headers/Common.php b/src/Fracture/Http/Headers/Common.php index b286605..8cf9e70 100755 --- a/src/Fracture/Http/Headers/Common.php +++ b/src/Fracture/Http/Headers/Common.php @@ -64,4 +64,14 @@ public function isFinal() { return false; } + + + public function getParameter($name) + { + if (array_key_exists($name, $this->data)) { + return $this->data[$name]; + } + + return null; + } } diff --git a/src/Fracture/Http/RequestBuilder.php b/src/Fracture/Http/RequestBuilder.php index 76e587b..e8f8dd6 100644 --- a/src/Fracture/Http/RequestBuilder.php +++ b/src/Fracture/Http/RequestBuilder.php @@ -70,7 +70,7 @@ protected function applyContentParsers($instance) foreach ($this->parsers as $value => $parser) { if ($header->contains($value)) { - $parameters = $this->alterParameters($parameters, $parser, $value); + $parameters += $this->alterParameters($parser, $value, $header); } } @@ -78,17 +78,16 @@ protected function applyContentParsers($instance) } - private function alterParameters($parameters, $parser, $value) + private function alterParameters($parser, $value, $header) { - $result = call_user_func($parser); + $result = call_user_func($parser, $header); if (false === is_array($result)) { $message = "Parser for '$value' did not return a 'name => value' array of parameters"; trigger_error($message, \E_USER_WARNING); } - $parameters += $result; - return $parameters; + return $result; } diff --git a/tests/unit/Fracture/Http/Headers/CommonTest.php b/tests/unit/Fracture/Http/Headers/CommonTest.php index 499fd3c..91162da 100755 --- a/tests/unit/Fracture/Http/Headers/CommonTest.php +++ b/tests/unit/Fracture/Http/Headers/CommonTest.php @@ -104,4 +104,22 @@ public function testParsedDataForPreparedInstance() $instance->prepare(); $this->assertSame('beta', $instance->getParsedData()); } + + + /** + * @covers Fracture\Http\Headers\Common::__construct + * @covers Fracture\Http\Headers\Common::prepare + * @covers Fracture\Http\Headers\Common::getParsedData + * @covers Fracture\Http\Headers\Common::getParameter + */ + public function testValueRetrieval() + { + $instance = $this->getMockForAbstractClass('Fracture\Http\Headers\Common', ['type/subtype; name=4']); + $instance->expects($this->any()) + ->method('extractData') + ->will($this->returnValue(['name' => 4])); + + $instance->prepare(); + $this->assertSame(4, $instance->getParameter('name')); + } } diff --git a/tests/unit/Fracture/Http/RequestBuilderTest.php b/tests/unit/Fracture/Http/RequestBuilderTest.php index 997cbc5..a6e0184 100644 --- a/tests/unit/Fracture/Http/RequestBuilderTest.php +++ b/tests/unit/Fracture/Http/RequestBuilderTest.php @@ -335,6 +335,31 @@ public function testAppliedContentParsersWithMissingHeader() } + + /** + * @covers Fracture\Http\RequestBuilder::create + * @covers Fracture\Http\RequestBuilder::applyContentParsers + * @covers Fracture\Http\RequestBuilder::addContentParser + */ + public function testAppliedContentParsersWithHeader() + { + $input = [ + 'server' => [ + 'CONTENT_TYPE' => 'text/html;version=2', + ], + ]; + + $builder = $this->getMock('Fracture\Http\RequestBuilder', ['isCLI']); + + $builder->addContentParser('text/html', function ($header) { + return ['foo' => $header->getParameter('version')]; + }); + + $instance = $builder->create($input); + $this->assertEquals(2, $instance->getParameter('foo')); + } + + /** * @covers Fracture\Http\RequestBuilder::create * @covers Fracture\Http\RequestBuilder::applyContentParsers