1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Negotiation; |
4
|
|
|
|
5
|
|
|
class Negotiator extends AbstractNegotiator |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* {@inheritdoc} |
9
|
|
|
*/ |
10
|
39 |
|
protected function acceptFactory($accept) |
11
|
|
|
{ |
12
|
39 |
|
return new Accept($accept); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
35 |
|
protected function match(AcceptHeader $accept, AcceptHeader $priority, $index) |
19
|
|
|
{ |
20
|
35 |
|
if (!$accept instanceof Accept || !$priority instanceof Accept) { |
21
|
|
|
return null; |
22
|
|
|
} |
23
|
|
|
|
24
|
35 |
|
$acceptBase = $accept->getBasePart(); |
25
|
35 |
|
$priorityBase = $priority->getBasePart(); |
26
|
|
|
|
27
|
35 |
|
$acceptSub = $accept->getSubPart(); |
28
|
35 |
|
$prioritySub = $priority->getSubPart(); |
29
|
|
|
|
30
|
35 |
|
$intersection = array_intersect_assoc($accept->getParameters(), $priority->getParameters()); |
31
|
|
|
|
32
|
35 |
|
$baseEqual = !strcasecmp($acceptBase, $priorityBase); |
33
|
35 |
|
$subEqual = !strcasecmp($acceptSub, $prioritySub); |
34
|
|
|
|
35
|
35 |
|
if (($acceptBase === '*' || $baseEqual) |
36
|
35 |
|
&& ($acceptSub === '*' || $subEqual) |
37
|
35 |
|
&& count($intersection) === count($accept->getParameters()) |
38
|
35 |
|
) { |
39
|
30 |
|
$score = 100 * $baseEqual + 10 * $subEqual + count($intersection); |
40
|
|
|
|
41
|
30 |
|
return new Match($accept->getQuality() * $priority->getQuality(), $score, $index); |
42
|
|
|
} |
43
|
|
|
|
44
|
34 |
|
if (!strstr($acceptSub, '+') || !strstr($prioritySub, '+')) { |
45
|
34 |
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Handle "+" segment wildcards |
49
|
4 |
|
list($acceptSub, $acceptPlus) = $this->splitSubPart($acceptSub); |
50
|
4 |
|
list($prioritySub, $priorityPlus) = $this->splitSubPart($prioritySub); |
51
|
|
|
|
52
|
4 |
|
$subEqual = !strcasecmp($acceptSub, $prioritySub); |
53
|
4 |
|
$plusEqual = !strcasecmp($acceptPlus, $priorityPlus); |
54
|
|
|
|
55
|
4 |
|
if (($acceptBase === '*' || $baseEqual) |
56
|
4 |
|
&& ($acceptSub === '*' || $subEqual || $acceptPlus === '*' || $plusEqual) |
57
|
4 |
|
&& count($intersection) === count($accept->getParameters()) |
58
|
4 |
|
) { |
59
|
4 |
|
$score = 100 * $baseEqual + 10 * $subEqual + $plusEqual + count($intersection); |
60
|
|
|
|
61
|
4 |
|
return new Match($accept->getQuality() * $priority->getQuality(), $score, $index); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Split a subpart into the subpart and "plus" part. |
69
|
|
|
* |
70
|
|
|
* For media-types of the form "application/vnd.example+json", matching |
71
|
|
|
* should allow wildcards for either the portion before the "+" or |
72
|
|
|
* after. This method splits the subpart to allow such matching. |
73
|
|
|
*/ |
74
|
4 |
|
protected function splitSubPart($subPart) |
75
|
|
|
{ |
76
|
4 |
|
if (!strstr($subPart, '+')) { |
77
|
|
|
return [$subPart, '']; |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
return explode('+', $subPart, 2); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|