Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 87921cd

Browse files
Freeaqingmeweierophinney
authored andcommitted
Refactored Basz's code a little. Added docblocks + unit tests
1 parent bacf55b commit 87921cd

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/Header/AbstractAccept.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ public function match($matchAgainst)
302302
foreach ($this->getPrioritized() as $left) {
303303
foreach ($matchAgainst as $right) {
304304
if ($right->type == '*' || $left->type == '*') {
305-
if ($res = $this->matchAcceptParams($left, $right)) {
306-
$res->matchedAgainst = $right;
305+
if ($this->matchAcceptParams($left, $right)) {
306+
$left->setMatchedAgainst($right);
307307

308-
return $res;
308+
return $left;
309309
}
310310
}
311311

@@ -315,10 +315,10 @@ public function match($matchAgainst)
315315
($left->format == $right->format ||
316316
$right->format == '*' || $left->format == '*')))
317317
{
318-
if ($res = $this->matchAcceptParams($left, $right)) {
319-
$res->matchedAgainst = $right;
318+
if ($this->matchAcceptParams($left, $right)) {
319+
$left->setMatchedAgainst($right);
320320

321-
return $res;
321+
return $left;
322322
}
323323
}
324324
}

src/Header/Accept/FieldValuePart/AbstractFieldValuePart.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ abstract class AbstractFieldValuePart
2727
*/
2828
private $internalValues;
2929

30+
/**
31+
* A Field Value Part this Field Value Part matched against.
32+
* @var AbstractFieldValuePart
33+
*/
34+
protected $matchedPart;
35+
3036
/**
3137
*
3238
* @param object $internalValues
@@ -36,6 +42,28 @@ public function __construct($internalValues)
3642
$this->internalValues = $internalValues;
3743
}
3844

45+
/**
46+
* Set a Field Value Part this Field Value Part matched against.
47+
*
48+
* @param AbstractFieldValuePart $matchedPart
49+
* return AbstractFieldValuePart provides fluent interface
50+
*/
51+
public function setMatchedAgainst(AbstractFieldValuePart $matchedPart)
52+
{
53+
$this->matchedPart = $matchedPart;
54+
return $this;
55+
}
56+
57+
/**
58+
* Get a Field Value Part this Field Value Part matched against.
59+
*
60+
* return AbstractFieldValuePart|null
61+
*/
62+
public function getMatchedAgainst()
63+
{
64+
return $this->matchedPart;
65+
}
66+
3967
/**
4068
*
4169
* @return object

test/Header/AcceptTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,38 @@ public function testParsingAndAssemblingQuotedStrings()
193193
}
194194

195195

196+
public function testMatchReturnsMatchedAgainstObject()
197+
{
198+
$acceptStr = 'Accept: text/html;q=1; version=23; level=5, text/json;level=1,' .
199+
'text/xml;level=2;q=0.4';
200+
$acceptHeader = Accept::fromString($acceptStr);
201+
202+
$res = $acceptHeader->match('text/html; _randomValue=foobar');
203+
$this->assertInstanceOf(
204+
'Zend\Http\Header\Accept\FieldValuePart\AbstractFieldValuePart',
205+
$res->getMatchedAgainst()
206+
);
207+
$this->assertEquals(
208+
'foobar',
209+
$res->getMatchedAgainst()->getParams()->_randomValue
210+
);
211+
212+
$acceptStr = 'Accept: */*; ';
213+
$acceptHeader = Accept::fromString($acceptStr);
214+
215+
$res = $acceptHeader->match('text/html; _foo=bar');
216+
$this->assertInstanceOf(
217+
'Zend\Http\Header\Accept\FieldValuePart\AbstractFieldValuePart',
218+
$res->getMatchedAgainst()
219+
);
220+
221+
$this->assertEquals(
222+
'bar',
223+
$res->getMatchedAgainst()->getParams()->_foo
224+
);
225+
}
226+
227+
196228
public function testVersioning()
197229
{
198230
$acceptStr = 'Accept: text/html;q=1; version=23; level=5, text/json;level=1,' .

0 commit comments

Comments
 (0)