1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright (c) 2006-present brian ridley |
5
|
|
|
* @author brian ridley <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ptlis\ConNeg\Preference\Matched; |
10
|
|
|
|
11
|
|
|
use ptlis\ConNeg\Preference\PreferenceInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class for matched preferences. |
15
|
|
|
*/ |
16
|
|
|
class MatchedPreference implements MatchedPreferenceInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Which HTTP field the match relates to. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $fromField; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The preference from the client. |
27
|
|
|
* |
28
|
|
|
* @var PreferenceInterface |
29
|
|
|
*/ |
30
|
|
|
private $clientPref; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The preference from the server. |
34
|
|
|
* |
35
|
|
|
* @var PreferenceInterface |
36
|
|
|
*/ |
37
|
|
|
private $serverPref; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $fromField |
44
|
|
|
* @param PreferenceInterface $serverPref |
45
|
|
|
* @param PreferenceInterface $clientPref |
46
|
|
|
*/ |
47
|
82 |
|
public function __construct($fromField, PreferenceInterface $clientPref, PreferenceInterface $serverPref) |
48
|
|
|
{ |
49
|
82 |
|
$this->fromField = $fromField; |
50
|
82 |
|
$this->clientPref = $clientPref; |
51
|
82 |
|
$this->serverPref = $serverPref; |
52
|
82 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
77 |
|
public function getClientPreference() |
58
|
|
|
{ |
59
|
77 |
|
return $this->clientPref; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
77 |
|
public function getServerPreference() |
66
|
|
|
{ |
67
|
77 |
|
return $this->serverPref; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the shared variant for this pair. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
71 |
|
public function getVariant() |
76
|
|
|
{ |
77
|
|
|
// Special handling for language partial matches. These require that the returned variant name is the portion of |
78
|
|
|
// the language before the wildcard as this is what the application will be using to encode the response. |
79
|
71 |
|
if ($this->isLanguageWildcard()) { |
80
|
6 |
|
return str_replace('-*', '', $this->serverPref->getVariant()); |
81
|
|
|
|
82
|
|
|
// If the client contained a wildcard or is absent return the concrete variant from teh server. |
83
|
71 |
|
} elseif ($this->clientWildcardOrAbsent()) { |
84
|
56 |
|
return $this->serverPref->getVariant(); |
85
|
|
|
|
86
|
|
|
// In all other cases the client is canonical |
87
|
|
|
} else { |
88
|
39 |
|
return $this->clientPref->getVariant(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns true if the match was by partial language wildcard. |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
71 |
|
private function isLanguageWildcard() |
98
|
|
|
{ |
99
|
71 |
|
return PreferenceInterface::LANGUAGE === $this->fromField |
100
|
71 |
|
&& PreferenceInterface::PARTIAL_WILDCARD === $this->serverPref->getPrecedence(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns true if the client contained a wildcard or was absent. |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
71 |
|
private function clientWildcardOrAbsent() |
109
|
|
|
{ |
110
|
71 |
|
return !(strlen($this->clientPref->getVariant()) && !strstr($this->clientPref->getVariant(), '*')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the product of the server & client quality factors. |
115
|
|
|
* |
116
|
|
|
* @return float |
117
|
|
|
*/ |
118
|
77 |
|
public function getQualityFactor() |
119
|
|
|
{ |
120
|
77 |
|
return $this->clientPref->getQualityFactor() * $this->serverPref->getQualityFactor(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the combined precedence. |
125
|
|
|
* |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
29 |
|
public function getPrecedence() |
129
|
|
|
{ |
130
|
29 |
|
return $this->getServerPreference()->getPrecedence() + $this->getClientPreference()->getPrecedence(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create string representation of the preference. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
2 |
|
public function __toString() |
139
|
|
|
{ |
140
|
2 |
|
return $this->getVariant() . ';q=' . $this->getQualityFactor(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|