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