|
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
|
|
|
* Comparator used to order the preferences. |
|
15
|
|
|
*/ |
|
16
|
|
|
class MatchedPreferenceComparator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Comparison function used for ordering matched preferences. |
|
20
|
|
|
* |
|
21
|
|
|
* @param MatchedPreferenceInterface $lValue |
|
22
|
|
|
* @param MatchedPreferenceInterface $rValue |
|
23
|
|
|
* |
|
24
|
|
|
* @return int -1, 0, 1 (see usort() callback for meaning) |
|
25
|
|
|
*/ |
|
26
|
75 |
|
public function compare(MatchedPreferenceInterface $lValue, MatchedPreferenceInterface $rValue) |
|
27
|
|
|
{ |
|
28
|
|
|
// Compare by quality factors - highest quality factor has precedence. |
|
29
|
75 |
|
$result = $this->compareQualityFactorPair($lValue, $rValue); |
|
30
|
|
|
|
|
31
|
|
|
// Quality factors are equal attempt to sort by match precedence |
|
32
|
75 |
|
if (0 === $result) { |
|
33
|
27 |
|
$result = $this->comparePrecedence($lValue, $rValue); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Quality factors & precedences match, simply sort alphabetically as this ensures that the sort is stable |
|
37
|
75 |
|
if (0 === $result) { |
|
38
|
19 |
|
$result = $this->compareVariant($lValue, $rValue); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
75 |
|
return $result; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Comparison function for quality factors of matched preferences. |
|
46
|
|
|
* |
|
47
|
|
|
* @param MatchedPreferenceInterface $lValue |
|
48
|
|
|
* @param MatchedPreferenceInterface $rValue |
|
49
|
|
|
* |
|
50
|
|
|
* @return int -1, 0, 1 (see usort() callback for meaning) |
|
51
|
|
|
*/ |
|
52
|
75 |
|
private function compareQualityFactorPair( |
|
53
|
|
|
MatchedPreferenceInterface $lValue, |
|
54
|
|
|
MatchedPreferenceInterface $rValue |
|
55
|
|
|
) { |
|
56
|
|
|
// Build a list of quality factor comparisons to perform; highest preference given to quality factor products, |
|
57
|
|
|
// followed by those provided by the client & finally the server provided. |
|
58
|
|
|
$compareList = array( |
|
59
|
|
|
array( |
|
60
|
75 |
|
'left' => $lValue, |
|
61
|
75 |
|
'right' => $rValue |
|
62
|
|
|
), |
|
63
|
|
|
array( |
|
64
|
75 |
|
'left' => $lValue->getClientPreference(), |
|
65
|
75 |
|
'right' => $rValue->getClientPreference() |
|
66
|
|
|
), |
|
67
|
|
|
array( |
|
68
|
75 |
|
'left' => $lValue->getServerPreference(), |
|
69
|
75 |
|
'right' => $rValue->getServerPreference() |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
75 |
|
$result = 0; |
|
74
|
75 |
|
foreach ($compareList as $compare) { |
|
75
|
75 |
|
$result = $this->compareQualityFactor($compare['left'], $compare['right']); |
|
76
|
|
|
|
|
77
|
|
|
// If a non matching result was found then we have the result of our comparison |
|
78
|
75 |
|
if (0 !== $result) { |
|
79
|
57 |
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
75 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Compare quality factors of preferences. |
|
88
|
|
|
* |
|
89
|
|
|
* @param PreferenceInterface $lValue |
|
90
|
|
|
* @param PreferenceInterface $rValue |
|
91
|
|
|
* |
|
92
|
|
|
* @return int -1, 0, 1 (see usort() callback for meaning) |
|
93
|
|
|
*/ |
|
94
|
75 |
|
private function compareQualityFactor(PreferenceInterface $lValue, PreferenceInterface $rValue) |
|
95
|
|
|
{ |
|
96
|
75 |
|
if ($rValue->getQualityFactor() < $lValue->getQualityFactor()) { |
|
97
|
48 |
|
return -1; |
|
98
|
74 |
|
} elseif ($rValue->getQualityFactor() > $lValue->getQualityFactor()) { |
|
99
|
36 |
|
return 1; |
|
100
|
|
|
} else { |
|
101
|
63 |
|
return 0; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Compare precedences of preferences. |
|
107
|
|
|
* |
|
108
|
|
|
* @param PreferenceInterface $lValue |
|
109
|
|
|
* @param PreferenceInterface $rValue |
|
110
|
|
|
* |
|
111
|
|
|
* @return int -1, 0, 1 (see usort() callback for meaning) |
|
112
|
|
|
*/ |
|
113
|
27 |
|
private function comparePrecedence(PreferenceInterface $lValue, PreferenceInterface $rValue) |
|
114
|
|
|
{ |
|
115
|
27 |
|
if ($rValue->getPrecedence() < $lValue->getPrecedence()) { |
|
116
|
8 |
|
return -1; |
|
117
|
27 |
|
} elseif ($rValue->getPrecedence() > $lValue->getPrecedence()) { |
|
118
|
8 |
|
return 1; |
|
119
|
|
|
} else { |
|
120
|
19 |
|
return 0; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Compare preferences alphabetically |
|
126
|
|
|
* |
|
127
|
|
|
* @param MatchedPreferenceInterface $lValue |
|
128
|
|
|
* @param MatchedPreferenceInterface $rValue |
|
129
|
|
|
* |
|
130
|
|
|
* @return int -1, 0, 1 (see usort() callback for meaning) |
|
131
|
|
|
*/ |
|
132
|
19 |
|
private function compareVariant(MatchedPreferenceInterface $lValue, MatchedPreferenceInterface $rValue) |
|
133
|
|
|
{ |
|
134
|
19 |
|
return strcasecmp($lValue->getVariant(), $rValue->getVariant()); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|