|
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\Negotiator\Matcher; |
|
10
|
|
|
|
|
11
|
|
|
use ptlis\ConNeg\Preference\Matched\MatchedPreference; |
|
12
|
|
|
use ptlis\ConNeg\Preference\Matched\MatchedPreferenceComparator; |
|
13
|
|
|
use ptlis\ConNeg\Preference\Matched\MatchedPreferenceInterface; |
|
14
|
|
|
use ptlis\ConNeg\Preference\PreferenceInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Matcher looking for exact variant matches. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ExactMatcher implements MatcherInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var MatchedPreferenceComparator |
|
23
|
|
|
*/ |
|
24
|
|
|
private $comparator; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param MatchedPreferenceComparator $comparator |
|
31
|
|
|
*/ |
|
32
|
76 |
|
public function __construct(MatchedPreferenceComparator $comparator) |
|
33
|
|
|
{ |
|
34
|
76 |
|
$this->comparator = $comparator; |
|
35
|
76 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
54 |
|
public function hasMatch($fromField, array $matchingList, PreferenceInterface $clientPref) |
|
41
|
|
|
{ |
|
42
|
54 |
|
return $this->getMatchingIndex($matchingList, $clientPref) >= 0; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
36 |
|
public function match($fromField, array $matchingList, PreferenceInterface $clientPref) |
|
49
|
|
|
{ |
|
50
|
36 |
|
$matchIndex = $this->getMatchingIndex($matchingList, $clientPref); |
|
51
|
|
|
|
|
52
|
36 |
|
if ($matchIndex >= 0) { |
|
53
|
36 |
|
$newMatch = new MatchedPreference( |
|
54
|
36 |
|
$fromField, |
|
55
|
|
|
$clientPref, |
|
56
|
36 |
|
$matchingList[$matchIndex]->getServerPreference() |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
36 |
|
if ($this->comparator->compare($matchingList[$matchIndex], $newMatch) > 0) { |
|
60
|
36 |
|
$matchingList[$matchIndex] = $newMatch; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
36 |
|
return $matchingList; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the first index containing a matching variant, or -1 if not present. |
|
69
|
|
|
* |
|
70
|
|
|
* @param MatchedPreferenceInterface[] $matchingList |
|
71
|
|
|
* @param PreferenceInterface $pref |
|
72
|
|
|
* |
|
73
|
|
|
* @return int |
|
74
|
|
|
*/ |
|
75
|
54 |
|
private function getMatchingIndex(array $matchingList, PreferenceInterface $pref) |
|
76
|
|
|
{ |
|
77
|
54 |
|
$index = -1; |
|
78
|
|
|
|
|
79
|
54 |
|
foreach ($matchingList as $key => $match) { |
|
80
|
53 |
|
if ($match->getVariant() === $pref->getVariant()) { |
|
81
|
36 |
|
$index = $key; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
54 |
|
return $index; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|