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

Commit 1e41fbd

Browse files
committed
Merge branch 'hotfix/2615'
Introduce AcceptableViewModelSelector, and remove Accept matching from JsonStrategy and FeedStrategy Close zendframework/zendframework#2615 Close zendframework/zendframework#2410

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/Header/AbstractAccept.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ protected function hasType($matchAgainst)
291291
* Match a media string against this header
292292
*
293293
* @param array|string $matchAgainst
294-
* @return array|boolean The matched value or false
294+
* @return AcceptFieldValuePart|boolean The matched value or false
295295
*/
296296
public function match($matchAgainst)
297297
{
@@ -302,8 +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-
return $res;
305+
if ($this->matchAcceptParams($left, $right)) {
306+
$left->setMatchedAgainst($right);
307+
308+
return $left;
307309
}
308310
}
309311

@@ -313,8 +315,10 @@ public function match($matchAgainst)
313315
($left->format == $right->format ||
314316
$right->format == '*' || $left->format == '*')))
315317
{
316-
if ($res = $this->matchAcceptParams($left, $right)) {
317-
return $res;
318+
if ($this->matchAcceptParams($left, $right)) {
319+
$left->setMatchedAgainst($right);
320+
321+
return $left;
318322
}
319323
}
320324
}

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 $matchedAgainst;
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 $matchedAgainst)
52+
{
53+
$this->matchedAgainst = $matchedAgainst;
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->matchedAgainst;
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)